c# - 如何使用 WinApi 随心所欲地 move 窗口

标签 c# winapi move message

当我尝试用它拖动窗口时,窗口会跳跃并闪烁:

protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_MOVE)
    {
        int x = (m.LParam.ToInt32() & 0xffff);
        int y = ((m.LParam.ToInt32() >> 16) & 0xffff);

        if (x < 500)
            Location = new Point(0, y);
        else
            base.WndProc(ref m);
    }
    else
        base.WndProc(ref m);
}
  • 必须停止跳跃
  • WM_MOVEWM_MOVINGWM_WINDOWPOSCHANGING 或其他 move 事件在拖动窗口时必须继续触发,因为我希望检查每个新位置。
  • 另一个问题是 Location = new Point(0, y); 触发另一个 move 事件(应忽略​​此事件)

请帮忙!

最佳答案

下面是使用 WM_WINDOWPOSCHANGING 并修改 WINDOWPOS 结构的示例:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public struct WINDOWPOS
    {
        public IntPtr hwnd;
        public IntPtr hwndInsertAfter;
        public int x;
        public int y;
        public int cx;
        public int cy;
        public uint flags;
    }

    public const int WM_WINDOWPOSCHANGING = 0x46;

    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_WINDOWPOSCHANGING:
                WINDOWPOS wp = (WINDOWPOS)System.Runtime.InteropServices.Marshal.PtrToStructure(m.LParam, typeof(WINDOWPOS));
                if (true) // ... if somecondition ...
                {
                    // modify the location with x,y:
                    wp.x = 0;
                    wp.y = 0;
                }
                System.Runtime.InteropServices.Marshal.StructureToPtr(wp, m.LParam, true);
                break;
        }

        base.WndProc(ref m);
    }

}

关于c# - 如何使用 WinApi 随心所欲地 move 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16963077/

相关文章:

c# - 如何在 Blazor 的 onkeypress 方法中读取输入的当前值?

c# - 通过串口发送数据

c++ - 不使用资源文件的 Windows API 对话框

c++ - 如何使用 Windows API 在菜单栏中包含 DropDownList?

linux - 将 x 个文件 move 到新文件夹

javascript - 使图像随机 move onclick

c# - 如何使用 C# .NET 跨域设置/更改 Active Directory 用户密码?

c# - 找不到 System.ArgumentNullException : Value cannot be null 的修复

c++ - PROCESSENTERY32 结构中的 szExeFile 给出了一个奇怪的值

c++ - 隐藏单个函数背后的 move 语义