4

C++スクリプトでマウスカーソルを動かしたい。Parallels内のWindows7でVisualC++ 2010 Expressを使用しており、コンソールアプリケーションを作成しました。

SetCursorPosメソッドは知っていますが、機能していません(何もしません)。

SendInputを使用してクリックをシミュレートできましたが、実際にはマウスが移動しません。

これは私のコードです:

#include <Windows.h>
#include <Tlhelp32.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <time.h>

void mouseLeftClick(const int x, const int y);

// window
HWND hWindow;

int main()
{
    // find window
    hWindow = FindWindow(NULL, "Calculadora");

    if (NULL == hWindow) {
        OutputDebugStringA("Couldn't find application.");
    }else{

        if (!SetForegroundWindow(hWindow)) {
            OutputDebugStringA("Couldn't set application to foreground.");
        }else{
            // click on 1
            mouseLeftClick(20 265));
            Sleep(500);
            // click on 2
            mouseLeftClick(60, 265);
            Sleep(500);
        }
    }
    return 0;
}

void mouseLeftClick(const int x, const int y)
{ 
    // get the window position
    RECT rect;
    GetWindowRect(hWindow, &rect);

    // calculate scale factor
    const double XSCALEFACTOR = 65535 / (GetSystemMetrics(SM_CXSCREEN) - 1);
    const double YSCALEFACTOR = 65535 / (GetSystemMetrics(SM_CYSCREEN) - 1);

    // get current position
    POINT cursorPos;
    GetCursorPos(&cursorPos);
    double cx = cursorPos.x * XSCALEFACTOR;
    double cy = cursorPos.y * YSCALEFACTOR;

    // calculate target position relative to application
    double nx = (x + rect.left) * XSCALEFACTOR;
    double ny = (y + rect.top) * YSCALEFACTOR;

    INPUT Input={0};
    Input.type = INPUT_MOUSE;

    Input.mi.dx = (LONG)nx;
    Input.mi.dy = (LONG)ny;

    // set move cursor directly and left click
    Input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;

    SendInput(1,&Input,sizeof(INPUT));
}
4

2 に答える 2

6

これは、SmartMouseがオンまたは自動であるためにParallelsで発生します。Parallels VMのプログラムでマウスを制御するには、SetCursorPos最初にカーソルを非表示にする必要があります。ShowCursor(0);たとえば、マウスを動かす前に、これを行うことができますSetCursorPos。これで、SmartMouseが自動またはオフに設定されているときにマウスを制御できるようになります。

于 2012-11-05T21:44:39.987 に答える
2

問題を見つけました。Parallelsには、OSXとWindowsの間を自由に移動できるSmartMouseと呼ばれる機能があることがわかりました。非アクティブ化すると、マウスが期待どおりに移動しました。

于 2012-07-01T15:44:22.087 に答える