程序源码下载链接:http://download.csdn.net/detail/y85171642/5361537
该程序未使用多线程技术,主要是楼主对多线程不是很了解,求大神改进!
主要使用2D 画图,
求好的思路想法!(因为本程序运行有时候会卡死,原因不明,然后直接跳出移动的过程得到结果)
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Hanoi_02
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int penWidth = 1;//画笔的粗细
int diskHCut = 15; //圆盘的宽度差,单一一侧的差值。(宽度差要*2)H 横向
int diskVSpace = 3;//圆盘之间高度上下的距离。V纵向
int diskWidth = 200; //最大号圆盘的宽度
int diskHeight = 17;//圆盘的高度
int moveSpeed = 500;//每秒移动的像素点速度
int moveInterval = 20;//每次移动的像素点个数
int bmpWidth = 660;
int bmpHeight = 420;
int dPillar = 10;//柱子间间距
int pillarHeight = 300;//柱子高度
int count = 3;//圆盘的个数
private void Form1_Load(object sender, EventArgs e)
{
drawPicBoxBackground();
initDisk(count);
comboBox1.SelectedIndex = 0;
}
void initDisk(int num)
{
count = num;
Bitmap bmp = new Bitmap(bmpWidth, bmpHeight);
Graphics g = Graphics.FromImage(bmp);
Pen p = new Pen(Color.Blue, penWidth);//背景粗细
int pw = bmpWidth / 3;
for (int i = 0; i < num; i++)
{
int nowDiskWidth = diskWidth - i * diskHCut;
int x0 = (pw - nowDiskWidth) / 2;
int y0 = (bmpHeight - diskVSpace * i - diskHeight * i - 1 - diskVSpace - diskHeight);
int x1 = nowDiskWidth;
int y1 = diskHeight;
g.DrawRectangle(p, x0, y0, x1, y1);
}
pictureBox1.Image = bmp;
p.Dispose();
g.Dispose();
}
public void drawPicBoxBackground()
{
pictureBox1.Width = bmpWidth;
pictureBox1.Height = bmpHeight;
Bitmap bmp = new Bitmap(bmpWidth, bmpHeight);
Graphics g = Graphics.FromImage(bmp);
Pen p = new Pen(Color.Black, 1);//背景粗细
int pw = bmpWidth / 3;
for (int i = 0; i < 3; i++)
{
int Vline = pw / 2 + pw * i - 1;
g.DrawLine(p, Vline, bmpHeight - pillarHeight, Vline, bmpHeight - 1);
g.DrawLine(p, dPillar + pw * i, bmpHeight - 1, pw * i + pw - dPillar - 1, bmpHeight - 1);
}
pictureBox1.B