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 ) ;
}
}
}
分享到:
相关推荐
上次下载的Using ListView control under Win32 API在vc 2008转换失败,我在此基础上修改了一下可以在VC2008上运行!
C#语言,注册表使用,using Microsoft.Win32;枚举类型数据的Parse
Using TreeControl (TreeView) under Win32 API
C#利用win32 Api 修改本地系统时间、获取硬盘序列号,可以用于软件注册机制的编写! 代码如下: using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; ...
本文实例讲述了C#通过WIN32 API实现嵌入程序窗体的方法,...我们把它封装到一个类中: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using Syst
Your Complete Example-Rich Guide to Using and Extending LINQ to Objects and PLINQ Using LINQ to Objects, .NET developers can write queries over object collections with the same deep functionality ...
CSharp算法Cambridge+University+Press+-+Data+Structures+and+Algorithms+Using+C#+(Mar+2007)
C# 视频API类 摄像头 using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Drawing.Design ;
Example program to demonstrate printing using Win32 API. Tom Lee
我意识到我可以使用低级 Win32 API 钩子来完成这项工作,但无法在网上或 GitHub 上的其他地方找到任何工作。 我看到的大多数项目的 API 或方法都存在问题,这些问题不再适用于 Visual Studio 2012/2013 或 .NET 4.5...
DATA STRUCTURES AND ALGORITHMS USING C#
ASP.NET using c# and MVC.pdf
Problem Solving in Data Structures & Algorithms Using C# 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
VSTO详细介绍(英文版)
Visual C# and Databases is a tutorial that provides a detailed introduction to using Visual C# for accessing and maintaining databases. Topics covered include: database structure, database design, ...
Beginning ASP.NET Databases using C#
Win32的API函数是微软自己的东西,可以直接在C#中直接调用,在做WinForm时还是很有帮助的。有时候我们之直接调用Win32 的API,可以很高效的实现想要的效果。 代码 using System; using System.Collections.Generic;...
数据结构C#版,pdf格式 本书除了包括对传统线形链表、二叉树、图论等的基于C#的描述外,还涉及对排序算法、搜索算法的讲解,同时包括对动态规划(dynamic programming)和贪心算法(greedy algorithms)的相关内容的...
Introduction to 3D Game Engine Design Using DirectX 9 and C# by Lynn Thomas Harrison 导言 —— 我自学游戏设计和3D可视化很多年了。我的书架上充满了程序设计、游戏开发,以及各种图形APIs的书籍。许多书籍都...