////////////////////
//mouse.h
//模拟鼠标的常见操作
////////////////////
#pragma once
#include
class MOUSE
{
private:
//坐标变量
POINT point;
public:
//移动类函数
void Move(int x,int y);
void RelativeMove(int cx,int cy);
void SavePos();
void RestorePos();
//锁定启用类
void Lock();
void Unlock();
//动作类
void LBClick();
void LBDbClick();
void LBDown();
void LBUp();
void RBClick();
void RBDbClick();
void RBDown();
void RBUp();
void MBClick();
void MBDbClick();
void MBDown();
void MBUp();
void MBRoll(int ch);
};
//移动鼠标到绝对位置(X坐标,Y坐标)
void MOUSE::Move(int x,int y)
{
this->point.x=x;
this->point.y=y;
::SetCursorPos(x,y);
}
//移动鼠标到相对位置(X位移,Y位移)
void MOUSE::RelativeMove(int cx,int cy)
{
::GetCursorPos(&this->point);
this->point.x+=cx;
this->point.y+=cy;
::SetCursorPos(this->point.x,this->point.y);
}
//保存当前位置()
void MOUSE::SavePos()
{
::GetCursorPos(&this->point);
}
//恢复鼠标位置()
void MOUSE::RestorePos()
{
::SetCursorPos(this->point.x,this->point.y);
}
//锁定鼠标()
void MOUSE::Lock()
{
POINT pt;
RECT rt;
::GetCursorPos(&pt);
rt.left=rt.right=pt.x;
rt.top=rt.bottom=pt.y;
rt.right++;
rt.bottom++;
::ClipCursor(&rt);
}
//解锁鼠标()
void MOUSE::Unlock()
{
::ClipCursor(NULL);
}
//左键单击()
void MOUSE::LBClick()
{
this->SavePos();
::mouse_event(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_LEFTUP,this->point.x,this->point.y,0,0);
}
//左键双击()
void MOUSE::LBDbClick()
{
this->SavePos();
::mouse_event(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_LEFTUP,this->point.x,this->point.y,0,0);
::mouse_event(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_LEFTUP,this->point.x,this->point.y,0,0);
}
//左键按下()
void MOUSE::LBDown()
{
this->SavePos();
::mouse_event(MOUSEEVENTF_LEFTDOWN,this->point.x,this->point.y,0,0);
}
//左键抬起()
void MOUSE::LBUp()
{
this->SavePos();
::mouse_event(MOUSEEVENTF_LEFTUP,this->point.x,this->point.y,0,0);
}
//右键单击()
void MOUSE::RBClick()
{
this->SavePos();
::mouse_event(MOUSEEVENTF_RIGHTDOWN|MOUSEEVENTF_RIGHTUP,this->point.x,this->point.y,0,0);
}
//右键双击()
void MOUSE::RBDbClick()
{
this->SavePos();
::mouse_event(MOUSEEVENTF_RIGHTDOWN|MOUSEEVENTF_RIGHTUP,this->point.x,this->point.y,0,0);
::mouse_event(MOUSEEVENTF_RIGHTDOWN|MOUSEEVENTF_RIGHTUP,this->point.x,this->point.y,0,0);
}
//右键按下()
void MOUSE::RBDown()
{
this->SavePos();
::mouse_event(MOUSEEVENTF_RIGHTDOWN,this->point.x,this->point.y,0,0);
}
//右键抬起()
void MOUSE::RBUp()
{
this->SavePos();
::mouse_event(MOUSEEVENTF_RIGHTUP,this->point.x,this->point.y,0,0);
}
//中键单击()
void MOUSE::MBClick()
{
this->SavePos();
::mouse_event(MOUSEEVENTF_MIDDLEDOWN|MOUSEEVENTF_MIDDLEUP,this->point.x,this->point.y,0,0);
}
//中键双击()
void MOUSE::MBDbClick()
{
this->SavePos();
::mouse_event(MOUSEEVENTF_MIDDLEDOWN|MOUSEEVENTF_MIDDLEUP,this->point.x,this->point.y,0,0);
::mouse_event(MOUSEEVENTF_MIDDLEDOWN|MOUSEEVENTF_MIDDLEUP,this->point.x,this->point.y,0,0);
}
//中键按下()
void MOUSE::MBDown()
{
this->SavePos();
::mouse_event(MOUSEEVENTF_MIDDLEDOWN,this->point.x,this->point.y,0,0);
}
//中键抬起()
void MOUSE::MBUp()
{
this->SavePos();
::mouse_event(MOUSEEVENTF_MIDDLEUP,this->point.x,this->point.y,0,0);
}
//中键滚动(滚动位移)
void MOUSE::MBRoll(int ch)
{
this->SavePos();
::mouse_event(MOUSEEVENTF_WHEEL,this->point.x,this->point.y,ch,0);
}
/////////////////////////
//keyboard.h
//模拟键盘的常见操作
/////////////////////////
#pragma once
#include
class KEYBOARD
{
public:
void PressStr(char *str);
void PressKey(BYTE bVk);
void KeyDown(BYTE bVk);
void KeyUp(BYTE bVk);
};
//按键(虚拟键值)
void KEYBOARD::PressKey(BYTE bVk)
{
::keybd_event(bVk,0,0,0);
::keybd_event(bVk,0,KEYEVENTF_KEYUP,0);
}
//按下(虚拟键值)
void KEYBOARD::KeyDown(BYTE bVk)
{
::keybd_event(bVk,0,0,0);
}
//抬起(虚拟键值)
void KEYBOARD::KeyUp(BYTE bVk)
{
::keybd_event(bVk,0,KEYEVENTF_KEYUP,0);
}
//发送字符串(字符串)
void KEYBOARD::PressStr(char *str)
{
for (unsigned i=0;i {
if (str[i]>0x60 && str[i]<0x7B)
this->PressKey(str[i]-0x20);
else
this->PressKey(str[i]);
}
}
BOOL SetCursorPos(
int X, // horizontal position
int Y // vertical position
);
设置鼠标位置。鼠标指针在屏幕像素坐标系统中的X,Y位置
这个函数是用来设置Mouse位置的。可以用这个函数来移动mouse在屏幕上的移动。
另外一个函数功能比较强,即mouse_event()
VOID mouse_event(
DWORD dwFlags,
DWORD dx,
DWORD dy,
DWORD dwData,
DWORD dwExtraInfo
);
设置mouse状态。参数说明如下:
dwFlags Long,下述标志的一个组合 :
MOUSEEVENTF_ABSOLUTE dx和dy指定鼠标坐标系统中的一个绝对位置。在鼠标坐标系统中,屏幕在水平和垂直方向上均匀分割成65535×65535个单元
MOUSEEVENTF_MOVE 移动鼠标
MOUSEEVENTF_LEFTDOWN 模拟鼠标左键按下
MOUSEEVENTF_LEFTUP 模拟鼠标左键抬起
MOUSEEVENTF_RIGHTDOWN 模拟鼠标右键按下
MOUSEEVENTF_RIGHTUP 模拟鼠标右键按下
MOUSEEVENTF_MIDDLEDOWN 模拟鼠标中键按下
MOUSEEVENTF_MIDDLEUP 模拟鼠标中键按下
dx 根据是否指定了MOUSEEVENTF_ABSOLUTE标志,指定水平方向的绝对位置或相对运动
dy 根据是否指定了MOUSEEVENTF_ABSOLUTE标志,指定垂直方向的绝对位置或相对运动
dwData amount of wheel movement
dwExtraInfo,通常未用的一个值。用GetMessageExtraInfo函数可取得这个值。可用的值取决于特定的驱动程序。
例如:将mouse移动到坐标(450,100) 可用函数:
SetCursorPos(450,100) ; 或
mouse_event(MOUSEEVENTF_MOVE ,0,450,100,GetMessageExtraInfo()); 来实现
实现鼠标单击:
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,GetMessageExtraInfo());
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,GetMessageExtraInfo ());
那么,如何模拟mouse不断的移动和点击呢?这需要用定时器完成。
以C为例:
在初始化程序的时候,设置时钟:
...
case WM_CREATE:
...
SetTimer(1, 1000, NULL);
break;
这样就设置了ID号为1 的时钟事件, 它每秒(1000毫秒)将产生一个 WM_TIMER事件。
然后我们在WM_TIMER事件中将入相关的处理:
case WM_TIMER:
{
...
// mouse move code...
// mouse click code...
break;
}
阅读(1730) | 评论(0) | 转发(0) |