Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

c# - Move form only vertically

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...