《C#限制鼠标移动范围.doc》由会员分享,可在线阅读,更多相关《C#限制鼠标移动范围.doc(12页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、1一个鼠标类#region一个鼠标类2/*/3/MouseControl4/MadebyMichael5/date2008-01-306/7classMouse89internalconstbyteSM_MOUSEPRESENT=19;10internalconstbyteSM_CMOUSEBUTTONS=43;11internalconstbyteSM_MOUSEWHEELPRESENT=75;1213internalstructPOINTAPI1415internalintx;16internalinty;171819internalstructRECT2021internalintlef
2、t;22internalinttop;23internalintright;24internalintbottom;252627System.Runtime.InteropServices.DllImport(user32.dll,EntryPoint=SwapMouseButton)28internalexternstaticintSwapMouseButton(intbSwap);2930System.Runtime.InteropServices.DllImport(user32,EntryPoint=ClipCursor)31internalexternstaticintClipCur
3、sor(refRECTlpRect);3233System.Runtime.InteropServices.DllImport(user32.dll,EntryPoint=GetCursorPos)34internalexternstaticintGetCursorPos(refPOINTAPIlpPoint);3536System.Runtime.InteropServices.DllImport(user32.dll,EntryPoint=ShowCursor)37internalexternstaticboolShowCursor(boolbShow);3839System.Runtim
4、e.InteropServices.DllImport(user32.dll,EntryPoint=EnableWindow)40internalexternstaticintEnableWindow(inthwnd,intfEnable);4142System.Runtime.InteropServices.DllImport(user32.dll,EntryPoint=GetWindowRect)43internalexternstaticintGetWindowRect(inthwnd,refRECTlpRect);4445System.Runtime.InteropServices.D
5、llImport(user32.dll,EntryPoint=SetCursorPos)46internalexternstaticintSetCursorPos(intx,inty);4748System.Runtime.InteropServices.DllImport(user32.dll,EntryPoint=GetSystemMetrics)49internalexternstaticintGetSystemMetrics(intnIndex);5051System.Runtime.InteropServices.DllImport(user32.dll,EntryPoint=Set
6、DoubleClickTime)52internalexternstaticintSetDoubleClickTime(intwCount);5354System.Runtime.InteropServices.DllImport(user32.dll,EntryPoint=GetDoubleClickTime)55internalexternstaticintGetDoubleClickTime();5657System.Runtime.InteropServices.DllImport(kernel32.DLL,EntryPoint=Sleep)58internalexternstatic
7、voidSleep(intdwMilliseconds);5960/得到鼠标相对与全屏的坐标,不是相对与你的Form的,且与你的分辨率有关系6162publicstaticintFullScreenPosition_X6364get6566POINTAPI_POINTAPI=newPOINTAPI();6768GetCursorPos(ref_POINTAPI);6970return_POINTAPI.x;71727374publicstaticintFullScreenPosition_Y7576get7778POINTAPI_POINTAPI=newPOINTAPI();7980GetCu
8、rsorPos(ref_POINTAPI);8182return_POINTAPI.y;83848586/隐藏显示鼠标87publicstaticvoidHide()8889ShowCursor(false);909192publicstaticvoidShow()9394ShowCursor(true);959697/将鼠标锁定在你的Form里不过你得将你的Form先锁了,FormResize就失效了98publicstaticvoidLock(System.Windows.Forms.FormObjectForm)99100RECT_FormRect=newRECT();101102Get
9、WindowRect(ObjectForm.Handle.ToInt32(),ref_FormRect);103104ClipCursor(ref_FormRect);105106107publicstaticvoidUnLock()108109RECT_ScreenRect=newRECT();110111_ScreenRect.top=0;112_ScreenRect.left=0;113_ScreenRect.bottom=System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom;114_ScreenRect.right=S
10、ystem.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right;115116ClipCursor(ref_ScreenRect);117118119/鼠标失效,不过失效的好像不只是鼠标,小心哦120publicstaticvoidDisable(System.Windows.Forms.FormObjectForm)121122EnableWindow(ObjectForm.Handle.ToInt32(),0);123124125publicstaticvoidEnable(System.Windows.Forms.FormObjectF
11、orm)126127EnableWindow(ObjectForm.Handle.ToInt32(),1);128129/得到你的鼠标类型130publicstaticstringType131132get133134if(GetSystemMetrics(SM_MOUSEPRESENT)=0)135136return本计算机尚未安装鼠标;137138else139140if(GetSystemMetrics(SM_MOUSEWHEELPRESENT)!=0)141142returnGetSystemMetrics(SM_CMOUSEBUTTONS)+键滚轮鼠标;143144else14514
12、6returnGetSystemMetrics(SM_CMOUSEBUTTONS)+键鼠标;147148149150151152/设置鼠标双击时间153publicstaticvoidDoubleClickTime_Set(intMouseDoubleClickTime)154155SetDoubleClickTime(MouseDoubleClickTime);156157158publicstaticstringDoubleClickTime_Get()159160returnGetDoubleClickTime().ToString();161162163/设置鼠标默认主键164publ
13、icstaticvoidDefaultRightButton()165166SwapMouseButton(1);167168169publicstaticvoidDefaultLeftButton()170171SwapMouseButton(0);172173调用如下:1/锁窗体2Mouse.Lock(this);3/锁光标4Mouse.Disable(this);12) . 标 ( 体/ ; 0 标. 体 ) 个 鼠 认标标/ ) * ) / / ) ( (_ 击鼠 ;键) _( 滚) (0 ;鼠尚本 )=) ( 标你 ) . ) 0) . ) . ( 0哦,是像失过标/ _ ( . . _ ; . . 0 0= _ ( _ 0 0 ( 0 , . 00( = 0) . 效就 了 得不 的在鼠/ ; ) ) 示藏/ ) _ ) = 率辨与的 你是标屏与鼠/ ( = , ( . ) , . . . , ( . ) . . ; ) . ( . . , ( = . ( . . ( = ( = ;) ( = . 0 ; ) = ( ; = ; ; / - / /