程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 一個鼠標類( Using C# and Win32API)

一個鼠標類( Using C# and Win32API)

編輯:關於C#
 

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

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved