如何在c语言中编写用鼠标点击输入
答案:2 悬赏:0 手机版
解决时间 2021-12-01 04:33
- 提问者网友:人傍凄凉立暮秋
- 2021-11-30 09:34
如何在c语言中编写用鼠标点击输入
最佳答案
- 五星知识达人网友:一秋
- 2021-11-30 10:53
需要mouse.h这个库函数,你百度一下吧,在c的图形化编程中用到 不过用c而不是vc编写这个比较繁琐,呵呵
全部回答
- 1楼网友:神也偏爱
- 2021-11-30 12:30
//本函数ismouse的作用是检测鼠标,直到有单击后退出函数,单击的位置存在pt里
pt.X和pt.Y就是他的横竖坐标啦
#include
COORD pt;
int ismouse()
{
DWORD dwMode, dwOldMode;
DWORD dwRead;
int mouse_pos;
UINT count;
INPUT_RECORD ir[128];
CONSOLE_CURSOR_INFO cci, cciOld;
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE), hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if(!GetConsoleMode(hStdIn, &dwOldMode))
{
fprintf(stderr, "GetConsoleMode 错误:%d\n", GetLastError());
return -1;
}
dwMode = dwOldMode;
dwMode &= ~(ENABLE_QUICK_EDIT_MODE);
dwMode |= ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
if(!SetConsoleMode(hStdIn, dwMode))
{
fprintf(stderr, "SetConsoleMode 错误:%d\n", GetLastError());
return -1;
}
GetConsoleCursorInfo(hStdOut, &cciOld);
cci.bVisible = TRUE;
cci.dwSize = 100;
if(!SetConsoleCursorInfo(hStdOut, &cci))
{
fprintf(stderr, "SetConsoleCursorInfo 错误:%d\n", GetLastError());
return -1;
}
while(1)
{
if(!ReadConsoleInput(hStdIn, &ir[0], 128, &dwRead))
{
fprintf(stderr, "ReadConsoleInput 错误:%d\n", GetLastError());
return -1;
}
for(count=0; count {
switch(ir[count].EventType)
{
case MOUSE_EVENT:
switch(ir[count].Event.MouseEvent.dwEventFlags)
{
case 0:
if(ir[count].Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
{
SetConsoleMode(hStdIn, dwOldMode);
SetConsoleCursorInfo(hStdOut, &cciOld);
mouse_pos = ir[count].Event.MouseEvent.dwMousePosition.X;
mouse_pos = mouse_pos << 16 | ir[count].Event.MouseEvent.dwMousePosition.Y;
return mouse_pos;
}
break;
case MOUSE_MOVED:
pt.X = ir[count].Event.MouseEvent.dwMousePosition.X;
pt.Y = ir[count].Event.MouseEvent.dwMousePosition.Y;
SetConsoleCursorPosition(hStdOut, pt);
break;
}
break;
default:
break;
}
}
}
}
pt.X和pt.Y就是他的横竖坐标啦
#include
COORD pt;
int ismouse()
{
DWORD dwMode, dwOldMode;
DWORD dwRead;
int mouse_pos;
UINT count;
INPUT_RECORD ir[128];
CONSOLE_CURSOR_INFO cci, cciOld;
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE), hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if(!GetConsoleMode(hStdIn, &dwOldMode))
{
fprintf(stderr, "GetConsoleMode 错误:%d\n", GetLastError());
return -1;
}
dwMode = dwOldMode;
dwMode &= ~(ENABLE_QUICK_EDIT_MODE);
dwMode |= ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
if(!SetConsoleMode(hStdIn, dwMode))
{
fprintf(stderr, "SetConsoleMode 错误:%d\n", GetLastError());
return -1;
}
GetConsoleCursorInfo(hStdOut, &cciOld);
cci.bVisible = TRUE;
cci.dwSize = 100;
if(!SetConsoleCursorInfo(hStdOut, &cci))
{
fprintf(stderr, "SetConsoleCursorInfo 错误:%d\n", GetLastError());
return -1;
}
while(1)
{
if(!ReadConsoleInput(hStdIn, &ir[0], 128, &dwRead))
{
fprintf(stderr, "ReadConsoleInput 错误:%d\n", GetLastError());
return -1;
}
for(count=0; count
switch(ir[count].EventType)
{
case MOUSE_EVENT:
switch(ir[count].Event.MouseEvent.dwEventFlags)
{
case 0:
if(ir[count].Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
{
SetConsoleMode(hStdIn, dwOldMode);
SetConsoleCursorInfo(hStdOut, &cciOld);
mouse_pos = ir[count].Event.MouseEvent.dwMousePosition.X;
mouse_pos = mouse_pos << 16 | ir[count].Event.MouseEvent.dwMousePosition.Y;
return mouse_pos;
}
break;
case MOUSE_MOVED:
pt.X = ir[count].Event.MouseEvent.dwMousePosition.X;
pt.Y = ir[count].Event.MouseEvent.dwMousePosition.Y;
SetConsoleCursorPosition(hStdOut, pt);
break;
}
break;
default:
break;
}
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯