如何杀死一个窗口?
当我单击 ESRI COM 工具栏项时,它会生成一个窗口 - 它看起来像一个 Winform。由于我没有直接生成窗口,因此我不能只对其执行 Object.Close() 。我可以使用什么技术来删除我的应用程序生成的窗口,但我没有该窗口的对象引用?
When I click on an ESRI COM toolbar item, it spawns a window - it looks like a Winform. Since I didn't spawn the window directly, I can't just do an Object.Close() on it. What technique can I use to delete a window my application spawned, but that I don't have an object reference of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为最简单的方法是使用 p/invoke。
最简单的方法:
使用 FindWindow() 函数获取该窗口的 HWND(在 C# 中的 IntPtr 中,您可以使用 NativeWindow 类作为包装器 - http://msdn.microsoft.com/en-us/library/system.windows.forms.nativewindow.aspx#Y114)
一旦您有了 HWND,您可以使用 CloseWindow() 关闭窗口或使用 SendMessage(youHWND, WM_CLOSE, IntPtr.Zero, IntPtr.Zero) (WM_CLOSE = 0x0010) 向窗口发送消息。
如果你的窗口有一个父窗口(你可以使用spy++来找出它),你可以使用FindWindowEx()以更精确的方式找到你的窗口。
希望有帮助!
祝你好运!
ps
只是为了真正确保您不会意外地意外杀死另一个应用程序的窗口(如果您使用 FindWindow 或 FindWindowEx 而没有父级),您可以使用 GetWindowThreadProcessId() 来确保该窗口属于您的进程!
I think the easiest way is using p/invoke.
The easiest way:
Use FindWindow() function to get and HWND for that window (in C# its IntPtr, you can use NativeWindow class as a wrapper - http://msdn.microsoft.com/en-us/library/system.windows.forms.nativewindow.aspx#Y114)
Once you have the HWND you can use CloseWindow() to close the window or send a message to the window useing SendMessage(youHWND, WM_CLOSE, IntPtr.Zero, IntPtr.Zero) (WM_CLOSE = 0x0010).
If your window has a parent (you can use spy++ to find that out) you can find your window in a more precise way using FindWindowEx().
Hope it helps!
Good luck!
p.s.
Just to be REALLY sure you're not by accident killing another application's window unexpectedly (if you use FindWindow or FindWindowEx without a parent) you can use GetWindowThreadProcessId() to make sure that the window belongs to your process!
假设您没有窗口句柄,您可以与 Win32 互操作并执行以下操作:
在某些方法调用中:
然后它将调用以下命令来查找窗口并关闭它,只需将
调用Win32的信息可以在MSDN或者pinvoke.net中找到
Assuming you don't have the window handle you could interop to Win32 and do the following:
In some method call:
Then it will call the following to find the window and close it, just replace <title> with as descriptive a title for the window as you can to prevent closing of windows that were not intended to be closed.
The information for calling Win32 can be found in MSDN or pinvoke.net