如何从桌面句柄获取 Form 对象?
我想知道是否可以获取桌面的Form。我尝试从桌面获取 hWnd 并使用 Form.FromHandle 获取表单。但它总是返回 null。所以我认为这是不可能的;如果可能的话有人可以给我看一个示例代码吗? 这是下面不起作用的代码:
hWnd = GetDesktopWindow();
desktop = Form.FromHandle(hWnd) as Form;
System.Diagnostics.Debugger.Break();
PS 有人也可以解释一下我在这里做错了什么吗?
I want to know if it is possible to acquire the desktop's Form. I have tried to get the hWnd from the desktop and use Form.FromHandle to get the form. But it always returns null. So I assume this is not possible; if it is possible can someone show me an example code.
Here is the code that did not work below:
hWnd = GetDesktopWindow();
desktop = Form.FromHandle(hWnd) as Form;
System.Diagnostics.Debugger.Break();
P.S. Can someone also explain what I did wrong here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法执行此操作,因为桌面窗口不是
表单
。FromHandle()
尝试查找与给定窗口句柄相对应的托管Control
(在本例中为Form
)。由于不存在这样的Control
,因此它返回null
。You can't do this because the desktop window isn't a
Form
.FromHandle()
tries to find the managedControl
(in this case aForm
) that corresponds to the given window handle. Since no suchControl
exists, it returnsnull
.