作者:网际浪子        出处:网络

namespace ClassLibrary.Hardware
{
// 原创 Using C# and Win32API ( 最近我把所有的Win32API看了1遍 很是过瘾 )

public class Mouse
{
 internal const byte SM_MOUSEPRESENT = 19;
 internal const byte SM_CMOUSEBUTTONS = 43;
 internal const byte SM_MOUSEWHEELPRESENT = 75;

 internal struct POINTAPI
 {
  internal int x;
  internal int y;
 }

 internal struct RECT
 {
  internal int left ;
  internal int top ;
  internal int right ;
  internal int bottom ;
 }

 [System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SwapMouseButton")]
 internal extern static int SwapMouseButton ( int bSwap );

 [System.Runtime.InteropServices.DllImport("user32" , EntryPoint="ClipCursor")]
 internal extern static int ClipCursor(ref RECT lpRect);

 [System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint="GetCursorPos" )]
 internal extern static int GetCursorPos( ref POINTAPI lpPoint );

 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="ShowCursor")]
 internal extern static bool ShowCursor ( bool bShow ) ;

 [System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint = "EnableWindow" )]
 internal extern static int EnableWindow( int hwnd , int fEnable );

 [System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetWindowRect")] 
 internal extern static int GetWindowRect( int hwnd , ref RECT lpRect ) ;

 [System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SetCursorPos")] 
 internal extern static int SetCursorPos ( int x , int y ) ;

 [System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetSystemMetrics")]
 internal extern static int GetSystemMetrics( int nIndex );

 [System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SetDoubleClickTime")]
 internal extern static int SetDoubleClickTime ( int wCount );

 [System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetDoubleClickTime")]
 internal extern static int GetDoubleClickTime() ;

 [System.Runtime.InteropServices.DllImport("kernel32.DLL", EntryPoint="Sleep")]
 internal extern static void Sleep ( int dwMilliseconds ) ;

 //得到鼠标相对与全屏的坐标,不是相对与你的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 ) ;
 }
}
}

一个鼠标类( Using C# and Win32API)相关推荐

  1. 设计一个笔记本电脑类,属性随意,并且进行属性私有化,对外提供公开的set和get方法。 设计一个可插拔的接口:InsertDrawable,该接口有什么方法自行定义。

    代码 /* 开放型题目:设计一个笔记本电脑类,属性随意,并且进行属性私有化,对外提供公开的set和get方法.设计一个可插拔的接口:InsertDrawable,该接口有什么方法自行定义.设计一个鼠标 ...

  2. Vaadin介绍与开发练习之二(创建第一个Vaadin类)

    文章目录 默认项目目录结构 开发实例 本篇承接上一篇: Vaadin介绍与开发练习之一 在上一篇中通过Vaadin在线工具产生了一个普通的Servlet项目:my-vaadin,之后将该项目导入Ecl ...

  3. 计算机类和鼠标类是什么关系,UML考试试题及答案7.doc

    UML考试试题及答案7 一.选择题 (共40分,每小题2分) 下面的模型图中,哪个能正确表示"1个教师可以指导0个到多个学生的论文,1个学生必须有1个教师指导其论文" 的意思( ) ...

  4. java如何建Cube类,(JAVA)MyColorCube5(另一个Matrix3D类与面消隐)

    (JAVA)MyColorCube5(另一个Matrix3D类与面消隐) 实现功能:显示一个有颜色的正方体, 拖动鼠标时,正方体绕中心点转动. 虽然实现功能与MyColorCube2相似,但本例在My ...

  5. python做gui好吗_使用Python制作一个铅笔类(Python GUI编程)

    今天,带领大家制作一个铅笔类,实现在界面内自由画图的功能,类似一个铅笔在画布上绘图.通过这个课题,我们可以学习以下内容: Canvas自由绘图的基本思路鼠标事件绑定基本用法如何改变鼠标形状不赘述,先上 ...

  6. 设计一个学生类Student,包括数据成员:姓名、学号、二门课程(面向对象程序设计、高等数学)的成绩。

    (1).设计一个学生类Student,包括数据成员:姓名.学号.二门课程(面向对象程序设计.高等数学)的成绩. (2).创建一个管理学生的类Management,包括实现学生的数据的增加.删除.修改. ...

  7. 设计一个矩形类rectangle_万字长文带你捋清六种设计模式的设计原则(建议收藏)...

    对于设计模式,自己很早之前就看了好多本设计模式书籍,其中一些还看了好几遍,也一直希望自己能在编码的时候把这些设计模式用上去.可是,在日常的打码中,用的最多的就是单例,其次是观察者和建造者模式 ( bu ...

  8. 写一个ArrayList类的动态代理类

    动态代理可以提供对另一个对象的访问,同时隐藏实际对象的具体事实,代理对象对客户隐藏了实际对象.动态代理可以对请求进行其他的一些处理,在不允许直接访问某些类,或需要对访问做一些特殊处理等,这时候可以考虑 ...

  9. vue2 构建一个旅游类WebApp

    前言 从看了慕课上面黄易老师的饿了么课程后,总感觉学了VUE之后要做点什么,趁着热情兴趣还在,自己也用VUE全家桶写了一个旅游类的APP,为什么选择做旅游类的呢??呵呵,我不会告诉你虽然很多地方都没去 ...

最新文章

  1. 华为内部面试题库---(10)
  2. 个人开源作品,即时通讯App支持文本、语音、图片聊天
  3. Android Studio 加载网络图片
  4. python基础面试都问什么问题_基本 Python 面试问题
  5. Python【02】【基础部分】- B
  6. NHibernate3.0剖析:Query篇之NHibernate.Linq标准查询
  7. k8s学习 : 前端是如何连接到后端数据库的?
  8. chrome版本太旧 无法更新
  9. 《生产实习》实习报告——JAVA大数据工程师
  10. HDU-4747 二分+线段树
  11. HTML5 混合APP开发学习笔记(三)——CSS样式设计
  12. 服务器和交换机物理连接_服务器与交换机连接及校园网搭建方案
  13. VC编译选项 /ML /MLd /MT /MTd /MD /MDd之间的区别
  14. python 爬虫与数据可视化
  15. 如何破解PDF文档不能打印?
  16. c语言成绩查询系统大作业,C语言 · 成绩查询系统(示例代码)
  17. 计算机网络摘要怎么写,计算机网络实践论文摘要怎么写 计算机网络实践论文摘要范文参考...
  18. 写日志 Java 蓝桥杯
  19. Hbase入门之系统架构
  20. css flex布局实现文字垂直居中

热门文章

  1. android上传本地图片到服务器上,Android使用post方式上传图片到服务器的方法
  2. 【列表】python编程列表解析
  3. 读后感和机翻《他们在看哪里,为什么看?在复杂的任务中共同推断人类的注意力和意图》
  4. linux查看流量开源,Linux流量监控工具 - iftop
  5. 将输入字符串t中从第m个字符开始的全部字符复制到字符串s中_leetcode第32双周赛第二题leetcode1540. K 次操作转变字符串...
  6. c++ resizereserve
  7. 归一化互相关Normalization cross correlation (NCC)
  8. ATS 5.3.0中利用grep得到纯净的配置文件
  9. 终极Git课程——在虚幻引擎中的应用 The Ultimate Git Course – with Applications in Unreal Engine
  10. Nested Mappings