移动形式只在垂直方向 [英] Move form only vertically

查看:192
本文介绍了移动形式只在垂直方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何建立一个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);
        }
    }

这篇关于移动形式只在垂直方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆