【问题标题】:Setting mouse position without System.Windows.Forms在没有 System.Windows.Forms 的情况下设置鼠标位置
【发布时间】:2012-06-19 08:34:08
【问题描述】:

有没有办法在不使用 System.Windows.Forms.Cursor 的情况下操纵鼠标位置?可能是互操作之类的东西?

原因是我们使用了一个专门的 .NET 子集,它不能包含 System.Windows.Forms。

【问题讨论】:

  • 看看这个,这就是这个问题的提问者所做的。 stackoverflow.com/questions/8339565/send-mouse-keyboard-events
  • 是的,类似于互操作。 Pinvoke SetCursorPos().
  • 那是什么样的子集?它是否包括 WPF 或 WinRT 或类似的东西?
  • 被砍掉了。这就是我考虑互操作的原因,这确实是答案。

标签: c# windows mouse


【解决方案1】:

糟糕,阅读问题太快了,这是正确的 PInvoke 调用

[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);

来源:http://www.pinvoke.net/default.aspx/user32.setcursorpos

【讨论】:

【解决方案2】:
private void MoveCursor()
{
   // Set the Current cursor, move the cursor's Position,
   // and set its clipping rectangle to the form. 

   this.Cursor = new Cursor(Cursor.Current.Handle);
   Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
   Cursor.Clip = new Rectangle(this.Location, this.Size);
}

【讨论】:

  • 以下代码示例从当前光标的句柄创建一个光标,更改其位置和剪切矩形。结果是光标将从执行代码时的位置向上和向左移动 50 个像素。此外,光标的剪切矩形被更改为表单的边界(默认情况下它是用户的整个屏幕)。此示例需要一个 Form 和一个 Button 才能在单击时调用此代码。
  • 问题指出 System.Windows.Forms.Cursor 不能使用。
猜你喜欢
  • 1970-01-01
  • 2017-12-25
  • 2011-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-01
  • 1970-01-01
相关资源
最近更新 更多