سؤال


I was wondering whether you can convert a handle to a window "HWND". I need to call the "PostMessage" function using the "FindWindow" method.

I currently have to source

HANDLE mainProcess;
BOOL APIENTRY ATTACH_PROCESS(int ProcessID)
{

    mainProcess = OpenProcess(PROCESS_ALL_ACCESS, true, ProcessID);

    return TRUE;
}
BOOL APIENTRY SEND_INPUT(/*NOT USED FOR THIS SAMPLE*/ const char* String, bool Keydown)
{

    int ToDo = WM_KEYUP;
    if (Keydown)
        ToDo = WM_KEYDOWN;
    return PostMessage((HWND)mainProcess, ToDo, VK_TAB, NULL); 
}
هل كانت مفيدة؟

المحلول

call GetProcessId() using the mainProcess handle to get the ProcessID.

call EnumWindows()

For Each Window, call GetWindowThreadProcessId() to get the ProcessId of the process associated with the window.

Compare the ProcessID's, if they match -- you've found the HWND you want.

This is a somewhat expensive task, so best to find the hwnd you want upfront and just store it.

نصائح أخرى

No. A process can create multiple windows. Since there does not exist a 1-to-1 mapping, such a function would not make sense.

On the other hand, it is certainly possible to have a function which returns a list of windows created by a process.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top