即使鼠标不移动也会触发 Picturebox mousemove 事件
我正在开发一个 Windows C#、VS 2008 应用程序。我有一个 MDI 容器表单,在运行时单击按钮时可以向其中添加一个新表单。创建子窗体后,我在运行时向其添加一个面板控件和一个图片框控件。
我为图片框的鼠标向上、鼠标向下和鼠标移动添加了鼠标事件处理程序。鼠标向上和向下按预期工作,但当鼠标位于图片框上方且不移动时,鼠标移动事件会持续触发。我知道该事件正在被触发,因为在鼠标移动事件内部,我保留了一个计数器变量,并在每次调用 mousemove 事件时递增该值并将其更新为标签。
为什么会出现这种情况?我正在使用的代码如下。
谢谢
Form frm = new Form();
frm.Deactivate += new EventHandler(MDIChildDeactivate);
PictureBox pi = new PictureBox();
pi.Dock = DockStyle.Fill;
pi.MouseUp += new MouseEventHandler(ImageMouseUp);
pi.MouseDown += new MouseEventHandler(ImageMouseDown);
pi.MouseMove += new MouseEventHandler(ImageMouseMove);
pi.Paint += new PaintEventHandler(CanvasPaint);
pi.KeyDown += new KeyEventHandler(ImageKeyDown);
pi.KeyPress += new KeyPressEventHandler(ImageKeyPress);
/////////////////////////////////////////////////////////
pi.PreviewKeyDown += new PreviewKeyDownEventHandler(pi_PreviewKeyDown);
/////////////////////////////////////////////////////////
if (!IsTabbedMdi)
frm.ClientSize = size;
frm.AutoScroll = true;
pi.Name = ProjectFileName;
Panel pnl = new Panel();
pnl.Dock = DockStyle.None;
pnl.Size = WarpArt.Properties.Resources.GreyCheckerBoard.Size;
pi.Image = WarpArt.Properties.Resources.GreyCheckerBoard;
pnl.AutoScroll = true;
pnl.HorizontalScroll.Visible = true;
pnl.VerticalScroll.Visible = true;
pnl.AutoSizeMode = AutoSizeMode.GrowAndShrink;
pnl.Name = ProjectFileName;
pnl.Controls.Add(pi);
frm.Controls.Add(pnl);
frm.MdiParent = this;
frm.Show();
I am developing a Windows C#, VS 2008 application. I have a MDI Container form to which I add a new form at runtime during the click of a button. When the child form is created I add to it at runtime a panel control and a picture box control to the panel.
I add mouse event handlers for mouse up, mouse down and mouse move for the picturebox. Mouse up and down work as expected, but mouse move event keeps firing continuously when the mouse is over the picturebox and not moving. I know the event is getting fired because inside the mouse move event i keep a counter variable and increment and update the value to a label every time the mousemove event is called.
Why does this happen? Code I am using is below.
Thanks
Form frm = new Form();
frm.Deactivate += new EventHandler(MDIChildDeactivate);
PictureBox pi = new PictureBox();
pi.Dock = DockStyle.Fill;
pi.MouseUp += new MouseEventHandler(ImageMouseUp);
pi.MouseDown += new MouseEventHandler(ImageMouseDown);
pi.MouseMove += new MouseEventHandler(ImageMouseMove);
pi.Paint += new PaintEventHandler(CanvasPaint);
pi.KeyDown += new KeyEventHandler(ImageKeyDown);
pi.KeyPress += new KeyPressEventHandler(ImageKeyPress);
/////////////////////////////////////////////////////////
pi.PreviewKeyDown += new PreviewKeyDownEventHandler(pi_PreviewKeyDown);
/////////////////////////////////////////////////////////
if (!IsTabbedMdi)
frm.ClientSize = size;
frm.AutoScroll = true;
pi.Name = ProjectFileName;
Panel pnl = new Panel();
pnl.Dock = DockStyle.None;
pnl.Size = WarpArt.Properties.Resources.GreyCheckerBoard.Size;
pi.Image = WarpArt.Properties.Resources.GreyCheckerBoard;
pnl.AutoScroll = true;
pnl.HorizontalScroll.Visible = true;
pnl.VerticalScroll.Visible = true;
pnl.AutoSizeMode = AutoSizeMode.GrowAndShrink;
pnl.Name = ProjectFileName;
pnl.Controls.Add(pi);
frm.Controls.Add(pnl);
frm.MdiParent = this;
frm.Show();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我用鼠标在面板上移动图片时,我遇到了类似的问题,它正在摇晃。我使用
Control.MousePosition
而不是MouseEventArgs.Location
。发生这种情况是因为 PictureBox MouseMove 事件触发,即使鼠标本身没有移动,但它位于 PictureBox 上。I had similar problem when moving picture with mouse in panel it was shacking. I used
Control.MousePosition
instead ofMouseEventArgs.Location
. It happens because of picturebox MouseMove event firing even if mouse by it self doesn't move, but it is on PictureBox.有多种方法可以避免这种情况。然而,这可能是最简单的解决方案。很脏但是有用。
解释一下这一点;当调用 MouseMove 事件时,除非按下(在本例中)鼠标左键,否则它不会执行任何操作。只有这样,if语句中的代码才会被执行。
There are multiple ways to circumvent this. However this is probably the easiest solution. Dirty but it works.
To explain this; When the MouseMove event is called it wont do anything unless the (in this case) left mouse button is pressed. Only then the code in the if statement is executed.