您好,欢迎来到意榕旅游网。
搜索
您的当前位置:首页C#限制鼠标移动范围

C#限制鼠标移动范围

来源:意榕旅游网


1 2 3 4 5 6 7 8 9 10 11 12 13

一个鼠标类#region 一个鼠标类 /**////

/// Mouse Control /// Made by Michael /// date 2008-01-30 /// class Mouse

{

internal const byte SM_MOUSEPRESENT = 19; internal const byte SM_CMOUSEBUTTONS = 43;

internal const byte SM_MOUSEWHEELPRESENT = 75;

internal struct POINTAPI

14 {

15 internal int x; 16 internal int y; 17 } 18

19 internal struct RECT 20 {

21 internal int left; 22 internal int top; 23 internal int right; 24 internal int bottom; 25 } 26

27 [System.Runtime.InteropServices.DllImport(\"user32.dllton\")] 28 29 30 31 32 33)] 34 35 36 37 38 39\")]

internal extern static int SwapMouseButton(int bSwap);

[System.Runtime.InteropServices.DllImport(\"user32\ internal extern static int ClipCursor(ref RECT lpRect);

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

[System.Runtime.InteropServices.DllImport(\"user32.dll\ internal extern static bool ShowCursor(bool bShow);

[System.Runtime.InteropServices.DllImport(\"user32.dll\

40 internal extern static int EnableWindow(int hwnd, int fEnable); 41

42 [System.Runtime.InteropServices.DllImport(\"user32.dll\ct\")]

43 internal extern static int GetWindowRect(int hwnd, ref RECT lpRect); 44

45 [System.Runtime.InteropServices.DllImport(\"user32.dll\ 46 internal extern static int SetCursorPos(int x, int y); 47

48 [System.Runtime.InteropServices.DllImport(\"user32.dll\ics\")]

49 internal extern static int GetSystemMetrics(int nIndex); 50

51 [System.Runtime.InteropServices.DllImport(\"user32.dll\kTime\")]

52 internal extern static int SetDoubleClickTime(int wCount); 53

54 [System.Runtime.InteropServices.DllImport(\"user32.dll\kTime\")] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

internal extern static int GetDoubleClickTime();

[System.Runtime.InteropServices.DllImport(\"kernel32.DLL\ 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);

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97了

return _POINTAPI.y; } }

//隐藏 显示 鼠标

public static void Hide() {

ShowCursor(false); }

public static void Show() {

ShowCursor(true); }

//将鼠标锁定在你的Form里 不过你得将你的Form先锁了,Form Resize 就失效

98 public static void Lock(System.Windows.Forms.Form ObjectForm) 99 {

100 RECT _FormRect = new RECT(); 101

102 GetWindowRect(ObjectForm.Handle.ToInt32(), ref _FormRect); 103

104 ClipCursor(ref _FormRect); 105 } 106

107 public static void UnLock() 108 {

109 RECT _ScreenRect = new RECT(); 110

111 _ScreenRect.top = 0; 112 _ScreenRect.left = 0;

113 _ScreenRect.bottom = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom;

114 _ScreenRect.right = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right; 115116117118119120121122

ClipCursor(ref _ScreenRect); }

//鼠标失效,不过失效的好像不只是鼠标,小心哦

public static void Disable(System.Windows.Forms.Form ObjectForm) {

EnableWindow(ObjectForm.Handle.ToInt32(), 0);

123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166

}

public static void Enable(System.Windows.Forms.Form ObjectForm) {

EnableWindow(ObjectForm.Handle.ToInt32(), 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);

167 } 168

169 public static void DefaultLeftButton() 170 {

171 SwapMouseButton(0); 172 } 173 }

调用如下: 1 //锁窗体

2 Mouse.Lock(this); 3 //锁光标

4 Mouse.Disable(this);

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- yrrf.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务