日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

(转)模拟鼠标/键盘

發布時間:2023/12/2 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (转)模拟鼠标/键盘 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鼠標操作類

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

總結

以上是生活随笔為你收集整理的(转)模拟鼠标/键盘的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。