c - 如何在c中输入点击?
问题描述
我制作了一个非常简单的程序,它会在我选择当前的课程后自动打开我的 webex 链接。然后它将光标定位到确切的位置,以便我可以“按加入会议”。但是我不知道如何将自动点击集成到我的程序中,所以我不必手动点击(左键)。我怎样才能做到这一点?你还有什么建议可以让我的代码更干净吗?任何答案表示赞赏!PS:我替换了链接和老师的名字
#include <stdio.h>
#include <winuser.h>
int main(){
int number;
printf("1. first lesson \n2. second lesson \n3. third lesson \n4. fourth lesson\n5. fifth lesson\n6. sixth lesson\n7. seventh lesson \n8. eighth lesson\n9. neinth lesson\n10. tenth lesson\n11. eleventh lesson \n12. twelveth lesson \n13. thirteenth lesson\n Please enter a number: ");
scanf("%d",&number);
int ptx=1100;
int pty=980;
if (number==1)
{
system("start link1");
SetCursorPos(ptx, pty);
}
else if (number==2)
{
system ("start link2 ");
SetCursorPos(ptx, pty);
}
else if (number==3)
{
system ("start link3 ");
SetCursorPos(ptx, pty);
}
else if (number==4)
{
system ("start link4");
SetCursorPos(ptx, pty);
}
else if (number==5)
{
system ("start link5");
SetCursorPos(ptx, pty);
}
else if (number==6)
{
system ("start link6");
SetCursorPos(ptx, pty);
}
else if (number==7)
{
system ("start link7 ");
SetCursorPos(ptx, pty);
}
else if (number==8)
{
system ("start link8");
SetCursorPos(ptx, pty);
}
else if (number==9)
{
system ("start link9");
SetCursorPos(ptx, pty);
}
else if (number==10)
{
system ("start link10");
SetCursorPos(ptx, pty);
}
else if (number==11)
{
system ("start link11");
SetCursorPos(ptx, pty);
}
else if (number==12)
{
system ("start link12");
SetCursorPos(ptx, pty);
}
else if (number==13)
{
system ("start link13");
SetCursorPos(ptx, pty);
}
else
{
printf("Invalid number.Please try again.");
}
}
解决方案
正如您所包含的winuser.h
,我假设Windows是您项目的目标,因此您可以使用Windows API来模拟点击,这将允许您使用一种方法,即SendInput,不仅可以执行点击,还可以设置屏幕坐标您希望单击发生的位置,因此您不需要两个单独的函数来设置光标位置并实际执行单击。
当然,此解决方案是仅与 Windows 兼容的解决方案。
此外,最好将if
/else if
语句包装成一个switch
语句,让它看起来更干净。
#include <stdio.h>
#include <windows.h>
void sendClick(int ptx, int pty)
{
//Initializing an array of INPUT
INPUT Inputs[3] = { 0 };
//You need the two lines below in order to convert from screen to absolute coordinates
ptx = MulDiv(ptx, 65535, GetSystemMetrics(SM_CXSCREEN)-1);
pty = MulDiv(pty, 65535, GetSystemMetrics(SM_CYSCREEN)-1);
Inputs[0].type = INPUT_MOUSE;
Inputs[0].mi.dx = ptx; //Your click coordinates (x)
Inputs[0].mi.dy = pty; //Your click coordinates (y)
Inputs[0].mi.dwFlags = MOUSEEVENTF_MOVE; //Moving mouse to position
Inputs[1].type = INPUT_MOUSE;
Inputs[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN; //Left clicking
Inputs[2].type = INPUT_MOUSE;
Inputs[2].mi.dwFlags = MOUSEEVENTF_LEFTUP; //Releasing click
//Sending the inputs
SendInput(3, Inputs, sizeof(INPUT));
}
int main()
{
int number;
printf("1. first lesson \n2. second lesson \n3. third lesson \n4. fourth lesson\n5. fifth lesson\n6. sixth lesson\n7. seventh lesson \n8. eighth lesson\n9. neinth lesson\n10. tenth lesson\n11. eleventh lesson \n12. twelveth lesson \n13. thirteenth lesson\n Please enter a number: ");
scanf("%d", &number);
int ptx = 1100;
int pty = 980;
switch (number)
{
case 1 : system("start link1");
sendClick(ptx, pty);
break;
case 2:
system("start link2 ");
sendClick(ptx, pty);
break;
case 3:
system("start link3 ");
sendClick(ptx, pty);
break;
case 4:
system("start link4");
sendClick(ptx, pty);
break;
case 5:
system("start link5");
sendClick(ptx, pty);
break;
case 6:
system("start link6");
sendClick(ptx, pty);
break;
case 7:
system("start link7 ");
sendClick(ptx, pty);
break;
case 8:
system("start link8");
sendClick(ptx, pty);
break;
case 9:
system("start link9");
sendClick(ptx, pty);
break;
case 10:
system("start link10");
sendClick(ptx, pty);
break;
case 11:
system("start link11");
sendClick(ptx, pty);
break;
case 12:
system("start link12");
sendClick(ptx, pty);
break;
case 13:
system("start link13");
sendClick(ptx, pty);
break;
default:
printf("Invalid number.Please try again.");
break;
}
}
免责声明:请注意该sendClick()
函数如何执行以下操作:
ptx = MulDiv(ptx, 65535, GetSystemMetrics(SM_CXSCREEN)-1);
pty = MulDiv(pty, 65535, GetSystemMetrics(SM_CYSCREEN)-1);
这样做是为了转换按钮的坐标,您可能将其作为屏幕坐标,即相对于屏幕尺寸的坐标。(对于 1920x1080 屏幕,右下角为x = 1919
和y = 1079
)。
但是,如果将来您想自己输入绝对归一化坐标(表示为相对数字,从 0 到65535
,对于x
和y
轴),只需注释掉上面提到的行。
您可以在此处阅读有关 Windows 坐标系的更多信息。
推荐阅读
- ionic4 - Stenciljs:路由到新内容并且不能更改背景
- java - 在java中使用RC4加密图像
- c# - 发送多个 httpclient 或 httpresponse 时 UI 挂起
- python - 如果进程(文件)由外部程序打开,如何使用 Python 关闭它?
- c# - UWP 如何使用 AudioGraph API 暂停录音?
- r - 如何选择 R 数据框中两列的所有唯一组合?
- node.js - 是否可以在语音通道中映射具有某些权限的人?不和谐.js
- google-data-studio - 对两个不同的数据源使用相同的过滤器控件
- javascript - 带有文本和值下拉列表的 Kendo UI 网格过滤器
- c# - 如何读取文件中的数据并将行保存到二维表中