简体   繁体   中英

Losing Mouse Events over PictureBox in c#

I have two panels, one contains a flowpanel containing image thumbnails and the other (which I will call the "imagepanel") contains a picturebox with Docking set to full. Initially, the imagepanel is hidden. When I click on one of the thumbnails on the flowpanel, it hides the flowpanel and displays the imagepanel with the picturebox showing the full image associated with the thumbnail.

The first time I click on the thumbnail, all mouse events for the picturebox work as expected (MouseMove, etc). Right-clicking the picturebox hides the imagepanel and displays the flowpanel.

The next time I click the thumbnail, none of the mousevents for the picturebox are being triggered. When I click on the image, my mouse events once again operate work as expected. I don't want my users to have to click on the picturebox.

I tried using picturebox.Focus() and picturebox.Select() , but neither of these did anything. I also tried to simulate a Left Mouse Click using this link, but it didn't work either:

How to simulate mouse click code example

What should I do to set "focus" on the picturebox, so that the picturebox events are picked up?

======== EDIT ========

My form has a Top/Down splitter panel. The lower panel contains Left/Right splitter panel. The Right panel contains the Flow Panel and the ImagePanel, and the troublesome PictureBox is on the ImagePanel. In the Paint and Click events for the PictureBox, and on the Click event for the thumbnails on the flowpanel, I am writing this.ActiveControl.Name to the console. It always shows the name of the Top/Down splitter panel. Yet, when I add a MouseMove event to the Top/Down splitter panel, it never fires, even though it is consistently the Active Control for the form.

======== ANOTHER EDIT ========

I compiled ways of determining the control from a variety of sources. All of these return the PictureBox as the control with the focus, regardless of when I call the ShowFocus() method below. So apparently it is not a "Focus" problem.

    private void ShowFocus()
    {
        var _C_ = _get_all_controls(this);

        foreach (Control c in _C_)
        {
            if (c.Focused)
                Console.WriteLine(c.Name + " is focused now (ALL)");
        }

        foreach (Control c in this.Controls)
        {
            if (c.Focused)
                Console.WriteLine(c.Name + " is focused now (FORM)");
        }


        Control fc = GetFocusedControl();

        if (fc != null)
            Console.WriteLine("Focused Control: " + fc.Name);
    }

    private IEnumerable<Control> _get_all_controls(Control c)
    {
        return c.Controls.Cast<Control>().SelectMany(item =>
            _get_all_controls(item)).Concat(c.Controls.Cast<Control>()).Where(control =>
            control.Name != string.Empty);
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)]
    internal static extern IntPtr GetFocus();

    public static Control GetFocusedControl()
    {
        Control focusedControl = null;
        // To get hold of the focused control:
        IntPtr focusedHandle = GetFocus();
        if (focusedHandle != IntPtr.Zero)
            // Note that if the focused Control is not a .Net control, then this will return null.
            focusedControl = Control.FromHandle(focusedHandle);
        return focusedControl;
    }

======== EDIT 3 ========

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

来自 Spy++ 的鼠标事件

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:

    [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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM