WndProc 没有可见的形式?

发布于 2024-07-19 17:38:04 字数 130 浏览 9 评论 0原文

我想在第二个线程上创建一个表单,该线程将在其 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 技术交流群。

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

发布评论

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

评论(5

层林尽染 2024-07-26 17:38:04

我不确定你所说的“清洁工”是什么意思。 创建用户不可见的表单的标准方法是将 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.

慵挽 2024-07-26 17:38:04

据我所知,你所做的事情是违反规则的(尽管这些东西有时确实有效)......所有 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.

隔岸观火 2024-07-26 17:38:04

该项目中的代码有一个巧妙的方法来处理这个问题: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.

泪是无色的血 2024-07-26 17:38:04

如果将 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).

恰似旧人归 2024-07-26 17:38:04

使用:

Public Declare Function CreateWindowExA Lib "user32" (ByVal dwExStyle As Long, 
    ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, 
    ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, 
    ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) 
    As Long

Public Declare Function CreateWindowExW Lib "user32" (ByVal dwExStyle As Long, 
    ByVal lpClassName As Long, ByVal lpWindowName As Long, ByVal dwStyle As Long, 
    ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, 
    ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Long) 
    As Long

注意:您可以在声明中将lpClassNamelpWindowName更改为stringlong
确保使用“Static”作为 lpClassName 成员 - 这是您想要的不可见窗口样式,通常用于接收 wndproc 窗口。

此 api 的返回值是 hwnd(handle),您可以对其进行子类化以获取消息
它将接收所有标准 Windows 消息,或者您可以向其发送自定义消息,因此当收到标准消息时,它不会意外触发您的代码。

例子:
hwnd_main 是其父窗口的 hwnd 子类,

dim hwnd_recieve as long
hwnd_recieve = CreateWindowEx(num_zero, "Static", "",0,0,0,0,0, hwnd_main,0,0,0)

玩得开心!

use:

Public Declare Function CreateWindowExA Lib "user32" (ByVal dwExStyle As Long, 
    ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, 
    ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, 
    ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) 
    As Long

or

Public Declare Function CreateWindowExW Lib "user32" (ByVal dwExStyle As Long, 
    ByVal lpClassName As Long, ByVal lpWindowName As Long, ByVal dwStyle As Long, 
    ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, 
    ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Long) 
    As Long

note: you can change the lpClassName or lpWindowName to string or long in the declaration
make 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 window

dim hwnd_recieve as long
hwnd_recieve = CreateWindowEx(num_zero, "Static", "",0,0,0,0,0, hwnd_main,0,0,0)

subclass this and have fun!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文