(新手)在winform窗体上创建一个按钮,点击按钮可以生成菱形,平行四边形,三角形这种图形
if (radioButton2.Checked)//画直线
{
if (CursorD)
{
e.Graphics.DrawLine(new Pen(Color.Red), Spt, Ept);
}
}
我是在学MFC画图,这个c#文件是昨天碰巧下的。
所以外行看起来过程是这样的:
program.cs设置入口函数,form.cs放处理函数。(窗口(像vb?)上按钮什么的准备好)
1、初始化。
public Form1()
{
InitializeComponent();
StartDraw();//这个是为了代码的整洁?
CursorD = false;
TwoClick = false;
DoubleClick = false;
}
// 下载于www.51aspx.com
private void StartDraw()
{
img = new Bitmap(500,500);
g = Graphics.FromImage(img);
g.Clear(Color.White);
}
private void StartDraw()
{
//绘图需要画笔和画布,
//img画布Graphics gs = Graphics.FromImage(img);
//画笔Pen newPen = new Pen(Color.Red, 1);
img = new Bitmap(500,500);
g = Graphics.FromImage(img);
g.Clear(Color.White);//清屏
}
2、绘图(paint函数)
private void Form1_Paint(object sender, PaintEventArgs e)
if一次只有2个分支,多种画图倾向于switch-case结构。
这个PaintEventArgs大概和mfc的CDC差不多,句柄(确定窗口,有一些sever函数)
radioButton2.Checked 可以直接读 “按钮被打上记号“
3、鼠标事件
在msdn上有graphics methods,我在mfc用到的并没有直接的菱形,平行四边形,三角形,但是可以用
public void DrawLine(
Pen pen,
Point pt1,
Point pt2
)
pen
Type: System.Drawing .Pen
Pen that determines the color, width, and style of the line.
pt1
Type: System.Drawing .Point
Point structure that represents the first point to connect.
pt2
Type: System.Drawing .Point
Point structure that represents the second point to connect.
点的关系根据平面几何的定理应该很容易。
诶呀,我是不是讲了一堆废话。