Simulate click into a hidden window

六眼飞鱼酱① 提交于 2019-11-27 03:40:40

问题


I've got a C# problem,

I am able to simulate a click to the current window, but I would like to do it if the window is minimized or hidden.

Any ideas?


回答1:


Here is a fully working snippet which will give you the window handle, target sub window and post a message to that subwindow.

#include "TCHAR.h"
#include "Windows.h"

int _tmain(int argc, _TCHAR* argv[])
{
    HWND hwndWindowTarget;
    HWND hwndWindowNotepad = FindWindow(NULL, L"Untitled - Notepad");
    if (hwndWindowNotepad)
    {
        // Find the target Edit window within Notepad.
        hwndWindowTarget = FindWindowEx(hwndWindowNotepad, NULL, L"Edit", NULL);
        if (hwndWindowTarget) {
            PostMessage(hwndWindowTarget, WM_CHAR, 'G', 0);
        }
    }

    return 0;
}

At the moment it will send the G character to notepad "Untitled" (open a new notepad, do nothing.

You can find the sub-window using spy++ which comes with visual studio.

Here is an example using SendInput to send mouse events:

#include "TCHAR.h"
#include "Windows.h"

int _tmain(int argc, _TCHAR* argv[])
{
    POINT pt;
    pt.x = 300;
    pt.y = 300;

    HWND hwndWindowTarget;
    HWND hwndWindowNotepad = FindWindow(NULL, L"Untitled - Notepad");
    if (hwndWindowNotepad)
    {
        // Find the target Edit window within Notepad.
        hwndWindowTarget = FindWindowEx(hwndWindowNotepad, NULL, L"Edit", NULL);
        if (hwndWindowTarget) {
            PostMessage ( hwndWindowTarget, WM_RBUTTONDOWN, 0, (pt.x) & (( pt.y) << 16) );
            PostMessage ( hwndWindowTarget, WM_RBUTTONUP, 0, (pt.x ) & (( pt.y) << 16) );
        }
    }

return 0;

}



来源:https://stackoverflow.com/questions/10279812/simulate-click-into-a-hidden-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!