这个是用Windwow From Control Library 的工程。添加了一个控件为Panel 的控件,并未这个控件添加了paint的事件。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _20160423汉诺塔
{
public partial class UserControl1: UserControl
{
private Pen myPen = new Pen(Color.Green, 10);
private Pen myPen1 = new Pen(Color.Red);
private SolidBrush mySolidBrush = new SolidBrush(Color.Green);
private const int recganleHeight = 20;
//起始点
private int StartX = 4;
private int StartY = 20;
private int ColNode = 0;
private int RowNode = 0;
private float radio = 1.1f;//缩小比列(仅限与宽度)
public UserControl1()
{
InitializeComponent();
this.Invalidate();
}
private void initSize()
{
//固定他的大小
this.Width = 688;
this.Height = 523;
}
private void UserControl1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
//初始化数据
ColNode = this.Width / 3- 20;
RowNode = this.Height -10;
//画第一个钉子图
//画横线
Point RowOne1 = new Point(StartX, RowNode);
Point RowOne2 = new Point(ColNode + StartX, RowNode);
g.DrawLine(myPen, RowOne1, RowOne2);
int mid = ColNode / 2 + StartX;
//画竖线
Point ColOne1 = new Point(mid, StartY);
Point ColOne2 = new Point(mid, RowNode);
g.DrawLine(myPen, ColOne1, ColOne2);
//画第2s个钉子图
//画横线
int x2 = StartX + 30 + ColNode;//第二个图形地点的位置
RowOne1 = new Point(x2, RowNode);
RowOne2 = new Point(ColNode + x2, RowNode);
g.DrawLine(myPen, RowOne1, RowOne2);
mid = ColNode / 2 + x2;
//画竖线
ColOne1 = new Point(mid, StartY);
ColOne2 = new Point(mid, RowNode);
g.DrawLine(myPen, ColOne1, ColOne2);
//画第3个钉子图
int x3 = x2 + 30 + ColNode;
RowOne1 = new Point(x3, RowNode);
RowOne2 = new Point(ColNode + x3, RowNode);
g.DrawLine(myPen, RowOne1, RowOne2);
mid = ColNode / 2 + x3;
//画竖线
ColOne1 = new Point(mid, StartY);
ColOne2 = new Point(mid, RowNode);
g.DrawLine(myPen, ColOne1, ColOne2);
//开始画累积木
int xPlus = ColNode / 20;
float rectangleWide = (float)ColNode / radio;
for (int i = 1; i <4 ; i++)
{
g.DrawRectangle(myPen1, StartX + xPlus * i, RowNode - recganleHeight *i -6 , rectangleWide, recganleHeight);
rectangleWide = rectangleWide -26;
}
}
}
}