嘿,我有点傻问题,我需要在一个if语句中将Point Pos设置为鼠标当前位置,在另一个语句中将鼠标光标移动到设置位置。首先,我需要分配全局Point变量,然后Cursor移动到指定的变量,我不希望发生这种情况。 部分来源:
protected override void WndProc(ref Message m)
{
Point Pos = new Point(0, 0);
if (m.Msg == 0x0312)
{
int id = m.WParam.ToInt32();
if (id == 0)
{
Pos.X = MousePosition.X;
Pos.Y = MousePosition.Y;
}
if (id == 1)
{
Cursor.Position = (Pos);
}
}
base.WndProc(ref m);
}
答案 0 :(得分:1)
如果您打算捕获鼠标位置并在以后恢复它,那么您必须使Pos变量成为类的字段而不是方法的局部变量。像这样:
private Point Pos;
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312) // Trap WM_HOTKEY
{
switch (m.WParam.ToInt32()) {
case 0: Pos = Cursor.Position; break;
case 1: Cursor.Position = Pos; break;
}
}
base.WndProc(ref m);
}
答案 1 :(得分:0)
Point pos = this.PointToClient(Cursor.Position);
将返回您的实际鼠标位置。