C++ Handle as HWND?

淺唱寂寞╮ 提交于 2019-12-11 14:49:58

问题



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

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