1#region
3using System;
4using System.Runtime.InteropServices;
6#endregion
8namespace Windows.Forms.Base
9{
public class Mouse
{
MouseEventFlag enum#region MouseEventFlag enum
14 [Flags]
public enum MouseEventFlag : uint
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}
31 #endregion
33 internal const byte SM_CMOUSEBUTTONS = 43;
internal const byte SM_MOUSEPRESENT = 19;
internal const byte SM_MOUSEWHEELPRESENT = 75;
37 public static int FullScreenPosition_X
{
get
{
POINTAPI _POINTAPI = new POINTAPI();
GetCursorPos(ref _POINTAPI);
return _POINTAPI.x;
}
}
47 public static int FullScreenPosition_Y
{
get
{
POINTAPI _POINTAPI = new POINTAPI();
GetCursorPos(ref _POINTAPI);
return _POINTAPI.y;
}
}
57 public static string Type
{
get
{
if (GetSystemMetrics(SM_MOUSEPRESENT) == 0)
{
return "本计算机尚未安装鼠标";
}
if (GetSystemMetrics(SM_MOUSEWHEELPRESENT) != 0)
{
return GetSystemMetrics(SM_CMOUSEBUTTONS) + "键滚轮鼠标";
}
return GetSystemMetrics(SM_CMOUSEBUTTONS) + "键鼠标";
}
}
73 /**//// <summary>
///鼠标左右键功能互换
/// </summary>
/// <param name="bSwap"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "SwapMouseButton")]
public static extern int SwapMouseButton(int bSwap);
81 /**//// <summary>
/// 鼠标的移动区域限制
/// 0为释放限制
/// </summary>
/// <param name="lpRect"></param>
/// <returns></returns>
[DllImport("user32", EntryPoint = "ClipCursor")]
public static extern int ClipCursor(ref RECT lpRect);
90 /**//// <summary>
/// 获取鼠标坐标
/// </summary>
/// <param name="lpPoint"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "GetCursorPos")]
public static extern int GetCursorPos(ref POINTAPI lpPoint);
98 /**//// <summary>
/// 显示和隐藏鼠标指针.
/// 1为显示0为隐藏
/// </summary>
/// <param name="bShow"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "ShowCursor")]
public static extern bool ShowCursor(bool bShow);
107 /**//// <summary>
/// 将非模态窗口显示为模态窗口
/// </summary>
/// <param name="hwnd"></param>
/// <param name="fEnable"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "EnableWindow")]
public static extern int EnableWindow(int hwnd, int fEnable);
116 /**//// <summary>
/// 获得窗口的大小
/// </summary>
/// <param name="hwnd"></param>
/// <param name="lpRect"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "GetWindowRect")]
public static extern int GetWindowRect(int hwnd, ref RECT lpRect);
125 /**//// <summary>
/// 设置鼠标坐标
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
public static extern int SetCursorPos(int x, int y);
134 /**//// <summary>
/// 返回Win桌面中各种显示单元的宽度和高度、是否安装了鼠标、是否调换了鼠标左右键的定义等
/// </summary>
/// <param name="nIndex"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
public static extern int GetSystemMetrics(int nIndex);
142 /**//// <summary>
/// 参数wCount,表示鼠标双击时间,为毫秒级,系统默认时间为500
/// </summary>
/// <param name="wCount"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "SetDoubleClickTime")]
public static extern int SetDoubleClickTime(int wCount);
150 /**//// <summary>
/// 该函数无参数;它的返回值为毫秒,为双击鼠标双击有效的时间间隔。
/// </summary>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
public static extern int GetDoubleClickTime();
157 /**//// <summary>
/// 只是休眠而已
/// </summary>
/// <param name="dwMilliseconds"></param>
[DllImport("kernel32.DLL", EntryPoint = "Sleep")]
public static extern void Sleep(int dwMilliseconds);
164 /**//// <summary>
/// 模拟鼠标操作
/// 调用方法
/// mouse_event(MOUSEEVENTF_LEFTDOWN, X * 65536 / 1024, Y * 65536 / 768, 0, 0);
/// mouse_event(MOUSEEVENTF_LEFTUP, X * 65536 / 1024, Y * 65536 / 768, 0, 0);
/// 其中X,Y分别是你要点击的点的横坐标和纵坐标
/// </summary>
/// <param name="dwFlags"></param>
/// <param name="dx"></param>
/// <param name="dy"></param>
/// <param name="dwData"></param>
/// <param name="dwExtraInfo"></param>
[DllImport("user32.dll")]
public static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
179 // 隐藏 显示 鼠标
public static void Hide()
{
ShowCursor(false);
}
185 public static void Show()
{
ShowCursor(true);
}
190 // 将鼠标锁定在你的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);
}
198 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);
}
208 // 鼠标失效,不过失效的好像不只是鼠标,小心哦
public static void Disable(System.Windows.Forms.Form ObjectForm)
{
EnableWindow(ObjectForm.Handle.ToInt32(), 0);
}
214 public static void Enable(System.Windows.Forms.Form ObjectForm)
{
EnableWindow(ObjectForm.Handle.ToInt32(), 1);
}
219 // 鼠标自己移动
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);
}
}
}
261 // 得到你的鼠标类型
263 // 设置鼠标双击时间
public static void DoubleClickTime_Set(int MouseDoubleClickTime)
{
SetDoubleClickTime(MouseDoubleClickTime);
}
269 public static string DoubleClickTime_Get()
{
return GetDoubleClickTime().ToString();
}
274 // 设置鼠标默认主键
public static void DefaultRightButton()
{
SwapMouseButton(1);
}
280 public static void DefaultLeftButton()
{
SwapMouseButton(0);
}
285 Nested type: POINTAPI#region Nested type: POINTAPI
287 public struct POINTAPI
{
public int x;
public int y;
}
293 #endregion
295 Nested type: RECT#region Nested type: RECT
297 public struct RECT
{
public int bottom;
public int left;
public int right;
public int top;
}
305 #endregion
}
307}

c# 鼠标操作的更多相关文章

  1. Python模拟键盘输入和鼠标操作

    Python模拟键盘输入和鼠标操作 一.Python键盘输入模拟: import win32api import win32con win32api.keybd_event(17,0,0,0)  #c ...

  2. opencv鼠标操作及GUI矩形绘画

    OpenCV的鼠标操作是通过一个中介函数配合回调函数来实现的.指定鼠标操作消息回调函数的函数为SetMouseCallback. void setMouseCallback(const string& ...

  3. WPF 中模拟键盘和鼠标操作

    转载:http://www.cnblogs.com/sixty/archive/2009/08/09/1542210.html 更多经典文章:http://www.qqpjzb.cn/65015.ht ...

  4. python selenium-webdriver 元素操作之鼠标操作(四)

    上节内容主要说明了元素的定位,本节内容说要说对元素的操作,元素的操作分为两部分一部分是鼠标的操作,另一种是对键盘对元素的操作,下面我们主要讲解一下鼠标对元素的操作. webdriver 模块中几种比较 ...

  5. python-web自动化-鼠标操作

    鼠标操作由ActionChains类来完成鼠标操作 perform() 执行鼠标操作move_to_element() 鼠标悬浮:最常用的操作double_click() 双击操作context_cl ...

  6. Selenium基础知识(二)鼠标操作

    一.鼠标操作 这个需要使用webdriver下的ActionChains类,这个类是操作鼠标操作的: from selenium.webdriver import ActionChains 鼠标操作可 ...

  7. selenium自动化之鼠标操作

    在做自动化测试的时候,经常会遇到这种情况,某个页面元素,你必须要把鼠标移动到上面才能显示出元素.那么这种情况,我们怎么处理呢?,selenium给我们提供了一个类来处理这类事件——ActionChai ...

  8. 【python】鼠标操作

    [python]鼠标操作 推荐地址:http://www.cnblogs.com/fnng/p/3288444.html --------------------------------------- ...

  9. OpenCV——图像的载入、显示、输出到文件和滑动条、鼠标操作

    图像的载入.显示.输出到文件和滑动条 滑动条 示例: 鼠标操作

随机推荐

  1. html表格标签与属性

    标记:  标 记  说 明 <Table> 表格标记 <Tr> 行标记 <Td> 单元格标记  <Th> 表头标记 <Table>标记属性: ...

  2. JAVA正则表达式之贪婪、勉强和侵占

    在JAVA正则表达式中量词(quantifiers)允许指定匹配出现的次数,方便起见,当前 Pattern API 规范下,描述了贪婪.勉强和侵占三种量词.首先粗略地看一下,量词X?.X??和X?+都 ...

  3. 如何在sqlserver建立新用户并关联相应的数据库

    我们经常需要在数据库上建立有权限的用户,该用户只能去操作某个特定的数据库(比如该用户只能去读,去写等等),那么我们应该怎么在sqlserver上设置呢?下面的步骤有点长,只要一步一步跟着设置就行 方法 ...

  4. (转)反射发送实战(-)InvokeMember

    反射是.net中的高级功能之一,利用反射可以实现许多以前看来匪夷所思的功能,下面是我看了<Programming C#>(O'Reilly)之后对于反射的一点实践,本想直接做个应用程序来说 ...

  5. 概率dp小结

    好久之前学过,记得是一次亚洲区的前几天看了看概率dp,然后亚洲区就出了一道概率dp,当时虽然做上了,但是感觉有很多地方没懂,今天起早温习了一下,觉得很多地方茅塞顿开,果然学习的话早上效果最好了. 首先 ...

  6. JDK + Tomcat 安装配置

    学习Java 开发的第一步就是配置环境,今天第一次配置,把过程记录下以备后用. 一.下载JDK.Tomcat JDK:http://www.oracle.com/technetwork/java/ja ...

  7. POJ1840 hash

    POJ1840 问题重述: 给定系数a1,a2, ..,a5,求满足a1 * x1 ^ 3 + a2 * x2 ^ 3 +... + a5 * x5 ^ 3 = 0的 xi 的组数.其中ai, xi都 ...

  8. Python 一路走来 DOM & Jquery

    DOM           查找:                直接查找                间接查找                —getElementById             ...

  9. How many ways??(HDU 2157)

    How many ways?? Sample Input 4 4 //n个点,m条路径0 1 //s->t可通0 21 32 32 //询问数0 3 2 //从0到3走两条路可到的方案有多少种0 ...

  10. cf B. Dima and To-do List

    http://codeforces.com/contest/366/problem/B 从0到k枚举起点,然后i+k判断是不是i+k>=n如果是i=(i+k)%n;否则i=i+k; #inclu ...