簡體   English   中英

啟用/禁用 GUI 控件會禁用我的 OnMouseWheel 覆蓋

[英]Enabling/Disabling GUI controls disables my OnMouseWheel override

在我的項目中,我使用 WS_EX_TRANSPARENT 標志來動態定義半透明表單是否應該接收用戶的鼠標事件。

為了使其更直觀,我添加了代碼以在啟用 WS_EX_TRANSPARENT 時禁用所有可見控件,但是,當調用此代碼時,它似乎“鎖定”了我的OnMouseWheel覆蓋。

下面是我的代碼。 我應該指出,如果我注釋掉 'EnableGUIControls' 方法,這段代碼可以正常工作 - 事實上,如果我注釋掉 'EnableGUIControls' 方法中的任何行,它會完美運行 - 所以它與禁用所有控件有關。

從表單中移除焦點並重新激活它可以解決問題,但手動調用 Form.Activte() 不能。

我正在考慮禁用所有可見控件會以某種方式禁用父級? 有誰知道發生了什么?

        private void SetTransparentToMouse(bool should_be_transparent)
    {
        IntPtr flags = GetWindowLong(this.Handle, GWL_EXSTYLE);

        if (((flags.ToInt64() & WS_EX_TRANSPARENT.ToInt64()) > 0) == should_be_transparent)
        {
            return;
        }
        else
        {
            SwapTransparent();
            EnableGUIControls(!should_be_transparent);
        }
    }

    private void SwapTransparent()
    {
        IntPtr flags = new IntPtr(GetWindowLong(this.Handle, GWL_EXSTYLE).ToInt64() ^ WS_EX_TRANSPARENT.ToInt64());
        SetWindowLong(this.Handle, GWL_EXSTYLE, flags);
    }

            protected override void OnKeyUp(KeyEventArgs e)
    {
        if (!e.Alt)
        {
            SetTransparentToMouse(default_mouse_transparency);
        }

        base.OnKeyUp(e);
    }

            protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
    {
        if (e.Alt)
        {
            SetTransparentToMouse(!default_mouse_transparency);
        }

        base.OnKeyDown(e);
    }

    //Commenting out the call to this, or any line within this resolves the problem!:
        void EnableGUIControls(bool enabled)
    {
        this.Button_Opacity.Enabled = enabled;
        this.Button_Close.Enabled = enabled;
        this.Button_Minimize.Enabled = enabled;
        this.Button_Open.Enabled = enabled;
        this.Button_Pan.Enabled = enabled;
        this.Button_Sizemode.Enabled = enabled;
        this.Button_Zoom.Enabled = enabled;
    }

當所有控件都被禁用時,沒有焦點控件可以捕獲並將鼠標消息重定向到窗體。 嘗試 Form.Focus() 而不是 Form.Activate()。

暫無
暫無

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

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