0

如何在通过拖动标题栏移动表单时将 winform 的不透明度设置为 50%,并在鼠标左键打开后将其不透明度重置为 100%。

4

3 回答 3

3

有趣的是,您也可以在 OnResizeBegin 和 OnResizeEnd 覆盖中执行此操作——这将适用于移动和调整表单大小。

如果您只想在移动时更改不透明度,而不是在调整大小时更改,那么 alex 的答案会更好。

于 2009-07-29T14:47:25.470 回答
2

这是一个代码示例:

    public partial class Form1 : System.Windows.Forms.Form
{
    private const long BUTTON_DOWN_CODE = 0xa1;
    private const long BUTTON_UP_CODE = 0xa0;
    private const long WM_MOVING = 0x216;

    static bool left_button_down = false;

    protected override void DefWndProc(ref System.Windows.Forms.Message m)
    {
        //Check the state of the Left Mouse Button
        if ((long)m.Msg == BUTTON_DOWN_CODE)
            left_button_down = true;
        else if ((long)m.Msg == BUTTON_UP_CODE)
            left_button_down = false;

        if (left_button_down)
        {
            if ((long)m.Msg == WM_MOVING)
            {
                //Set the forms opacity to 50% if user is moving
                if (this.Opacity != 0.5)
                    this.Opacity = 0.5;
            }
        }

        else if (!left_button_down)
            if (this.Opacity != 1.0)
                this.Opacity = 1.0;

        base.DefWndProc(ref m);
    }
}
于 2009-07-29T14:46:40.760 回答
2

Form.Opacity设置为 0.5 以响应表单的 WndProc 中的WM_NCLBUTTONDOWN

然后在收到WM_NCLBUTTONUP时将 Opacity 设置为 1.0 。

于 2009-07-29T14:40:13.643 回答