C#窗体控件移动 [英] C# Form Control Move

查看:137
本文介绍了C#窗体控件移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反正是有控制,您可以在移动窗体?

Is there anyway to control where you can move a form?

所以,如果我移动窗体,它只能在垂直轴移动,当我尝试以水平移动,没有任何反应。

So if i move a form, it can only be moved on the vertical axis and when i try to move it horizontally, nothing happens.

我不想要一个越野车实施类似locationchanged或移动事件,坡平回来内联。我不存在使用有点像的WndProc覆盖的方式,但寻找了一段时间后,我不能找到任何东西。请帮助

I dont want a buggy implementation like locationchanged or move event and poping it back inline. I no there is a way using something like a WndProc override but after searching for a while, i couldnt find anything. Please help

推荐答案

您最有可能要重写的WndProc和处理WM_MOVING消息。 根据MSDN

You would most likely want to override WndProc and handle the WM_MOVING message. According to MSDN:

的WM_MOVING消息被发送到该用户正在移动一个
窗口。通过
处理此消息,
应用程序可以监视拖动矩形的位置
,如果需要,
改变其立场。

The WM_MOVING message is sent to a window that the user is moving. By processing this message, an application can monitor the position of the drag rectangle and, if needed, change its position.

这将是一个办法做到这一点,但是,你显然需要tweek它满足您的需要:

This would be a way to do it, however, you would obviously need to tweek it for your needs:

using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace VerticalMovingForm
{
    public partial class Form1 : Form
    {
        private const int WM_MOVING = 0x0216;
        private readonly int positionX;
        private readonly int positionR;

        public Form1()
        {
            Left = 400;
            Width = 500;                            
            positionX = Left;
            positionR = Left + Width;
        }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_MOVING)
            {
                var r = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));
                r.Left = positionX;
                r.Right = positionR;
                Marshal.StructureToPtr(r, m.LParam, false);
            }
            base.WndProc(ref m);                
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }
    }
}

这篇关于C#窗体控件移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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