SetCursorPos( x,y ); 移动鼠标到x,y位置
我们提供的服务有:成都网站设计、网站制作、微信公众号开发、网站优化、网站认证、荥阳ssl等。为近1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的荥阳网站制作公司
mouse_event( MOUSEEVENTF_LEFTDOWN, x,y,0,0 ); 左键按下
mouse_event( MOUSEEVENTF_RIGHTDOWN, x,y,0,0 ); 右键按下
如果要捕捉鼠标的移动,一般采用勾子程序,详情,请查阅鼠标勾子代码
可以
B00L PostMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam);
关于后两个类型:
typedef UINT WPARAM;
typedef LONG LPARAM;
都是4字节
hWnd--要发给的程序句柄
Msg:
Msg=WM_MOUSEMOVE..鼠标移动事件
wParam:
MK_CONTROL ctrl键按下.
MK_LBUTTON 鼠标左键键按下.
MK_MBUTTON 鼠标中间键按下.
MK_RBUTTON 鼠标右键按下.
MK_SHIFT shift键按下.
lParam--高位当前鼠标y坐标,低位当前鼠标x坐标
比如给句柄为hWnd的程序队列发送一个鼠标移动信息,同时鼠标左键和中间处于按下状态,鼠标坐标为(120,100)
PostMessage(hWnd, WM_MOUSEMOVE ,MK_LBUTTON|MK_MBUTTON,(16100)|120);
如果是要发送数据,然后你在程序端自己处理的话,推荐用sendmessage函数,因为PostMessage只是把消息发送到程序的消息队列去,程序自动处理.就是说,如果接收消息的程序是你自己写,用sendmessage就可以,如果是别人写的程序,你要仿真一个事件给那个程序,那么就用PostMessage
如果用
SendMessage(Hwnd, WM_MOUSEMOVE , x, y);
其中
x:wParam,取值范围
MK_CONTROL Set if the ctrl key is down.
MK_LBUTTON Set if the left mouse button is down.
MK_MBUTTON Set if the middle mouse button is down.
MK_RBUTTON Set if the right mouse button is down.
MK_SHIFT Set if the shift key is down.
y:lParam,鼠标的坐标
xPos = LOWORD(lParam); //horizontal position of cursor
yPos = HIWORD(lParam); //vertical position of cursor
你就可以在程序里重载虚函数LRESULT CALLBACK WindowProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
switch(message)
{
case WM_MOUSEMOVE:
//写你要在鼠标移动时做的处理
break;
.....
}
CDialog::WindowProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
}
CDialog::WindowProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);是MFC自己的处理函数,最好加上,不然会很麻烦
很简单的SendMessage和postmessage一样使用方法
我之前给别人写过一个打地鼠的小游戏,就是用鼠标操作的。发给你参考下。代码很简单。
主要函数我在下方说明了,但更多信息你必须自己百度看,一言两语说不完。
SetConsoleCursorPosition函数可以定位光标位置,也就是文字内容显示的起点。
ReadConsoleInput(HANDLE hConsoleInput,*INPUT_RECORD lpBuffer,DWORD nLength,
DWORD lpNumberOfEventsRead)函数可以获取鼠标的操作信息。
调用过上面函数后,lpBuffer由于是传址,所以其地址里的值就包含了鼠标信息。
lpBuffer.EventType == MOUSE_EVENT //判断是鼠标事件
lpBuffer.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED//判断是鼠标左键点击
两个判断一起就是鼠标左键点击的判断,其他事件参数你可以自己网上找。
#include stdio.h
#include stdlib.h
#include windows.h
#include conio.h
#include time.h
#include string.h
#include malloc.h
#define gSizek 30//区域大小宽度
#define gSizeg 20//区域大小高度
#define gBegin 3//活动区域起始行
int main()
{
int t=0,s0,s1,i,j,count=0,fen=0,row=0,clo=0;
char gameA[gSizeg][gSizek+1],fSave[10]={0};
SetConsoleTitle("打地鼠");
HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE); // 获取标准输入设备句柄
INPUT_RECORD inRec;
DWORD res;
COORD p0;
p0.X=0;
p0.Y=0;
srand(time(0));
s0= time(NULL);
strcpy(gameA[0]," GAME");
strcpy(gameA[1],"未命中次数:0,计分:000000");
for(i=gBegin-1;igSizeg;i++)
{
for(j=0;jgSizek+1;j++)
{
if(igBegin-1 igSizeg-1 j0 jgSizek-1)
gameA[i][j]=' ';
else
gameA[i][j]=4;
if(j==gSizek)
gameA[i][j]=0;
}
}
for(i=0;igSizeg;i++)
printf("%s\n",gameA[i]);
while (1)
{
if(t=3)
{
if(row0 clo0)
gameA[row][clo]=' ';
row=rand()%(gSizeg-1);
clo=rand()%(gSizek-1);
s0= time(NULL);
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),p0);
if(row3)
row=4;
if(clo1)
clo=1;
gameA[row][clo]=2;
for(i=0;igSizeg;i++)
{
//gameA[i][gSizeg+1]=0;
printf("%s\n",gameA[i]);
}
}
if(count==3)
{
p0.X=10;
p0.Y=8;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),p0);
printf("GAME OVER!");
break;
}
s1= time(NULL);
t=s1-s0;
ReadConsoleInput(hInput, inRec, 1, res);
if (inRec.EventType == MOUSE_EVENT inRec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED) //鼠标左键
{
if(inRec.Event.MouseEvent.dwMousePosition.X==clo inRec.Event.MouseEvent.dwMousePosition.Y==row)
{
if(fen==0)
fen=1;
if(fen999999)
fen=999999;
else
fen=fen*2;
sprintf(fSave,"%06d",fen);
gameA[1][18]=0;
strcat(gameA[1],fSave);
}
else
{
count++;
fen=0;
gameA[1][11]=count+'0';
}
t=4;
}
}
while(1);
return 0;
}
#include stdio.h
int main (void)
{
SetCursorPos(1,1);
return 0;
}
#include Windows.h
#include stdio.h
#include process.h
VOID MyPlaySound(LPVOID lp)
{
unsigned FREQUENCY[]={392,392,440,392,523,494,
392,392,440,392,587,523,
392,392,784,659,523,494,440,
698,698,659,523,587,523,
392,392,440,392,523,494,
392,392,440,392,587,523,
392,392,784,659,523,494,440,
698,698,659,523,587,523};
unsigned DELAY[]={375,125,500,500,500,1000,
375,125,500,500,500,1000,
375,125,500,500,500,500,1000,
375,125,500,500,500,1000,
375,125,500,500,500,1000,
375,125,500,500,500,1000,
375,125,500,500,500,500,1000,
375,125,500,500,500,1000};
int CIRCLE;
for(CIRCLE=0;CIRCLE50;CIRCLE++)
{
Beep(FREQUENCY[CIRCLE],DELAY[CIRCLE]);
}
}
VOID MyPrint(LPVOID lp)
{
int i;
char a[10][4]=;
for(i=0;i10;i++)
{
printf("%s",a[i]);
Sleep(500);
}
}
int main()
{
_beginthread(MyPlaySound, 0, NULL);
_beginthread(MyPrint, 0, NULL);
Sleep(20000);
return 0;
}
编译之前记得把项目属性里改成多线程方案的.不然会出错.我是在VS2010下编译的.