C#模拟window操作鼠标的方法

本文实例讲述了C#模拟window操作鼠标的方法。分享给大家供大家参考。具体实现方法如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace winapi
{
  class Program
  {
    [DllImport("user32.dll", EntryPoint = "mouse_event", SetLastError = true)]
    private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
    const int MOUSEEVENTF_MOVE = 0x0001;  // 移动鼠标
    const int MOUSEEVENTF_LEFTDOWN = 0x0002;// 模拟鼠标左键按下
    const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起
    const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下
    const int MOUSEEVENTF_RIGHTUP = 0x0010;// 模拟鼠标右键抬起
    const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; //模拟鼠标中键按下
    const int MOUSEEVENTF_MIDDLEUP = 0x0040; //模拟鼠标中键抬起
    const int MOUSEEVENTF_ABSOLUTE = 0x8000; //标示是否采用绝对坐标
    static void Main(string[] args)
    {
      // 移动鼠标
      mouse_event(MOUSEEVENTF_MOVE, 400, 0, 0, 0);
      //点击鼠标右键
      mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 410, 0, 0, 0);
      Console.ReadLine();
    }
  }
}

希望本文所述对大家的C#程序设计有所帮助。

(0)

相关推荐

  • C#禁止textbox复制、粘贴、剪切及鼠标右键的方法

    本文实例讲述了C#禁止textbox复制.粘贴.剪切及鼠标右键的方法.分享给大家供大家参考.具体如下: class MyTextBox : System.Windows.Forms.TextBox { protected override void WndProc(ref System.Windows.Forms.Message m) { if (m.Msg != 0x007B && m.Msg != 0x0301 && m.Msg != 0x0302) { base.Wn

  • C#实现鼠标移动到曲线图上显示值的方法

    本文实例讲述了C#实现鼠标移动到曲线图上显示值的方法.分享给大家供大家参考.具体实现方法如下: 一.问题: 完成折线图报表后,产品经理要求把折线上的数值去掉,鼠标经过折线点时显示数值. 二.实现方法: 该方法针对dotnetcharting 下的charting折线图报表使用. 实现思路为,在该窗体上添加一个lable标签,当鼠标经过折线点时获取该点的x轴,y轴值和鼠标坐标值.然后将lable的坐标标记为鼠标所在坐标,并且给lable赋值,并且将lable显示出来.   具体实现代码如下: 复制

  • C# WinForm中Panel实现用鼠标操作滚动条的实例方法

    方法如下:在窗体的Load事件注册滚动事件,并增加对应的方法 复制代码 代码如下: private void FormSample_Load(object sender, EventArgs e)        { //注册事件            this.MouseWheel += new MouseEventHandler(FormSample_MouseWheel);        } /// <summary>        /// 滚动方法        /// </sum

  • C#实现获取鼠标句柄的方法

    本文实例讲述了C#实现获取鼠标句柄的方法,分享给大家供大家参考.具体实现方法如下: 一.调用user32.dll (1)引用 using System.Runtime.InteropServices; (2)调用方法 1.获取窗口标题 [DllImport( "user32.dll" )] public static extern int GetWindowText( IntPtr hWnd, StringBuilder lpString,int nMaxCount ); 注:hWnd

  • 解决C# winForm自定义鼠标样式的两种实现方法详解

    第一种:(调用系统API)首先引入两个命名空间 复制代码 代码如下: using System.Runtime.InteropServices;using System.Reflection; 导入API 复制代码 代码如下: [DllImport("user32.dll")]public static extern IntPtr LoadCursorFromFile(string fileName);[DllImport("user32.dll")]public

  • winform实现限制及解除鼠标移动范围的方法

    本文实例讲述了winform实现限制及解除鼠标移动范围的方法.分享给大家供大家参考.具体如下: 限制鼠标的移动范围: // this.Cursor = new Cursor(this.Cursor.Handle); // Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y); Cursor.Clip = new Rectangle(this.Location, this.Size); 解除限制鼠标的移动范围: Scre

  • C#实现随鼠标移动窗体实例

    本文实例讲述了c#实现随鼠标移动窗体的方法,分享给大家供大家参考. 具体实现方法如下: private void MainForm_Load(object sender, EventArgs e) { //绑定事件 MouseMove += Form_MouseMove; MouseDown += Form_MouseDown; } private Point _mousePoint; private void Form_MouseMove(object sender, MouseEventAr

  • C#中winform实现自动触发鼠标、键盘事件的方法

    程序触发鼠标.键盘事件是C#程序设计中比较常见的功能,本文实例展示了C#中winform实现自动触发鼠标.键盘事件的方法,有不错的实用价值.具体如下: 要想在C#程序中触发鼠标.键盘事件就必须要调用windows函数. 一.鼠标事件的触发 1.引用windows函数mouse_event /// <summary> /// 鼠标事件 /// </summary> /// <param name="flags">事件类型</param> /

  • winform模拟鼠标按键的具体实现

    今天朋友说被他们公司的学习网站恶心到了,下班后要他看学习资料,看完点下一页,而且一页必须停留多少时间才能点击下一页,想不看都不行,于是晚上我突发奇想要给他做一个模拟鼠标按键的程序,可以让鼠标定时间隔触发单击,顺便做下程序最小化到右下角. 首先要引用下user32.dll文件,电脑里就有,C:\Windows\System32搜索下出来了,复制出来放到debug目录下就行. 以下是解决方案代码 复制代码 代码如下: [DllImport("user32.dll", CharSet = C

  • C#实现的鼠标钩子

    C#实现的鼠标钩子,可以获取鼠标在屏幕中的坐标,记得要以管理员权限运行才行 复制代码 代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; u

随机推荐