将窗口句柄 (HWND) 绑定到非托管代码中的类名/组件类型

发布于 2024-10-07 13:01:33 字数 568 浏览 7 评论 0原文


我想通过知道窗口句柄来找出窗口的顶级组件名称。
这是在托管 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泡沫很甜 2024-10-14 13:01:33

这可能就是 WinForms 代码正在做的事情:

  1. 创建窗口时,使用 SetWindowLongPtr (handle, GWL_USERDATA, value) 来存储对拥有该窗口的对象的引用。
  2. Control::FromHandle 调用 GetWindowLongPtr (handle, GWL_USERDATA) 来检索托管对象引用,然后您可以使用该引用执行托管操作(GetType() 等)

要在本机 Win32 和 C++ 中执行此操作,请创建一个接口类,例如:

class IControl
{
public:
  virtual const string &GetTypeName () = 0;
};

然后从中派生控件:

class TextBoxControl : public IControl
{
  virtual const string &GetTypeName () { return "TextBox"; }
}

然后在控件构造函数中:

TextBoxControl::TextBoxControl ()
{
   handle = CreateWindowEx (parameters to create a text box);
   SetWindowLongPtr (handle, GWL_USERDATA, this);
}

最后,给出一个窗口句柄:

string GetWindowTypeName (HWND handle)
{
  IControl *control = GetWindowLongPtr (handle, GWL_USERDATA);
  return control->GetTypeName ();
}

This is probably what the WinForms code is doing:

  1. When a window is created, use SetWindowLongPtr (handle, GWL_USERDATA, value) to store a reference to the object owning the window.
  2. The Control::FromHandle calls 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:

class IControl
{
public:
  virtual const string &GetTypeName () = 0;
};

and then derive controls from it:

class TextBoxControl : public IControl
{
  virtual const string &GetTypeName () { return "TextBox"; }
}

and then in the control constructor:

TextBoxControl::TextBoxControl ()
{
   handle = CreateWindowEx (parameters to create a text box);
   SetWindowLongPtr (handle, GWL_USERDATA, this);
}

and finally, given a window handle:

string GetWindowTypeName (HWND handle)
{
  IControl *control = GetWindowLongPtr (handle, GWL_USERDATA);
  return control->GetTypeName ();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文