首页 > 解决方案 > 如何在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.");
}
 }

标签: cwinapi

解决方案


正如您所包含的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 = 1919y = 1079)。

但是,如果将来您想自己输入绝对归一化坐标(表示为相对数字,从 0 到65535,对于xy轴),只需注释掉上面提到的行。

您可以在此处阅读有关 Windows 坐标系的更多信息。


推荐阅读