关于C#的简单问题,第一次提问,请大家帮帮忙

来源:百度知道 编辑:UC知道 时间:2024/07/02 17:02:18
我想完成的任务:mousedown取一个点,mouseup取一个点,在两点间画一条线。我的代码如下:
namespace Line
{
public partial class Form1 : Form
{
int l1, l2, l3, l4;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{

}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
l1 = e.X; l2 = e.Y;

}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
l3 = e.X; l4 = e.Y;

}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics graLine = e.Graphics;
Pen myPen = new Pen(Color.Blue);
graLine.DrawLine(myPen, l1, l2, l3, l4);
}
}
}
问题:运行时直线不会马上出现,需要将窗体最小化,再最大化才会出现,不知道是什么原因……
是不是要将窗体重绘啊?怎么

增加一个笔刷的方法
方法里面的内容:
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics as System.Drawing.Graphics
formGraphics = Me.CreateGraphics()
formGraphics.DrawEllipse(myPen, New Rectangle(L1,L2,L3,L4))
myPen.Dispose()
formGraphics.Dispose()
或者是将上面的代码写成任意的方法,在下面调用他即可.
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
l3 = e.X; l4 = e.Y;
调用上面的方法
}

你没有帮助吗!帮助上有的啊 !
你看看下面的 复制一下就可以用
private Rectangle RcDraw;
private float PenWidth = 10;

private void Form1_MouseDown_1(object sender, System.Windows.Forms.MouseEventArgs e)
{

// Determine the initial rectangle coordinates...

RcDraw.X = e.X;
RcDraw.Y = e.Y;

}

private void Form1_MouseUp_1(object sender, System.Windows.Forms.MouseEventArgs e)
{