【转】C#模拟键盘鼠标事件

1.模拟键盘事件
 
  若要指定与   SHIFT、CTRL   和   ALT   键的任意组合一起使用的键,请在这些键代码之前加上以下一个或多个代码:  

 

看下方法的说明public class SendKeys : System.Object    System.Windows.Forms 的成员

摘要: 提供将键击发送到应用程序的方法。

  public static void Send ( System.String keys )    System.Windows.Forms.SendKeys 的成员

 摘要: 向活动应用程序发送击键。  public static void Sleep ( System.TimeSpan timeout )    System.Threading.Thread 的成员 摘要: 将当前线程阻塞指定的时间。 

public static void Flush (  )   

System.Windows.Forms.SendKeys 的成员

2.模拟鼠标有时,我们需在我们的程序中模拟鼠标的移动、点击等动作。——比如,一个再现用户操作的宏,或者一个演示操作方法的Demo程序。那么,我们在.Net中如何实现呢?.Net并没有提供改变鼠标指针位置、模拟点击操作的函数;但是Windows API提供了。其中一个是:
  
该函数可以改变鼠标指针的位置。其中X,Y是相对于屏幕左上角的绝对位置。另一个函数是:
  

 

这个函数不仅可以设置鼠标指针绝对的位置,而且可以以相对坐标来设置。另外,该函数还可以模拟鼠标左右键点击、鼠标滚轮操作等。其中的MouseEventFlag是一个基于uint类型的枚举,定义如下:
  

 

关于这两个函数的详细说明,可以查看MSDN Library或者Windows的Platform SDK文档。下面的演示程序(完整版源代码,VS.Net 2005/C#)演示了使用上面的函

数,控制鼠标移动到任务栏并点击“开始”按钮的方法。(该程序使用了FindWindowEx等API函数来查找任务栏及开始菜单)

原文地址:http://www.cnblogs.com/eaglet/archive/2009/07/24/1529920.html

本文来自CSDN博客:http://blog.csdn.net/fanweiwei/archive/2009/07/24/4376967.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WinAPI-Wrapper 模拟鼠标点击 用于模拟鼠标移动、点击、窗口操作等的Windows API包装器类。 API 下面是一些可用的方法的总结。有更多的方法和类,比下面列出的要多,但目的是要大致了解包装器能做什么。要查看关于特定方法的详细信息和参数的详细信息,请查看代码本身,因为它的注释很好。 Mouse.cs public static void LeftClick(); public static void RightClick(); public static void MiddleClick(); public static void LeftDown(); public static void LeftUp(); public static void RightDown(); public static void RightUp(); public static void MiddleDown(); public static void MiddleUp(); public static void Move(int x, int y); public static void LeftDrag(Point point1, Point point2, int interval, int lag); Window.cs public static bool DoesExist(string windowTitle); public static IntPtr Get(string windowTitle); public static IntPtr GetFocused(); public static void SetFocused(IntPtr hWnd); public static bool IsFocused(IntPtr hWnd); public static void Move(IntPtr hWnd, int x, int y); public static void Resize(IntPtr hWnd, int width, int height); public static void Hide(IntPtr hWnd); public static void Show(IntPtr hWnd); public static Rectangle GetDimensions(IntPtr hWnd); public static Size GetSize(IntPtr hWnd); public static Point GetLocation(IntPtr hWnd); public static string GetTitle(IntPtr hWnd); public static void SetTitle(IntPtr hWnd, string title); public static void Maximize(IntPtr hWnd); public static void Minimize(IntPtr hWnd); public static void Normalize(IntPtr hWnd); public static Bitmap Screenshot(IntPtr hWnd); public static void RemoveMenu(IntPtr hWnd); public static void Close(IntPtr hWnd); public static void DisableCloseButton(IntPtr hWnd); public static void DisableMaximizeButton(IntPtr hWnd); public static void DisableMinimizeButton(IntPtr hWnd); public static void EnableMouseTransparency(IntPtr hWnd); public static Point ConvertToWindowCoordinates(IntPtr hWnd, int x, int y); public static Point GetCoordinateRelativeToWindow(IntPtr hWnd); Desktop.cs public static Bitmap Screenshot(); public static void HideTaskBar(); public static void ShowTaskBar(); public static int GetWidth(); public static int GetHeight(); 使用 在windows api文件夹中编译代码会产生一个.dll文件。任何引用这个.dll的ccode都可以使用包装器。
C#提供了一个`System.Windows.Forms`命名空间,其中包含了许多与用户界面相关的类及方法,可以用来模拟键盘鼠标操作。 下面是一些常用的模拟键盘鼠标操作的方法: 1. 模拟按键操作 可以使用`SendKeys`类中的`Send`方法来模拟按键操作。例如,要模拟按下“a”键,可以使用以下代码: ``` SendKeys.Send("a"); ``` 2. 模拟组合键操作 如果要模拟组合键操作,例如Ctrl+C复制操作,可以将组合键的按键值用“+”连接起来,如下所示: ``` SendKeys.Send("^c"); ``` 其中,“^”表示Ctrl键,“+”表示Shift键,“%”表示Alt键,“{F1}”表示F1键,等等。 3. 模拟鼠标点击操作 可以使用`System.Windows.Forms.Cursor`类中的`Position`属性来获取当前鼠标的位置,然后使用`System.Windows.Forms.Mouse`类中的`LeftClick`或`RightClick`方法来模拟鼠标左键或右键点击操作。例如,要模拟在屏幕上(100,100)的位置进行左键点击操作,可以使用以下代码: ``` Cursor.Position = new Point(100, 100); Mouse.LeftClick(); ``` 4. 模拟鼠标移动操作 可以使用`System.Windows.Forms.Cursor`类中的`Position`属性来设置鼠标的位置,从而模拟鼠标移动操作。例如,要将鼠标移到屏幕上(200,200)的位置,可以使用以下代码: ``` Cursor.Position = new Point(200, 200); ``` 需要注意的是,模拟键盘鼠标操作可能会对系统产生影响,因此在使用时要谨慎,并且尽量避免在用户不知情的情况下进行操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值