问题
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);
}
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/10436918/c-handle-as-hwnd