无论如何,有什么控制可以在哪里移动表格的?
因此,如果我移动表格,则只能在垂直轴上移动,而当我尝试水平移动时,什么也不会发生。
我不希望有位置变更或移动事件之类的错误实现,然后将其内联弹出。我没有一种方法可以使用类似WndProc的替代方法,但是经过一段时间的搜索后,我找不到任何东西。请帮忙
最佳答案
您很可能希望重写WndProc并处理WM_MOVING消息。 According to MSDN:
WM_MOVING消息发送到
用户正在移动的窗口。通过
处理此消息,
应用程序可以监视位置
拖动矩形,如果需要,
改变它的位置。
这将是一种方法,但是,显然您需要根据需要对其进行调整:
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;
}
}
}