一个鼠标类( Using C# and Win32API)

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


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 ) ;
 }
}
}

转载于:https://www.cnblogs.com/leonardleonard/archive/2004/10/25/1928765.html

  • 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都可以使用包装器。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值