将窗口句柄 (HWND) 绑定到非托管代码中的类名/组件类型
我想通过知道窗口句柄来找出窗口的顶级组件名称。
这是在托管 C++ 代码中完成的:
//handle is the window handle as int
System::Windows::Forms::Control^ c = Control::FromHandle((System::IntPtr)System::Convert::ToInt32(handle));
System::Type^ t= c->GetType();
Console::WriteLine(t->FullName);//This is the top level name of the component.
但是,我不能将托管代码用于我必须开发的解决方案。
我尝试使用 GetClassName() 作为等效项,但这只是给了我 WindowsForms10.STATIC。 [...] 胡言乱语:)
有谁知道如何在非托管代码中完成此操作?
我知道 C++ 本身并不提供对 WinForms 的任何支持,但我希望以正确的方式获得指针。我已经在一些解决方案中看到了它的完成,但无法让我的代码工作:(
先感谢您。
I would like to find out the top level component name of a window from knowing its window handle.
This is done like so in managed C++ code:
//handle is the window handle as int
System::Windows::Forms::Control^ c = Control::FromHandle((System::IntPtr)System::Convert::ToInt32(handle));
System::Type^ t= c->GetType();
Console::WriteLine(t->FullName);//This is the top level name of the component.
However, I cannot use managed code for the solution that I have to develop.
I have tried to use GetClassName()
as an equivalent, but this just gives me WindowsForms10.STATIC. [...]
mumbo jumbo :)
Does anyone have any idea how this can be done in unmanaged code?
I know that C++ does not natively offer any support for WinForms, but I am hoping to get a pointer in the right way. I've seen it done in some solutions, but have been unable to get my code working :(
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能就是 WinForms 代码正在做的事情:
要在本机 Win32 和 C++ 中执行此操作,请创建一个接口类,例如:
然后从中派生控件:
然后在控件构造函数中:
最后,给出一个窗口句柄:
This is probably what the WinForms code is doing:
SetWindowLongPtr (handle, GWL_USERDATA, value)
to store a reference to the object owning the window.GetWindowLongPtr (handle, GWL_USERDATA)
to retrieve the managed object reference which you can then do managed stuff with (GetType(), etc)To do this in native Win32 and C++, create an interface class like:
and then derive controls from it:
and then in the control constructor:
and finally, given a window handle: