How to get HWND in win32?
Is there way to get the HWND handler of my window?
I'开发者_C百科m using win32.You could call GetActiveWindow to get the active control in your application, then repeatedly call GetParent on the returned handle until it returns NULL. The last valid handle you get should be the handle of your main window.
The easier way as someone else said is to store the returned value from CreateWindow somewhere.
It's probably good to understand why there is no simple way. It all boils down to "which window?". You'll likely have multiple windows visible on your screen, right now. For instance, the taskbar at the bottom of your screen is a window. even your own app typically has more than one. For instance, the "File Save" dialog is a window. Even the simple MessageBox
is a window.
So, how do you identify which window you're talking about? The common answer is that you identify them by their HWND
. So, to get the position of the "File Save" dialog window, you ask for the position associated with that HWND. Obviously, you can get any property that way, except the HWND
itself ! It makes sense to ask the X/Y position of HWND(0x5e21)
, but it's stupid to ask which HWND
belongs to HWND(0x5e21)
.
Now, it may happen that you have another more-or-less unique property and you want to get the HWND
from that. For instance, you may have an X/Y position. In that case, WindowFromPoint(xy)
will return the HWND
at that position.
But the most common case is that you need to react to a Windows message for your window. In that case, you get the HWND
of your window as the first argument of your WindowProc()
.
So, unless you tell us what unique information you do have, we can't tell you how to find the matching HWND
.
Didn't you create your window via CreateWindow()
or CreateWindowEx()
? The CreateWindowEx() function and the CreateWindow() function both return the HWND
of the newly created window.
Also, the operating system passes you the HWND
of your window(s) via your window procedure. It's not a function that you call; it's a function that the operating system calls to let your application do any processing that's needed.
精彩评论