c#调用API控制鼠标移动单击双击以及滚轮的公共类.docx
- 文档编号:10417963
- 上传时间:2023-02-11
- 格式:DOCX
- 页数:11
- 大小:15.43KB
c#调用API控制鼠标移动单击双击以及滚轮的公共类.docx
《c#调用API控制鼠标移动单击双击以及滚轮的公共类.docx》由会员分享,可在线阅读,更多相关《c#调用API控制鼠标移动单击双击以及滚轮的公共类.docx(11页珍藏版)》请在冰豆网上搜索。
c#调用API控制鼠标移动单击双击以及滚轮的公共类
c#调用API控制鼠标移动单击双击以及滚轮的公共类
usingSystem;
usingSystem.Text;
usingSystem.Runtime.InteropServices;usingSystem.Drawing;
usingSystem.Windows.Forms;
namespaceKeyboardHook
{
///
///Mousebuttonsthatcanbepressed
///
publicenumMouseButton
{
Left=0x2,
Right=0x8,
Middle=0x20
}
///
///Operationsthatsimulatemouseevents
///
publicstaticclassMouseSimulator
{
#regionWindowsAPICode
[DllImport("user32.dll")]
staticexternintShowCursor(boolshow);
[DllImport("User32")]
publicexternstaticvoidSetCursorPos(intx,inty);
[DllImport("user32.dll")]
staticexternvoidmouse_event(intflags,intdX,intdY,intbuttons,int
extraInfo);
[DllImport("user32.dll")]
staticexternvoidPostMessage(IntPtrhWnd,intMsg,intwParam,uint
lParam);
constintMOUSEEVENTF_MOVE=0x1;
constintMOUSEEVENTF_LEFTDOWN=0x2;
constintMOUSEEVENTF_LEFTUP=0x4;
constintMOUSEEVENTF_RIGHTDOWN=0x8;
constintMOUSEEVENTF_RIGHTUP=0x10;
constintMOUSEEVENTF_MIDDLEDOWN=0x20;
constintMOUSEEVENTF_MIDDLEUP=0x40;
constintMOUSEEVENTF_WHEEL=0x800;
constintMOUSEEVENTF_ABSOLUTE=0x8000;
constintWM_MOUSEMOVE=0x200;
constintWM_LBUTTONDOWN=0x201;
constintWM_RBUTTONDOWN=0x204;
constintWM_MBUTTONDOWN=0x207;
constintWM_LBUTTONUP=0x202;
constintWM_RBUTTONUP=0x205;
constintWM_MBUTTONUP=0x208;
#endregion
#regionProperties
///
///GetsorsetsastructurethatrepresentsbothXandYmousecoordinates
///
publicstaticPointPosition
{
get
{
returnnewPoint(Cursor.Position.X,Cursor.Position.Y);
}
set
{
Cursor.Position=value;
}
}
///
///Getsorsetsonlythemouse'sxcoordinate
///
publicstaticintX
{
get
{
returnCursor.Position.X;
}
set
{
Cursor.Position=newPoint(value,Y);
}
}
///
///Getsorsetsonlythemouse'sycoordinate
///
publicstaticintY
{
get
{
returnCursor.Position.Y;
}
set
{
Cursor.Position=newPoint(X,value);
}
}
#endregion
#regionMethods
///
///Pressamousebuttondown
///
///
publicstaticvoidMouseDown(MouseButtonbutton)
{
mouse_event(((int)button),0,0,0,0);
}
publicstaticvoidMouseDown(IntPtrhWnd,MouseButtonbutton,intx,inty)
{
switch(button)
{
caseMouseButton.Left:
PostMessage(hWnd,WM_LBUTTONDOWN,1,Convert.ToUInt32((x+(y
<<16))));
break;
caseMouseButton.Right:
PostMessage(hWnd,WM_RBUTTONDOWN,1,Convert.ToUInt32((x+(y
<<16))));
break;
caseMouseButton.Middle:
PostMessage(hWnd,WM_MBUTTONDOWN,1,Convert.ToUInt32((x+(y
<<16))));
break;
}
}
///
///Letamousebuttonup
///
///
publicstaticvoidMouseUp(MouseButtonbutton)
{
mouse_event(((int)button)*2,0,0,0,0);
}
publicstaticvoidMouseUp(IntPtrhWnd,MouseButtonbutton,intx,inty)
{
switch(button)
{
caseMouseButton.Left:
PostMessage(hWnd,WM_LBUTTONUP,0,Convert.ToUInt32((x+(y<<
16))));
break;
caseMouseButton.Right:
PostMessage(hWnd,WM_RBUTTONUP,0,Convert.ToUInt32((x+(y<<
16))));
break;
caseMouseButton.Middle:
PostMessage(hWnd,WM_MBUTTONUP,0,Convert.ToUInt32((x+(y<<
16))));
break;
}
}
///
///Clickamousebutton(downthenup)
///
///
publicstaticvoidClick(MouseButtonbutton)
{
MouseDown(button);
MouseUp(button);
}
publicstaticvoidClick(IntPtrhWnd,MouseButtonbutton,intx,inty)
{
MouseDown(hWnd,button,x,y);
MouseUp(hWnd,button,x,y);
}
///
///Doubleclickamousebutton(downthenuptwice)
///
///
publicstaticvoidDoubleClick(MouseButtonbutton)
{
Click(button);
Click(button);
}
publicstaticvoidDoubleClick(IntPtrhWnd,MouseButtonbutton,intx,int
y)
{
Click(hWnd,button,x,y);
Click(hWnd,button,x,y);
}
///
///Rollthemousewheel.Deltaof120wheelsuponcenormally,-120wheels
downoncenormally
///
///
publicstaticvoidMouseWheel(intdelta)
{
mouse_event(MOUSEEVENTF_WHEEL,0,0,delta,0);
}
///
///Showahiddencurrentoncurrentlyapplication
///
publicstaticvoidShow()
{
ShowCursor(true);
}
///
///Hidemousecursoronlyoncurrentapplication'sforms
///
publicstaticvoidHide()
{
ShowCursor(false);
}
///
///Pressamousebuttondown
///
///
publicstaticvoidMouseDown(MouseButtonsbutton)
{
switch(button)
{
caseMouseButtons.Left:
MouseDown(MouseButton.Left);
break;
caseMouseButtons.Middle:
MouseDown(MouseButton.Middle);
break;
caseMouseButtons.Right:
MouseDown(MouseButton.Right);
break;
}
}
///
///Letamousebuttonup
///
///
publicstaticvoidMouseUp(MouseButtonsbutton)
{
switch(button)
{
caseMouseButtons.Left:
MouseUp(MouseButton.Left);
break;
caseMouseButtons.Middle:
MouseUp(MouseButton.Middle);
break;
caseMouseButtons.Right:
MouseUp(MouseButton.Right);
break;
}
}
///
///Clickamousebutton(downthenup)
///
///
publicstaticvoidClick(MouseButtonsbutton)
{
switch(button)
{
caseMouseButtons.Left:
Click(MouseButton.Left);
break;
caseMouseButtons.Middle:
Click(MouseButton.Middle);
break;
caseMouseButtons.Right:
Click(MouseButton.Right);
break;
}
}
///
///Doubleclickamousebutton(downthenuptwice)
///
///
publicstaticvoidDoubleClick(MouseButtonsbutton)
{
switch(button)
{
caseMouseButtons.Left:
DoubleClick(MouseButton.Left);
break;
caseMouseButtons.Middle:
DoubleClick(MouseButton.Middle);
break;
caseMouseButtons.Right:
DoubleClick(MouseButton.Right);
break;
}
}
#endregion
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c# 调用 API 控制 鼠标 移动 单击 双击 以及 滚轮 公共