C++ 如何在win32中获得HWND?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4320417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:06:55  来源:igfitidea点击:

How to get HWND in win32?

c++winapi

提问by Ramilol

Is there way to get the HWND handler of my window?
I'm using win32.

有没有办法获得我的窗口的 HWND 处理程序?
我正在使用 win32。

回答by Mark Ransom

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.

您可以调用 GetActiveWindow 来获取应用程序中的活动控件,然后在返回的句柄上重复调用 GetParent,直到它返回 NULL。您获得的最后一个有效句柄应该是主窗口的句柄。

The easier way as someone else said is to store the returned value from CreateWindow somewhere.

正如其他人所说,更简单的方法是将 CreateWindow 的返回值存储在某处。

回答by MSalters

It's probably good to understand why there is no simple way. It all boils down to "whichwindow?". 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 MessageBoxis a window.

理解为什么没有简单的方法可能很好。这一切都归结为“哪个窗口?”。现在,您的屏幕上可能会显示多个窗口。例如,屏幕底部的任务栏是一个窗口。甚至您自己的应用程序通常也不止一个。例如,“文件保存”对话框就是一个窗口。即使是简单的MessageBox也是一个窗口。

So, how do you identify whichwindow 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, exceptthe HWNDitself ! It makes sense to ask the X/Y position of HWND(0x5e21), but it's stupid to ask which HWNDbelongs to HWND(0x5e21).

那么,你如何确定哪些你所谈论的窗口?常见的答案是您通过它们的HWND. 因此,要获得“文件保存”对话框窗口的位置,您需要询问与该 HWND 关联的位置。显然,您可以通过这种方式获得任何属性,除了HWND本身!问 的 X/Y 位置是有意义的HWND(0x5e21),但问哪个HWND属于是愚蠢的HWND(0x5e21)

Now, it may happen that you have another more-or-less unique property and you want to get the HWNDfrom that. For instance, you may have an X/Y position. In that case, WindowFromPoint(xy)will return the HWNDat that position.

现在,您可能有另一个或多或少的独特属性,并且您想从中获取HWND。例如,您可能有一个 X/Y 位置。在这种情况下,WindowFromPoint(xy)将返回该HWND位置的 。

But the most common case is that you need to react to a Windows message for your window. In that case, you get the HWNDof your window as the first argument of your WindowProc().

但最常见的情况是您需要对窗口的 Windows 消息做出反应。在这种情况下,您HWND将窗口的 用作WindowProc().

So, unless you tell us what unique information you do have, we can't tell you how to find the matching HWND.

因此,除非您告诉我们您拥有哪些独特的信息,否则我们无法告诉您如何找到匹配的HWND.

回答by In silico

Didn't you create your window via CreateWindow()or CreateWindowEx()? The CreateWindowEx() functionand the CreateWindow()function both return the HWNDof the newly created window.

您不是通过CreateWindow()或 来创建窗口的CreateWindowEx()吗?该CreateWindowEx()函数CreateWindow的()函数都返回HWND新创建的窗口。

Also, the operating system passes you the HWNDof 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.

此外,操作系统通过您的窗口过程将您的HWND窗口传递给。这不是您调用的函数;它是操作系统调用的一个函数,让您的应用程序进行任何需要的处理。