移动形式只在垂直方向只在、形式、方向

2023-09-03 01:14:48 作者:奥特曼来啦

如何建立一个WinForms形式将由TitleBar中移动只在垂直方向?

How to create a WinForms form which will be moved by TitleBar only vertically?

推荐答案

您必须拦截Windows发送WM_MOVING通知消息。这里的code:

You have to intercept the WM_MOVING notification message that Windows sends. Here's the code:

using System.Runtime.InteropServices;
...
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }
        private struct RECT {
            public int left, top, right, bottom;
        }
        protected override void WndProc(ref Message m) {
            if (m.Msg == 0x216) {  // Trap WM_MOVING
                var rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));
                int w = rc.right - rc.left;
                rc.left = this.Left;
                rc.right = rc.left + w;
                Marshal.StructureToPtr(rc, m.LParam, false);
            }
            base.WndProc(ref m);
        }
    }