WndProc 没有可见的形式?
我想在第二个线程上创建一个表单,该线程将在其 WndProc 方法中接收消息。 创建这样的隐形表单的推荐方法是什么? 设置“ShowInTaskbar = false”和“Visible = false”是否足够,或者是否有“更干净”的方法?
I want to create a form on a second thread that will receive messages in it's WndProc method. What is the recommended way to create an invisible form like this? Is setting "ShowInTaskbar=false" and "Visible=false" enough, or is there a "cleaner" way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不确定你所说的“清洁工”是什么意思。 创建用户不可见的表单的标准方法是将 Visible 和 ShowInTaskbar 设置为 false。
I'm not sure what you mean by "cleaner". The standard way to create a form that is invisible to the user is to set Visible and ShowInTaskbar to false.
据我所知,你所做的事情是违反规则的(尽管这些东西有时确实有效)......所有 UI 内容都应该在主线程中。
至于你的问题,你应该能够将 Visible 设置为 false 。 我相信,设置 ShowInTaskbar 仅对主应用程序表单很重要,如果您这样做,用户将无法从任务栏返回您的应用程序。
As far as I know, what you're doing is against the rules (although, these things do work sometimes) ... all UI stuff is supposed to be in the main thread.
As for your question, you should be able to just set Visible to false. Setting ShowInTaskbar would only be important for the main application form, I believe, and if you did that users wouldn't have a way to get back to your app from the taskbar.
该项目中的代码有一个巧妙的方法来处理这个问题:http://www. codeproject.com/KB/dotnet/XDMessaging.aspx
我不会在这里复制代码,因为它是受版权保护的,但您可以自行下载以供参考。
XDListener 类扩展了 NativeWindow,因此它能够接收消息。 在构造函数中,在窗口上设置了一些属性,使其不可见。 该类中重写方法 WndProc 以处理到达的消息。 我测试了这段代码可以在 Win 7 上使用 C# 应用程序运行,而我的所有表单都隐藏并且没有显示在任务栏中。
The code in this project has a neat way of dealing with this problem: http://www.codeproject.com/KB/dotnet/XDMessaging.aspx
I'm not copying the code here because it's copyrighted, but you can use it for reference by downloading it yourself.
The XDListener class extends NativeWindow, so it is able to receive messages. In the constructor some attributes are set on the window which make it invisible. The method WndProc is overridden in that class to process messages that arrive. I tested that this code works on Win 7 with a C# app while all my Forms are all hidden and not being displayed in the TaskBar.
如果将 ShowInTaskbar 设置为 false,您将无法接收 Windows 消息。 唯一的方法是隐藏表单(它仍然隐藏任务栏图标)。
You won't be able to receive windows messages if you set your ShowInTaskbar to false. The only way to do it is by hiding your form instead (it still hides the taskbar icon).
使用:
或
注意:您可以在声明中将
lpClassName
或lpWindowName
更改为string
或long
确保使用“Static”作为
lpClassName
成员 - 这是您想要的不可见窗口样式,通常用于接收 wndproc 窗口。此 api 的返回值是 hwnd(handle),您可以对其进行子类化以获取消息
它将接收所有标准 Windows 消息,或者您可以向其发送自定义消息,因此当收到标准消息时,它不会意外触发您的代码。
例子:
hwnd_main
是其父窗口的 hwnd 子类,玩得开心!
use:
or
note: you can change the
lpClassName
orlpWindowName
tostring
orlong
in the declarationmake sure to use "Static" as the
lpClassName
member - this is the invisible window style you want that is usually used for recieve wndproc windows.the return value from this api is the hwnd(handle) that you can subclass to get the messages
it will receive all standard windows messages or you can send custom messages to it, so it won't fire your code accidentally when a standard message is received.
example:
hwnd_main
is the hwnd of its parent windowsubclass this and have fun!