使用 NativeWindow 显示任务栏项目

发布于 2024-08-28 20:54:19 字数 881 浏览 7 评论 0原文

我的应用程序旨在几乎完全通过使用缩略图和跳转列表的 Windows 7 任务栏项目来工作。我知道我可以轻松创建一个 Form 并简单地隐藏它,但这似乎有点矫枉过正。另外,我想尽可能多地使用 NativeWindow,因为我以前从未使用过它。

本质上,我有一个名为 RootWindow 的类,它派生自 NativeWindow ,它将处理热键以及其他所有内容。我根本不需要可见的窗口,而只是需要处理窗口消息并提供一个可以附加缩略图和跳转列表的任务栏项目。

是否需要将某种特殊的 CreateParams 选项传递给 CreateHandle ?还是我运气不好?

编辑:嗯,这比我想象的要容易,尽管这并不完全是我想要的。当我将 NativeWindow 的句柄传递给 ShowWindow API 后,任务栏项就会出现。但是,它还在屏幕的左上角显示一个窗口。有什么方法可以在仍然显示任务栏项目的同时摆脱该窗口吗?

public class RootWindow : NativeWindow {
    public const int SW_SHOWNOACTIVATE = 4;

    [DllImport("User32.dll")]
    private static extern int ShowWindow(IntPtr hWnd, short cmdShow);

    public RootWindow() {
            CreateHandle(new CreateParams());
            ShowWindow(this.Handle, SW_SHOWNOACTIVATE);
    }
}

My application is intended to work almost entirely through a Windows 7 taskbar item with the use of thumbnails and jump lists. I know I can easily create a Form and simply hide it, but this seems like overkill. Plus, I'd like to toy around with NativeWindow as much as possible, because I've never used it before.

Essentially, I have a class called RootWindow that derives from NativeWindow that will handle hotkeys and hopefully everything else. I don't need a visible window at all, but simply something to process window messages and provide a taskbar item that I can attach thumbnails and jump lists to.

Is there some kind of special CreateParams option I need to pass to CreateHandle? Or am I out of luck?

EDIT: Well, it was easier than I thought it would be, although it's not exactly what I want. Once I passed the NativeWindow's handle to the ShowWindow API, the taskbar item appeared. However, it also shows a window in the upper left corner of the screen. Is there some way to get rid of that window while still showing the taskbar item?

public class RootWindow : NativeWindow {
    public const int SW_SHOWNOACTIVATE = 4;

    [DllImport("User32.dll")]
    private static extern int ShowWindow(IntPtr hWnd, short cmdShow);

    public RootWindow() {
            CreateHandle(new CreateParams());
            ShowWindow(this.Handle, SW_SHOWNOACTIVATE);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

情绪 2024-09-04 20:54:19

诀窍是将窗口的样式设置为 WS_POPUP。

const int WS_POPUP = unchecked((int)0x80000000);
const int SW_SHOWNOACTIVATE = 4;

CreateHandle(new CreateParams() {
    Style = WS_POPUP
});

ShowWindow(Handle, SW_SHOWNOACTIVATE);

我还禁用了窗口的 Aero Peek,因为它仅用于后台工作。

const int DWMNCRP_ENABLED = 2;
const int DWMWA_DISALLOW_PEEK = 11;

int policy = DWMNCRP_ENABLED;
DwmSetWindowAttribute(Handle, DWMWA_DISALLOW_PEEK, ref policy, sizeof(int));

The trick was to set the window's style to WS_POPUP.

const int WS_POPUP = unchecked((int)0x80000000);
const int SW_SHOWNOACTIVATE = 4;

CreateHandle(new CreateParams() {
    Style = WS_POPUP
});

ShowWindow(Handle, SW_SHOWNOACTIVATE);

I also disabled Aero Peek for the window, since it's just for background work.

const int DWMNCRP_ENABLED = 2;
const int DWMWA_DISALLOW_PEEK = 11;

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