walklang / uilib

A simply and powerful ui script framework library. via http://www.uilib.cn
Apache License 2.0
45 stars 7 forks source link

向窗口发送ESC #17

Open suhao opened 1 year ago

suhao commented 1 year ago

在Windows系统的界面编程中,浏览器编程是一种热门技术。

内嵌一个浏览器窗口并全屏播放,如果浏览器窗口在A屏,此时在B屏执行相关的窗口操作,此时可能浏览器未正常退出全屏,我们可以向窗口发送ESC命令。

    POINT position = {0};
    ::GetCursorPos(&position);

    CRect full_rect;
    ::GetWindowRect(full_sreen_hwnd_, &full_rect);

    INPUT input[2];
    memset(input, 0, sizeof(input));

    input[0].type = INPUT_MOUSE;
    input[0].mi.dx =
        static_cast<long>(65535.0f / (GetSystemMetrics(SM_CXSCREEN) - 1) *
                          (full_rect.left + 100));
    input[0].mi.dy = static_cast<long>(
        65535.0f / (GetSystemMetrics(SM_CYSCREEN) - 1) * (full_rect.top + 100));
    input[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE |
                          MOUSEEVENTF_MIDDLEDOWN | MOUSEEVENTF_MIDDLEUP;

    input[1].ki.wVk = VK_ESCAPE;
    input[1].type = INPUT_KEYBOARD;
    input[1].ki.wScan =
        static_cast<BYTE>(MapVirtualKey(VK_ESCAPE, MAPVK_VK_TO_VSC));

    ::SendInput(2, input, sizeof(INPUT));

    ::SetCursorPos(position.x, position.y);

其中,full_sreen_hwnd_可以通过OnActivateApp消息时取GetForegroundWindow,然后判断与浏览器同进程、同线程等约束条件而得。