当我尝试用它拖动窗口时,窗口会跳跃并闪烁:
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_MOVE
、WM_MOVING
、WM_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/