pictureBox1.Image = Image.FromFile("01.jpg");在picturebox上如何响应mousehover事件呢,我是想在鼠标停在图片上的时候画一个矩形框。之前用mousemove事件,能正确响应,但是鼠标一停,画的矩形就没了,鼠标移动过程中也一直在闪,用mousehover事件,根本就没反应 救救急吧 private void PictureBox1_MouseHover(object sender, EventArgs e)
{ newPicBox.Invalidate();
Graphics g1 = newPicBox.CreateGraphics(); Pen p1 = new Pen(Color.Red); g1.DrawRectangle(p1, MousePosition.X, MousePosition.Y , 90, 120);
p1.Dispose();
g1.Dispose(); } private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{ if (e.Button == MouseButtons.Left)
{ newPicBox.Invalidate();
Graphics g1 = newPicBox.CreateGraphics(); Pen p1 = new Pen(Color.Red); g1.DrawRectangle(p1, e.X - 90, e.Y - 120, 90, 120);
p1.Dispose();
g1.Dispose();
}
}
{ newPicBox.Invalidate();
Graphics g1 = newPicBox.CreateGraphics(); Pen p1 = new Pen(Color.Red); g1.DrawRectangle(p1, MousePosition.X, MousePosition.Y , 90, 120);
p1.Dispose();
g1.Dispose(); } private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{ if (e.Button == MouseButtons.Left)
{ newPicBox.Invalidate();
Graphics g1 = newPicBox.CreateGraphics(); Pen p1 = new Pen(Color.Red); g1.DrawRectangle(p1, e.X - 90, e.Y - 120, 90, 120);
p1.Dispose();
g1.Dispose();
}
}
2,MousePosition.X, MousePosition.Y是屏幕坐标,不是你newPicBox的坐标
3,你如果用WindowsSDK写过程序,就会明白,Windows中的在显示器上显示内容,只有一个方法(处理WM_PAINT时自绘),所以必须在Paint事件中绘,在Control.CreateGraphics绘图的动作,如果要想正常显示,必须在Paint事件中重现你的绘图动做.所以,任何抛弃Paint事件的绘图,不可能会有出路(设置背景图片,基类也是在OnPaint中处理的)
http://community.csdn.net/Expert/topic/5042/5042878.xml?temp=7.012576E-02
{
newPicBox.Invalidate();
Graphics g1 = e.Graphics; Pen p1 = new Pen(Color.White); g1.DrawRectangle(p1, mouse_x - 90, mouse_y - 120, 90, 120);
} private int mouse_x;
private int mouse_y;
private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{ if (e.Button == MouseButtons.Left)
{
mouse_x = e.X;
mouse_y = e.Y;
}
}
搞定了,在paint事件中写就可以了,谢谢大家,可以结了