0

我正在使用无边框表单并使用 HTCAPTION 实现该表单,以便可以拖动它。HTCAPTION 的另一个好处是,当双击时,它可以最小化和最大化。唯一的问题是我想捕获一个事件,以便当窗口最大化时,我可以将按钮的图标从最大化按钮更改为正常模式按钮。关于如何实现这一点的任何好主意?

protected override void WndProc(ref Message m) { base.WndProc(ref m);

        switch(m.Msg)
        {
            case Global.WM_NCHITTEST:
                Point mouseCursor = PointToClient(Cursor.Position);

                if (mouseCursor.X < borderSize && mouseCursor.Y < borderSize)
                    m.Result = (IntPtr)Global.HTTOPLEFT;
                else if (mouseCursor.X < borderSize && mouseCursor.Y > Height - borderSize)
                    m.Result = (IntPtr)Global.HTBOTTOMLEFT;
                else if (mouseCursor.X < borderSize)
                    m.Result = (IntPtr)Global.HTLEFT;
                else if (mouseCursor.X > Width - borderSize && mouseCursor.Y < borderSize)
                    m.Result = (IntPtr)Global.HTTOPRIGHT;
                else if (mouseCursor.X > Width - borderSize && mouseCursor.Y > Height - borderSize)
                    m.Result = (IntPtr)Global.HTBOTTOMRIGHT;
                else if (mouseCursor.X > Width - borderSize)
                    m.Result = (IntPtr)Global.HTRIGHT;
                else if (mouseCursor.Y < borderSize)
                    m.Result = (IntPtr)Global.HTTOP;
                else if (mouseCursor.Y > Height - borderSize)
                    m.Result = (IntPtr)Global.HTBOTTOM;
                else
                    m.Result = (IntPtr)Global.HTCAPTION;
                break;
        }
    }

这是我想在捕获的事件中添加的代码

        if (WindowState == FormWindowState.Maximized)
        {
            WindowState = FormWindowState.Normal;
            pb_max.Image = GomeeSoft.Properties.Resources.buttonmax;
        }
        else
        {
            Screen screen = Screen.FromRectangle(new Rectangle(Left, Top, Width, Height));
            this.MaximumSize = screen.WorkingArea.Size;
            WindowState = FormWindowState.Maximized;
            pb_max.Image = GomeeSoft.Properties.Resources.buttonreturn;
        }
4

1 回答 1

1

双击会生成 WM_SYSCOMMAND 消息。您也可以使用 WndProc 检测到:

    private const int WM_NCHITTEST = 0x84;
    private const int WM_SYSCOMMAND = 0x112;
    private const int SC_MINIMIZE = 0xf020;
    private const int SC_MAXIMIZE = 0xf030;
    private const int SC_RESTORE = 0xf120;

    protected override void WndProc(ref Message m) {
        if (m.Msg == WM_SYSCOMMAND) {
            switch (m.WParam.ToInt32() & 0xfff0) {
                case SC_MINIMIZE: Console.WriteLine("Minimize"); break;
                case SC_MAXIMIZE: Console.WriteLine("Maximize"); break;
                case SC_RESTORE:  Console.WriteLine("Restore");  break;
            }
        }
        base.WndProc(ref m);
        if (m.Msg == WM_NCHITTEST && m.Result == (IntPtr)1) m.Result = (IntPtr)2;
    }
于 2013-01-31T02:09:27.163 回答