【问题标题】:Enabling/Disabling GUI controls disables my OnMouseWheel override启用/禁用 GUI 控件会禁用我的 OnMouseWheel 覆盖
【发布时间】:2011-05-29 15:10:39
【问题描述】:

在我的项目中,我使用 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;
    }

【问题讨论】:

    标签: c# winforms winapi forms controls


    【解决方案1】:

    当所有控件都被禁用时,没有焦点控件可以捕获并将鼠标消息重定向到窗体。试试 Form.Focus() 而不是 Form.Activate()。

    【讨论】:

      猜你喜欢
      • 2021-09-06
      • 2015-09-21
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多