question

RichardMartin-3128 avatar image
0 Votes"
RichardMartin-3128 asked RichardMartin-3128 answered

Why do I lose mouse events over a picturebox ?

This is regarding a WinForms application written in c# using .Net 4.7.2.

I have a form that has a Top/Down splitter panel. The lower split panel contains Left/Right splitter panel. The Right split panel contains two panels, only one of which is visible at any given time: one contains a flowpanel containing image thumbnails and the other (which I will call the "imagepanel") contains a picturebox with Docking set to full. Right-clicking on a thumbnail hides the flowpanel and displays the imagepanel with the selected thumbnail shown in full in the picturebox. Right-clicking on the PictureBox hides the imagepanel and displays the flowpanel.

When I right-click on a thumbnail for the very first time after running my application, the MouseMove event for the picturebox fires as expected. I have a test label elsewhere that displays the coordinates of the cursor that demonstrates that the MouseMove event is firing. When I right-click on the Picturebox to go back to the flow panel and click on another thumbnail (or the same thumbnail), the MouseMove event never fires again unless I physically left-click on the picturebox.

I have tried setting Focus to the picturebox:

 pictureBox1.Focus();

I have tried selecting the picturebox:

 pictureBox1.Select();

I have tried simulating a left mouse-click using the sample found on the following page:

Simulating a Mouse Click

I know the simulated mouse click works because when I simulate the left mouse click over a button, it fires the Click event for the button. When I simulate the mouse click over the picturebox, it has no effect.

When I write this.ActiveControl.Name to the console in a variety of places, I always get the name of the Top/Down splitter panel, including the Click event of the PictureBox.

Why am I losing MouseMove events over the picturebox without having to click on it again ? I don't want my users to have to add this extra step.

I have tried creating just a simple application with minimal code to attempt to reproduce the issue, but, of course, I couldn't reproduce the issue.


dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


If you cannot reproduce the problem, then maybe it is caused by other parts of your application. Did you try to make a copy of your project and exclude some of controls, components, features, until the issue disappear?

0 Votes 0 ·
DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered

Hi RichardMartin-3128,
First make sure you subscribe to mousemove event:

 pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove); 
  private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  {
             label1.Text = e.X.ToString();
 }

Since you can't provide code to reproduce the problem, I have a few suggestions you can try.
>>the MouseMove event never fires again unless I physically left-click on the picturebox.
I guess it may be that the image panel loses focus.
So when you right-click the thumbnail to hide the flowchart panel, the image panel gets the focus

 imagepanel.Focus();

You can also try the MouseHover event to call pictureBox1.Focus().

 this.pictureBox1.MouseHover += new System.EventHandler(this.pictureBox1_Hover);
 private void pictureBox1_Hover(object sender, EventArgs e)
 {
             pictureBox1.Focus();
 }

Hope these are useful for you.
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichardMartin-3128 avatar image
0 Votes"
RichardMartin-3128 answered RichardMartin-3128 commented

Thank you for your response.

I subscribe to MouseDown, MouseMove, and MouseUp in the Designer. Do I need to do it again in the main .cs file ?

I didn't think picture boxes or panels could receive focus.

I did try setting focus to the panel containing the picturebox, but this had no effect.

Also, the Hover event doesn't fire unless you pause over a control. If I were going to have my users pause over a control, I may as well have them click on it. It would definitely be faster.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Shown below is the Spy++ output before and after left-clicking the picture box. As it clearly seen, the handle and the mouse events are the same.


98342-image.png


0 Votes 0 ·
image.png (95.5 KiB)
RichardMartin-3128 avatar image
0 Votes"
RichardMartin-3128 answered

I mentioned in my original post that I was simulating a Left Mouse Click, but that it hadn't worked.

When I use two consecutive LeftMouseClicks, it works!

This is the LeftMouseClick code:

         //This is a replacement for Cursor.Position in WinForms
         [System.Runtime.InteropServices.DllImport("user32.dll")]
         static extern bool SetCursorPos(int x, int y);
    
         [System.Runtime.InteropServices.DllImport("user32.dll")]
         public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
    
         public const int MOUSEEVENTF_LEFTDOWN = 0x02;
         public const int MOUSEEVENTF_LEFTUP = 0x04;
    
         //This simulates a left mouse click
         public static void LeftMouseClick(int xpos, int ypos)
         {
             SetCursorPos(xpos, ypos);
             mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
             mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
         }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.