【发布时间】:2017-07-17 11:48:29
【问题描述】:
我尝试使用鼠标上下在位图上绘制矩形。但问题是矩形总是延迟一个事件。例如,我第一次尝试绘制一个矩形(0,0,50,50),但位图上没有矩形绘制。我继续绘制一个矩形(50,50,100,100),但创建了一个矩形(0,0,50,50)(不是矩形(50,50,100,100)。如果我继续绘制下一个矩形,它总是这样延迟。请帮助我! 这是我的代码:
Rectangle rect = new Rectangle(0, 0, 0, 0);
int Xmouse;
int Ymouse;
public Form1()
{
InitializeComponent();
pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
this.DoubleBuffered = true;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Xmouse = e.X;
Ymouse = e.Y;
drawOK = true;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (drawOK)
{
drawOK = false;
rect = new Rectangle(Xmouse * 3676 / 800, Ymouse * 3676 / 800, (e.X - Xmouse) * 3676 / 800, (e.Y - Ymouse) * 3676 / 800);
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (var g = Graphics.FromImage(pictureBox1.Image))
{
using (Pen myPen = new Pen(Color.Black, 6))
{
g.DrawRectangle(myPen, rect);
}
}
}
【问题讨论】:
标签: graphics bitmap mouseevent mousedown mouseup