鼠标操作类

using System;

namespace Edobnet.Net.Lib

{

/// <summary>

/// Mouse 的摘要说明。

/// </summary>

public class Mouse

{

public Mouse()

{

//

// TODO: 在此处添加构造函数逻辑

//

}

internal const byte SM_MOUSEPRESENT = 19;

        internal const byte SM_CMOUSEBUTTONS = 43;

        internal const byte SM_MOUSEWHEELPRESENT = 75;

public const int MOUSEEVENTF_LEFTDOWN = 0x2;

public const int MOUSEEVENTF_LEFTUP = 0x4;

public const int MOUSEEVENTF_MIDDLEDOWN = 0x20;

public const int MOUSEEVENTF_MIDDLEUP = 0x40;

public const int MOUSEEVENTF_MOVE = 0x1;

public const int MOUSEEVENTF_RIGHTDOWN = 0x8;

public const int MOUSEEVENTF_RIGHTUP = 0x10;

        public struct POINTAPI

        {

        public int x;

        public int y;

        }

        public struct RECT

        {

        public int left ;

        public int top ;

        public int right ;

        public int bottom ;

        }

        [System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SwapMouseButton")]

        public extern static int SwapMouseButton ( int bSwap );

        [System.Runtime.InteropServices.DllImport("user32" , EntryPoint="ClipCursor")]

        public extern static int ClipCursor(ref RECT lpRect);

        [System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint="GetCursorPos" )]

        public extern static int GetCursorPos( ref POINTAPI lpPoint );

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="ShowCursor")]

        public extern static bool ShowCursor ( bool bShow ) ;

        [System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint = "EnableWindow" )]

        public extern static int EnableWindow( int hwnd , int fEnable );

        [System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetWindowRect")] 

        public extern static int GetWindowRect( int hwnd , ref RECT lpRect ) ;

        [System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SetCursorPos")] 

        public extern static int SetCursorPos ( int x , int y ) ;

        [System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetSystemMetrics")]

        public extern static int GetSystemMetrics( int nIndex );

        [System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SetDoubleClickTime")]

        public extern static int SetDoubleClickTime ( int wCount );

        [System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetDoubleClickTime")]

        public extern static int GetDoubleClickTime() ;

        [System.Runtime.InteropServices.DllImport("kernel32.DLL", EntryPoint="Sleep")]

        public extern static void Sleep ( int dwMilliseconds ) ;

[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="mouse_event")]

public static extern void mouse_event (

int dwFlags,

int dx,

int dy,

int cButtons,

int dwExtraInfo

);

        //得到鼠标相对与全屏的坐标,不是相对与你的Form的,且与你的分辨率有关系

        public static int FullScreenPosition_X

        {

        get

        {

       POINTAPI _POINTAPI = new POINTAPI();

       GetCursorPos ( ref _POINTAPI );

  

       return _POINTAPI.x;

        }

        }

 

        public static int FullScreenPosition_Y

        {

        get

        {

       POINTAPI _POINTAPI = new POINTAPI();

       GetCursorPos ( ref _POINTAPI );

  

       return _POINTAPI.y;

        }

        }

        // 隐藏 显示 鼠标

        public static void Hide()

        {

        ShowCursor( false ) ;

        }

 

        public static void Show()

        {

        ShowCursor( true ) ;

        }

        // 将鼠标锁定在你的Form里 不过你得将你的Form先锁了,Form Resize 就失效了

        public static void Lock( System.Windows.Forms.Form ObjectForm )

        {

        RECT _FormRect = new RECT ();

 

        GetWindowRect( ObjectForm.Handle.ToInt32() , ref _FormRect );

 

        ClipCursor( ref _FormRect );

        }

 

        public static void UnLock()

        {

        RECT _ScreenRect = new RECT ();

 

        _ScreenRect.top = 0;

        _ScreenRect.left = 0;

        _ScreenRect.bottom = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom;

        _ScreenRect.right = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right;

  

        ClipCursor( ref _ScreenRect );

        }

        // 鼠标失效,不过失效的好像不只是鼠标,小心哦

        public static void Disable( System.Windows.Forms.Form ObjectForm )

        {

        EnableWindow( ObjectForm.Handle.ToInt32() , 0 ) ;

        }

        public static void Enable( System.Windows.Forms.Form ObjectForm )

        {

        EnableWindow( ObjectForm.Handle.ToInt32() , 1 ) ;

        }

        // 鼠标自己移动 很想动画哦 参数是2个控件的handle

        // 看这个方法前,先用凉水擦把脸。。。 反正我写的时候 头晕

        public static void Move ( int From_Handle_ToInt32 , int To_Handle_ToInt32 )

        {

        RECT rectFrom = new RECT () ;

        RECT rectTo = new RECT () ;

  

        int i ;

 

        GetWindowRect( From_Handle_ToInt32 , ref rectFrom ) ;

        GetWindowRect( To_Handle_ToInt32 , ref rectTo ) ;

        if ( ( rectFrom.left + rectFrom.right ) / 2 - ( rectTo.left + rectTo.right ) / 2 > 0 )

        {

       for ( i = ( rectFrom.left + rectFrom.right ) / 2 ; i >= ( rectTo.left + rectTo.right ) / 2 ; i-- )

       {

       SetCursorPos ( i , ( rectFrom.top + rectFrom.bottom ) / 2) ;

       Sleep ( 1 ) ;

       }

        }

        else

        {

       for ( i = ( rectFrom.left + rectFrom.right ) / 2 ; i <= ( rectTo.left + rectTo.right ) / 2 ; i++ )

       {

       SetCursorPos ( i , ( rectFrom.top + rectFrom.bottom ) / 2) ;

       Sleep ( 1 ) ;

       }

        }

        if ( ( rectFrom.top + rectFrom.bottom ) / 2 - ( rectTo.top + rectTo.bottom ) / 2 > 0 )

        {

       for ( i = ( rectFrom.top + rectFrom.bottom ) / 2 ; i >= ( rectTo.top + rectTo.bottom ) / 2 ; i-- )

       {

       SetCursorPos ( ( rectTo.left + rectTo.right ) / 2 , i ) ;

       Sleep ( 1 ) ;

       }

        }

        else

        {

       for ( i = ( rectFrom.top + rectFrom.bottom ) / 2 ; i <= ( rectTo.top + rectTo.bottom ) / 2 ; i++ )

       {

       SetCursorPos ( ( rectTo.left + rectTo.right ) / 2 , i ) ;

       Sleep ( 1 ) ;

       }

        }

        }

 

        // 得到你的鼠标类型

        public static string Type

        {

        get

        {

       if ( GetSystemMetrics( SM_MOUSEPRESENT ) == 0 )

       {

       return "本计算机尚未安装鼠标" ;

       }

       else

       {

       if ( GetSystemMetrics( SM_MOUSEWHEELPRESENT ) != 0 )

       {

           return GetSystemMetrics( SM_CMOUSEBUTTONS ) + "键滚轮鼠标" ;

       }

       else

       {

           return GetSystemMetrics( SM_CMOUSEBUTTONS ) + "键鼠标" ;

       }

       }

        }

        }

        // 设置鼠标双击时间

 

        public static void DoubleClickTime_Set( int MouseDoubleClickTime )

        {

        SetDoubleClickTime( MouseDoubleClickTime );

        }

 

        public static string DoubleClickTime_Get()

        {

        return GetDoubleClickTime().ToString() ;

        }

        // 设置鼠标默认主键 我是没有见过谁左手用鼠标

        public static void DefaultRightButton()

        {

        SwapMouseButton ( 1 ) ;

        }

 

        public static void DefaultLeftButton()

        {

        SwapMouseButton ( 0 ) ;

        }

private static void LeftDown()

{

mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);

}

private static void LeftUp()

{

mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);

}

public static void LeftClick()

{

LeftDown();

LeftUp();

}

private static void MiddleDown()

{

mouse_event(MOUSEEVENTF_MIDDLEDOWN,0,0,0,0);

}

private static void MiddleUp()

{

mouse_event(MOUSEEVENTF_MIDDLEUP,0,0,0,0);

}

public static void MiddleClick()

{

MiddleDown();

MiddleUp();

}

private static void RightDown()

{

mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,0);

}

private static void RightUp()

{

mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,0);

}

public static void RightClick()

{

RightDown();

RightUp();

}

}

}

调用:

Mouse.SetCursorPos(100,100);

Mouse.LeftClick();//左键
 Mouse.RightClick();//右键

键盘操作可以用:
 SendKeys.SendWait();
Msn帮助:ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemWindowsFormsSendKeysClassTopic.htm

显示器一些操作:

using System;

using System.Runtime.InteropServices;

namespace Edobnet.Net.Lib

{

/// <summary>

/// Screen 的摘要说明。

/// </summary>

public class Screen

{

public enum DMDO

{

DEFAULT = 0,

D90 = 1,

D180 = 2,

D270 = 3

}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]

struct DEVMODE

{

public const int DM_DISPLAYFREQUENCY = 0x400000;

public const int DM_PELSWIDTH = 0x80000;

public const int DM_PELSHEIGHT = 0x100000;

private const int CCHDEVICENAME = 32;

private const int CCHFORMNAME = 32;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst=CCHDEVICENAME)]

public string dmDeviceName;

public short dmSpecVersion;

public short dmDriverVersion;

public short dmSize;

public short dmDriverExtra;

public int dmFields;

public int dmPositionX;

public int dmPositionY;

public DMDO dmDisplayOrientation;

public int dmDisplayFixedOutput;

public short dmColor;

public short dmDuplex;

public short dmYResolution;

public short dmTTOption;

public short dmCollate;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst=CCHFORMNAME)]

public string dmFormName;

public short dmLogPixels;

public int dmBitsPerPel;

public int dmPelsWidth;

public int dmPelsHeight;

public int dmDisplayFlags;

public int dmDisplayFrequency;

public int dmICMMethod;

public int dmICMIntent;

public int dmMediaType;

public int dmDitherType;

public int dmReserved1;

public int dmReserved2;

public int dmPanningWidth;

public int dmPanningHeight;

}

[DllImport("user32.dll", CharSet=CharSet.Auto)]

//static extern int ChangeDisplaySettings( DEVMODE lpDevMode,  int dwFlags);

static extern int ChangeDisplaySettings( [In] ref DEVMODE lpDevMode,  int dwFlags);

public Screen()

{

//

// TODO: 在此处添加构造函数逻辑

//

}

public static void changeScree(int W,int H,int F,out long RetVal)

{

RetVal=0;

DEVMODE dm = new DEVMODE();

dm.dmSize= (short)Marshal.SizeOf(typeof(DEVMODE));

dm.dmPelsWidth = W;

dm.dmPelsHeight= H;

dm.dmDisplayFrequency=F;

dm.dmFields = DEVMODE.DM_PELSWIDTH | DEVMODE.DM_PELSHEIGHT | DEVMODE.DM_DISPLAYFREQUENCY;

RetVal = ChangeDisplaySettings(ref dm, 0);

}

}

}

消息控制:

using System;

namespace Edobnet.Net.Lib

{

/// <summary>

/// Window 的摘要说明。

/// </summary>

public class Windows

{

public Windows()

{

//

// TODO: 在此处添加构造函数逻辑

//

}

[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="SendMessage")]

public static extern int SendMessage (

int hwnd,

int wMsg,

int wParam,

int lParam

);

}

}

如:关闭显示器可以用:

Windows.SendMessage(this.Handle.ToInt32(),0x112,0xF170,2);

打开

Windows.SendMessage(this.Handle.ToInt32(),0x112,0xF170,-1);

DC的一些操作

using System;

using System.Runtime.InteropServices;

namespace Edobnet.Net.Lib

{

/// <summary>

/// DC 的摘要说明。

/// </summary>

public class DC

{

public DC()

{

//

// TODO: 在此处添加构造函数逻辑

//

}

[DllImport("gdi32.dll", EntryPoint="CreateDC")]

public static extern int CreateDC (

string lpDriverName,

string lpDeviceName,

string lpOutput,

IntPtr lpInitData

);

[DllImport("gdi32.dll", EntryPoint="Rectangle")]

public static extern int Rectangle (

int hdc,

int X1,

int Y1,

int X2,

int Y2

);

[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]

private static extern bool BitBlt(

IntPtr hdcDest, //目标设备的句柄

int nXDest,//目标对象的左上角的X坐标

int nYDest,//目标对象的左上角的X坐标

int nWidth,//目标对象的矩形的宽度

int nHeight,//目标对象的矩形的长度

IntPtr hdcSrc,//源设备的句柄

int nXSrc,//源对象的左上角的X坐标

int nYSrc,//源对象的左上角的X坐标

System.Int32 dwRop//光栅的操作值

);

}

}

大家一些研究:edobnet@163.com
c++版本的控制也有,只是对鼠标操作还没有研究!

转载于:https://www.cnblogs.com/domainblogs/archive/2009/01/19/1378572.html

(转)模拟鼠标/键盘相关推荐

  1. Delphi下利用WinIo模拟鼠标键盘详解

    本文最早在编程论坛上发表,文章地址:http://programbbs.com/bbs/view12-17207-1.htm,相关文件可以在上述地址的页面中下载.转载时请注明出处. 前言 一日发现Se ...

  2. C#模拟鼠标键盘控制其他窗口(一)

    编写程序模拟鼠标和键盘操作可以方便的实现你需要的功能,而不需要对方程序为你开放接口.比如,操作飞信定时发送短信等.我之前开发过飞信耗子,用的是对飞信协议进行抓包,然后分析协议,进而模拟协议的执行,开发 ...

  3. Linux 模拟 鼠标 键盘 事件

    /************************************************************************* Linux 模拟 鼠标 键盘 事件* 说明:* 以 ...

  4. python 模拟鼠标键盘_python之模拟鼠标键盘动作

    上个月就打算开发个还算好玩的项目,但是一直没时间.这篇是此项目用到的一部分, 处理好此部分基本还差通信等方面的了.首先模拟鼠标键盘按下释放的动作,本人利用X11 这个库,所以要了解X11编程;其次,本 ...

  5. C# 模拟 鼠标 键盘操作

    C# 模拟 鼠标 键盘操作 转载于:https://www.cnblogs.com/macT/p/10208222.html

  6. python模拟鼠标键盘原理_Python-模拟鼠标键盘动作

    引 在使用电脑的时候,偶尔有需求要模拟鼠标键盘,进行一些机械重复的操作(刷新网页.抢票.某些小游戏等).如果为此专门下载一个按键精灵,总感觉杀鸡用牛刀,所以就开始探索一些轻量级解决方案.本人电脑上配置 ...

  7. python截图工具和模拟鼠标键盘_python PyAutoGUI 模拟鼠标键盘操作和截屏功能

    简介 一款跨平台/无依赖的自动化测试工具,目测只能控制鼠标/键盘/获取屏幕尺寸/弹出消息框/截屏. 安装 pip install pyautogui 鼠标键盘控制 >>> impor ...

  8. 模拟鼠标键盘操作,含硬件模拟技术。[转]

    模拟鼠标键盘操作,含硬件模拟技术. 键盘是我们使用计算机的一个很重要的输入设备了,即使在鼠标大行其道的今天,很多程序依然离不开键盘来操作.但是有时候,一些重复性的,很繁琐的键盘操作总会让人疲惫,于是就 ...

  9. PyMouse模拟鼠标键盘操作

    PyMouse模拟鼠标键盘操作 使用PyMouse实现模拟鼠标键盘操作 -----解决方案1 1)安装[pyMouse]( https://pypi.org/project/PyMouse/) 2)安 ...

  10. Python模拟鼠标键盘:pykeyboard库的使用

    目录 1.下载安装:pyHook库.PyWin32 2.安装:pykeyboard库 3.模拟鼠标键盘操作 3.1 鼠标操作 3.2 键盘操作 按键属性 4.输入中文 1.下载安装:pyHook库.P ...

最新文章

  1. Linux     grep命令,正则表达式
  2. 学习 jQuery下拉框,单选框,多选框整理
  3. 深度学习笔记:手写一个单隐层的神经网络
  4. XAML 编码规范 (思考)
  5. java excel 插件开发工具_程序员常用的15 种开发者工具推荐
  6. Bootstrap3 滚动监听插件的调用方式
  7. 中移M5311-NBIOT-OPENCPU开发-HTTP串口收发器
  8. 买断式软件逐渐向订阅式软件发展,是不是资本想一直割韭菜?
  9. |Vijos|NOIP2015|模拟|P1975 扫雷游戏
  10. python生成json文件_json文件生成by python
  11. LabVIEW编程LabVIEW控制研华PCI-1739U例程与相关资料
  12. gif动图怎么制作?手把手教你视频转gif动图
  13. MYSQLg高级------聚簇索引和非聚簇索引
  14. 看3D打印技术如何影响未来
  15. disk-磁盘检测工具(二合一)
  16. python处理字体(动态字体库)
  17. 软件工程课程周学习进度报告——第四周
  18. Excel将一个sheet页拆分多个
  19. clearcase命令收集
  20. 水滴公司欲赴美IPO,中国保险科技第一股含金量几何

热门文章

  1. mysql隔离级别验证_MySQL事务隔离级别以及验证
  2. Spark学习笔记(7)---Spark SQL学习笔记
  3. AFNetworking到底做了什么?
  4. HTML5基础一:常用布局标签
  5. eclipse中java.lang.OutOfMemoryError: Java heap space错误
  6. python seed()
  7. 用RAII技术管理资源及其泛型实现
  8. linux7设备的挂载,centos7磁盘分区与挂载解析
  9. linux bin目录误删,Linux下误删 /user/bin目录后的补救
  10. juyter显示决策树图形_决策树分析细分市场