C# 表单控件 move

标签 c# user-interface move wndproc

有什么方法可以控制表单的 move 位置吗?

所以如果我 move 一个表单,它只能在垂直轴上 move ,当我试图水平 move 它时,什么也没有发生。

我不想要一个有缺陷的实现,比如 locationchanged 或 move 事件,然后将其内联弹出。我没有办法使用类似 WndProc 覆盖的方法,但搜索了一段时间后,我找不到任何东西。请帮忙

最佳答案

例如:

using System.Runtime.InteropServices;

protected override void WndProc(ref Message m)
{
    if (m.Msg == 0x216)  // WM_MOVING = 0x216
    {
        Rectangle rect = 
           (Rectangle) Marshal.PtrToStructure(m.LParam, typeof (Rectangle));
        if (rect.Left < 100)
        {
            // compensates for right side drift
            rect.Width = rect.Width + (100 - rect.Left);
            // force left side to 100
            rect.X = 100;
            Marshal.StructureToPtr(rect, m.LParam, true);
        }
    }
    base.WndProc(ref m);
}

上面的代码将左手位置的最小值设置为 100。

无需像 driis 那样重新创建 RECT 结构,.NET native Rectangle 工作正常。但是,您必须通过 X 属性设置位置,因为 Left 是一个 Get only 属性。

关于C# 表单控件 move ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/932988/

相关文章:

c++ - 具有 move 操作和右值转发的对象生命周期

c# - 我的应用程序设置应该在 .Net Core/EF Core 类库项目中的什么位置?

c# - 当对象与上下文分离时如何删除 EF6 中的对象列表

c# - 从 CSV 执行 SQL INSERT 时如何避免重复数据

api - 面向服务的架构和用户界面

c++ - 为什么不调用 move 构造函数?

c# - 性能 : programmatic vs external compilation

Java 的 setPreferredSize 不会调整 JPanel 的大小

java - 我怎样才能让 JPanel 在窗口滚动时自动更新?

android - 有什么办法可以阻止 android 中键盘出现的标签吗?