★一个鼠标类☆(Using C# and Win32API)

lkal4587 2003-04-11 06:35:14
namespace 刘侃ClassLibrary.Hardware
{
// 0.原创 Using C# and Win32API ( 最近我把所有的Win32API看了1遍 很是过瘾 )

// 1.所有的方法都没有错误处理

// 2.觉的有用的朋友 给我加点分 我好继续

// 3.斧头 y 狗狗 你们到哪里去了? 想死我了!

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 ) ;
}
}
}
...全文
103 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
DavidBone 2003-04-15
  • 打赏
  • 举报
回复
ding
qimini 2003-04-15
  • 打赏
  • 举报
回复
up
lkal4587 2003-04-13
  • 打赏
  • 举报
回复
大家共同学习 共同进步了
yqdeng 2003-04-13
  • 打赏
  • 举报
回复
studying
Bob 2003-04-13
  • 打赏
  • 举报
回复
晚辈学习:)
linuxuer 2003-04-12
  • 打赏
  • 举报
回复
hehe
大家都使高手啊
cqnimin 2003-04-12
  • 打赏
  • 举报
回复
学习:)
用户 昵称 2003-04-12
  • 打赏
  • 举报
回复
study
lkal4587 2003-04-11
  • 打赏
  • 举报
回复
y 可是见到你了
yarshray 2003-04-11
  • 打赏
  • 举报
回复
感觉你的封装方式很象MFC

lkmj 2003-04-11
  • 打赏
  • 举报
回复
:)
lkal4587 2003-04-11
  • 打赏
  • 举报
回复
哦 多谢了

三人行 必有我师呀
TheAres 2003-04-11
  • 打赏
  • 举报
回复
大体提了一下我的看法,刘兄台承让了,老弟班门弄斧了。

using System;
using System.Windows.Forms;

namespace WinFormCS
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Mouse
{
internal const byte SM_MOUSEPRESENT = 19;
internal const byte SM_CMOUSEBUTTONS = 43;
internal const byte SM_MOUSEWHEELPRESENT = 75;

internal struct POINTAPI //用System.Drawing.Point就可以。
{

internal int x;
internal int y;
}

internal struct RECT //用Rectangle 就可以
{
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);
//直接对Cursor.Clip 设置就可以

[System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint="GetCursorPos" )]
internal extern static int GetCursorPos( ref POINTAPI lpPoint );
//直接Cursor.Position,转换为窗体的用Control.PointToClient ,转换为Scrren用Control.PointToScreen


[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="ShowCursor")]
internal extern static bool ShowCursor ( bool bShow ) ;

//Cursor.Show();

[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 ) ;
//Cursor.Position

[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetSystemMetrics")]
internal extern static int GetSystemMetrics( int nIndex );
//这个在Screen中有包含

[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() ;
//SystemInformation.DoubleClickTime
[System.Runtime.InteropServices.DllImport("kernel32.DLL", EntryPoint="Sleep")]
internal extern static void Sleep ( int dwMilliseconds ) ;

//Thread.Sleep

//得到鼠标相对与全屏的坐标,不是相对与你的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 ) ;
}
// Cursor.Hide();

public static void Show()
{
ShowCursor( true ) ;
}
//Cursor.Show();

// 将鼠标锁定在你的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 );
//直接对Cursor.Clip赋值
}

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 )
//用这个帖子中的API
//如何用程序实现键盘和鼠标的模拟?
//http://expert.csdn.net/Expert/TopicView3.asp?id=1567478


{
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 "本计算机尚未安装鼠标" ; //SystemInformation.MousePresent
}
else
{
if ( GetSystemMetrics( SM_MOUSEWHEELPRESENT ) != 0 )
{
return GetSystemMetrics( SM_CMOUSEBUTTONS ) + "键滚轮鼠标" ;
//SystemInformation.MouseWheelPresent
}
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 ) ;
}

}
}

lkal4587 2003-04-11
  • 打赏
  • 举报
回复
o 心疤 好久不见了
snewxf 2003-04-11
  • 打赏
  • 举报
回复
好。:)
lkal4587 2003-04-11
  • 打赏
  • 举报
回复
.net没有封装的多了去了 我这里一堆一堆
CForce 2003-04-11
  • 打赏
  • 举报
回复
http://www.bestdown.com/ebook.php?bkclass=01&PHPSESSID=0ba40486adc719501911a4011fcfb704
CForce 2003-04-11
  • 打赏
  • 举报
回复
就是说一些.net没有封装的,但是有用的api,播放声音就是典型的一个。
TheAres 2003-04-11
  • 打赏
  • 举报
回复
TO CForce():
书从那个地方可以搞到,别让我用美元去买啊。谢谢
lkal4587 2003-04-11
  • 打赏
  • 举报
回复
api一共好象是1465个


In search the lost of Win32 API


怎么成了 lost 了?

晕哦
加载更多回复(4)

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧