簡體   English   中英

在 c# 中的 PictureBox 上丟失鼠標事件

[英]Losing Mouse Events over PictureBox in c#

我有兩個面板,一個包含一個包含圖像縮略圖的流程面板,另一個(我將其稱為“圖像面板”)包含一個帶有 Docking 設置為完整的圖片框。 最初,圖像面板是隱藏的。 當我單擊流程面板上的其中一個縮略圖時,它會隱藏流程面板並顯示帶有圖片框的圖像面板,該圖片框顯示與縮略圖關聯的完整圖像。

第一次單擊縮略圖時,圖片框的所有鼠標事件都按預期工作(MouseMove 等)。 右鍵單擊圖片框隱藏圖像面板並顯示流程面板。

下次我單擊縮略圖時,不會觸發圖片框的任何鼠標事件。 當我單擊圖像時,我的鼠標事件再次按預期運行。 我不希望我的用戶必須點擊圖片框。

我嘗試使用picturebox.Focus()picturebox.Select() ,但這些都沒有做任何事情。 我還嘗試使用此鏈接模擬鼠標左鍵單擊,但它也不起作用:

如何模擬鼠標點擊代碼示例

我應該怎么做才能在圖片框上設置“焦點”,以便拾取圖片框事件?

========編輯========

我的表單有一個上/下拆分面板。 下面板包含左/右拆分器面板。 Right panel 包含 Flow Panel 和 ImagePanel,麻煩的 PictureBox 在 ImagePanel 上。 在 PictureBox 的 Paint 和 Click 事件以及流程面板上縮略圖的 Click 事件中,我將this.ActiveControl.Name寫入控制台。 始終顯示頂部/向下拆分器面板的名稱。 然而,當我將 MouseMove 事件添加到 Top/Down 拆分器面板時,它永遠不會觸發,即使它始終是表單的 Active Control。

========另一個編輯========

我編譯了從各種來源確定控制的方法。 無論我何時調用下面的 ShowFocus() 方法,所有這些都將 PictureBox 作為具有焦點的控件返回。 所以顯然這不是一個“焦點”問題。

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

========編輯 3 ========

下圖顯示了 Spy++ output 顯示鼠標左鍵單擊圖片框前后的事件。 可以清楚地看到,句柄是一樣的,鼠標事件也是一樣的。

來自 Spy++ 的鼠標事件

我在原始帖子中提到我正在模擬鼠標左鍵單擊,但它沒有奏效。

當我使用兩個連續的 LeftMouseClicks 時,它起作用了!

這是 LeftMouseClick 代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM