从HWND到控制 [英] From HWND to control

查看:48
本文介绍了从HWND到控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用user32.dll中的FindWindow方法来查找窗口并获取句柄,但是是否可以从句柄获取表单控件?并像普通形式一样使用它?示例:

Im using the FindWindow method from user32.dll to find a window and get the handle, but is it possible to get the form control from the handle? and the use it like an ordinary form? Example:

int myhwnd = FindWindow(null, "MyWindow");
form myform = SomeMagic.GetFormFromHandle(myhwnd);
myform.Visible = false;

还是我必须继续使用user32.dll中的方法向窗口发送消息?

or do I have to continue to use the methods in user32.dll to send a message to the window?

推荐答案

如果它是一个托管窗口(您已经使用System.Windows.Forms创建了它,并且它是从System.Windows.Forms.Control继承的),则可以使用

If it's a managed window (you've created it with System.Windows.Forms, and it has inherited from System.Windows.Forms.Control) you can get a reference to the Control object with

Control.FromHandle(myIntPtr);

然后,您只需要获得控件的父级,直到获得表单即可.

Then you just get the parent of the control until you get the form.

如果没有,您将无法获得Control对象,但是您可以做的是创建一个NativeWindow,然后使用AssignHandle将IntPtr句柄分配给该对象.至少可以使您对WndProc有所访问,并且类似,但是仅此而已.

If not, you can't get a Control object, what you can do though is to is to create a NativeWindow, and assign the IntPtr handle to the object with the AssignHandle. That will at least give you some access to the WndProc, and similar, but that's it.

这篇关于从HWND到控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆