开发者

Picturebox mousemove event fired even when mouse not moving

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();


I had similar problem when moving picture with mouse in panel it was shacking. I used Control.MousePosition instead of MouseEventArgs.Location. It happens because of picturebox MouseMove event firing even if mouse by it self doesn't move, but it is on PictureBox.


There are multiple ways to circumvent this. However this is probably the easiest solution. Dirty but it works.

private void MouseMove(object sender, MouseEventArgs e)
        {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                 //dosomething
                }
        }

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜