How do I set the position of the mouse cursor from a Console app in C#?

自作多情 提交于 2019-12-07 03:38:43

问题


I've found many articles on how to set the mouse position in a C# windows forms project - I want to do this in a console application. How can I set the absolute mouse position from a C# windows console application?

Thanks!

Hint: it's not Console.setCursorPosition, that only sets the position of the text cursor in the console.


回答1:


Inside your console application, add a reference to System.Windows.Forms.dll and use the other techniques you've read about. The choice of console vs windows exe only impacts the PE header (and maybe the default code template, but you can hack that trivially); you can still use the full framework in a console exe.

The mouse you want to control is in windows, not the console.




回答2:


This is an old thread, but for the sake of completion it can be done this way...

use System.Runtime.InteropServices;

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

then in method whatever position you wish e.g.

  SetCursorPos(500, 500);



回答3:


You can simply assign to Cursor.Position.

However, in a console application you will need to add references to the WinForms assemblies because console application projects do not include references to WinForms by default.

You will need to add System.Windows.Forms and System.Drawing, the latter to gain access to the Point class.



来源:https://stackoverflow.com/questions/6716275/how-do-i-set-the-position-of-the-mouse-cursor-from-a-console-app-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!