(转)模拟鼠标/键盘

鼠标操作类

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/361998.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

c++ 返回 char*

一段在C里经常犯错误的代码 一个类&#xff1a; class C{public:C(){}~C(){}public:string a;string funa(){string tmp "1234";return tmp;}};外部调用类C并使用其成员&#xff1a; C classc;char *test1 classc.a.c_str();printf("%s\n", test1);上述正…

JSF的工作方式和调试方式–可以使用polyglot吗?

JSF不是我们通常认为的那样。 这也是一个调试起来可能有些棘手的框架&#xff0c;尤其是在初次遇到时。 在这篇文章中&#xff0c;让我们继续探讨为什么会出现这种情况&#xff0c;并提供一些JSF调试技术。 我们将讨论以下主题&#xff1a; JSF不是我们经常想到的 JSF调试的难…

React组件实现越级传递属性

如果有这样一个结构&#xff1a;三级嵌套&#xff0c;分别是&#xff1a;一级父组件、二级子组件、三级孙子组件&#xff0c;且前者包含后者&#xff0c;结构如图&#xff1a; 如果把一个属性&#xff0c;比如color&#xff0c;从一级传递给三级&#xff0c;一般做法是使用prop…

尝试Office 2003 VSTO的开发、部署

背景&#xff1a;一年前&#xff0c;某项目需要使用到Excel进行数据录入&#xff0c;考虑到很多用户还是使用XPOffice 2003&#xff0c;所以开发的时候直接使用Excel 2003版本进行VBA开发。也许很多人都会说&#xff0c;Win10都出了&#xff0c;微软的Office都要免费了&#xf…

阅读react-redux源码(五) - connectAdvanced中store改变的事件转发、ref的处理和pure模式的处理

阅读react-redux源码 - 零阅读react-redux源码 - 一阅读react-redux源码(二) - createConnect、match函数的实现阅读react-redux源码(三) - mapStateToPropsFactories、mapDispatchToPropsFactories和mergePropsFactories阅读react-redux源码(四) - connectAdvanced、wrapWithC…

Servlet编程API

一、基本的servlet APIJavaEE关于Servlet的API主要有两个包&#xff1a;javax.servlet和javax.servlet.http。前者主要提供了Web容器能够使用的servlet基本类和接口&#xff0c;后者主要包括和HTTP协议相关的servlet类和接口。对servlet的编程&#xff0c;主要是根据需要&#…

初级开发人员在编写单元测试时常犯的错误

自从我编写第一个单元测试以来已经有10年了。 从那时起&#xff0c;我不记得我已经编写了成千上万的单元测试。 老实说&#xff0c;我在源代码和测试代码之间没有任何区别。 对我来说是同一回事。 测试代码是源代码的一部分。 在过去的3-4年中&#xff0c;我与多个开发团队合作…

OpenDaylight开发hello-world项目之开发工具安装

OpenDaylight开发hello-world项目之开发环境搭建 OpenDaylight开发hello-world项目之开发工具安装 OpenDaylight开发hello-world项目之代码框架搭建 在ODL开发之前&#xff0c;要安装好开发环境。ODL使用java语言开发&#xff0c;所以要安装好java。ODL的代码框架是有maven这个…

Google Chrome 扩展程序开发

根据公司的规定&#xff0c;每月八小时&#xff0c;弹性工作制。所以大家平时来的不太准时&#xff0c;如果有事&#xff0c;下班也就早些回去了。所以一个月下来工作时间可能不够&#xff0c;但是公司的考勤日历是这样的&#xff1a; 除了请假和法定节假日外&#xff0c;其他样…

阅读react-redux源码(六) - selectorFactory处理store更新

阅读react-redux源码 - 零阅读react-redux源码 - 一阅读react-redux源码(二) - createConnect、match函数的实现阅读react-redux源码(三) - mapStateToPropsFactories、mapDispatchToPropsFactories和mergePropsFactories阅读react-redux源码(四) - connectAdvanced、wrapWithC…

[Silverlight入门系列]使用MVVM模式(7):ViewModel的INotifyPropertyChanged接口实现

本文说说ViewModel的这个INotifyPropertyChanged接口可以用来做啥&#xff1f; 举例1&#xff1a;我有个TabControl&#xff0c;里面放了很多View&#xff0c;每个由ViewModel控制&#xff0c;我想是想TabSelectionChanged就打开相应的ViewModel&#xff0c;怎么做&#xff1f;…

无状态Spring安全性第1部分:无状态CSRF保护

如今&#xff0c;随着RESTful架构变得越来越标准&#xff0c;可能值得花一些时间重新考虑当前的安全方法。 在这个小系列的博客文章中&#xff0c;我们将探索一些以无状态方式解决与Web相关的安全问题的相对较新的方法。 这第一篇文章是关于保护您的网站免受跨站请求伪造&#…

window.Event参数详解

原文地址&#xff1a;window.Event参数详解作者&#xff1a;cz0090704window.evet 说明 event代表事件的状态&#xff0c;例如触发event对象的元素、鼠标的位置及状态、按下的键等等。 event对象只在事件发生的过程中才有效。 event的某些属性只对特定的事件有意义。比如&…

微信群运营之设计运营思路

商家要想运营好微信群&#xff0c;那么首要做的工作就是设计运营思路。如果做事毫无章法思路&#xff0c;那么很有可能会让自己的工作陷入僵局。运营微信群并不简单&#xff0c;需要考虑多方面社群鸭因素。卖什么产品&#xff0c;群管理体系的设立&#xff0c;规则的制定&#…

阅读react-redux源码(七) - 实现一个react-redux

阅读react-redux源码 - 零阅读react-redux源码 - 一阅读react-redux源码(二) - createConnect、match函数的实现阅读react-redux源码(三) - mapStateToPropsFactories、mapDispatchToPropsFactories和mergePropsFactories阅读react-redux源码(四) - connectAdvanced、wrapWithC…

[读书笔记]TCP/IP详解V1读书笔记-4 5

IP地址与以太网地址之间的关系 R P发送一份称作A R P请求的以太网数据帧给以太网上的每个主机。这个过程称作广播&#xff0c;在32 bit的I P地址和采用不同网络技术的硬件地址之间提供动态映射 ----------------------------------------- arp以太网帧的类型字段为x 0 8 0 6&am…

未来是Apache Karaf上的微服务架构

这是Jamie Goodyear的客座博客文章&#xff08; 博客 &#xff0c; icbts &#xff09;。 他是Savoir Technologies的开源倡导者&#xff0c;Apache开发人员和计算机系统分析师&#xff1b; 他为全球大型组织设计&#xff0c;批判和支持了体系结构。 他拥有纽芬兰纪念大学的计…

springcloud微服务多节点高性能、高可用、高并发部署

1. 共有三个服务 discovery服务&#xff0c;domain服务&#xff0c;gateway服务。 discovery服务是用来注册其他服务的&#xff0c;作为服务治理用。 domain服务是主业务服务。 gateway服务是所有服务的一个入口&#xff0c;用来做一些服务的判断和过滤用。 2. 有三台机器分别为…

只能是数字、字母、-和_

在文本框的keypress事件调用下面函数。 如 <input disabled"disabled" type"text" iduserNameToEdit οnkeypress"TextValidate()" /> 如果在文本框中按下特殊字符键&#xff0c;则显示警告信息&#xff0c;或者输入框不接受非法输入。 …

代码风格之Prettier简介

多人协作中统一的代码风格有利于项目的发展这是共识&#xff0c;但是采用什么标准来统一代码这选择就相对纷杂。项目刚开始使用了ESLint来规范代码&#xff0c;但是ESLint默认是支持JavaScript&#xff0c;加上配置可以支持TypeScript&#xff0c;而样式的支持则需要再配置Styl…