从任务栏隐藏应用程序

从任务栏隐藏应用程序

本文介绍了C#从任务栏隐藏应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力从应用程序的任务栏中隐藏另一个应用程序.
我一直在使用 SetWindowLong 函数,以便在扩展样式.

I have been struggling to hide another application from the taskbar from my application.
I have been using the SetWindowLong function in order to set/remove WS_EX_APPWINDOW on the extended style.

我已经尝试过分别设置和删除该属性,以及尝试获取当前的WindowLong并将其删除/添加到该属性,就像这样:

I have tried both setting and removing the property individually as well as taking the current WindowLong, and removing/adding it to that one, like so:

SetWindowLong(pMainWindow, GWL_EXSTYLE, GetWindowLong(pMainWindow) & WS_EX_APPWINDOW);

并尝试将其删除,如下所示:

And tried removing it like so:

SetWindowLong(pMainWindow, GWL_EXSTYLE, GetWindowLong(pMainWindow) & ~WS_EX_APPWINDOW);

还尝试了这两种方法,而没有先让窗口变长.这是我的整个代码:

Also tried both those methods without first getting the window long. Here is my entire code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    [DllImport("User32.dll")]
    public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    [DllImport("User32.dll")]
    public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    private const int SW_HIDE = 0x00;
    private const int SW_SHOW = 0x05;

    private const int WS_EX_APPWINDOW = 0x40000;
    private const int GWL_EXSTYLE = -0x14;

    private void HideWindowFromTaskbar(IntPtr pMainWindow)
    {
        SetWindowLong(pMainWindow, GWL_EXSTYLE, ~WS_EX_APPWINDOW);

        ShowWindow(pMainWindow, SW_HIDE);
        ShowWindow(pMainWindow, SW_SHOW);
    }

    private void ButtonHide_Click(object sender, RoutedEventArgs e)
    {
        HideWindowFromTaskbar(Process.GetProcessesByName("notepad")[0].MainWindowHandle);
    }
}

我注意到Spy ++中的属性发生了变化.我得到了一堆不同的结果,例如添加了WS_EX_APPWINDOW,但是其他属性随机消失了,等等.
在查看邮件时,我还看到它直接收到了STYLE_CHANGED之类的邮件.

I have noticed changes in Spy++ looking at the properties. I had a bunch of different results, like WS_EX_APPWINDOW being added, but also randomly have other attributes disappearing, etc.
When looking at the messages, I also saw that it DID get messages like STYLE_CHANGED.

任何帮助将不胜感激,
雷内(René)

Any help would be appreciated,
René

推荐答案

MSDN . Raymond Chen给出了以下这些规则的摘要 :

The rules for determining which windows have buttons on the taskbar are documented on MSDN. Raymond Chen gives the following summary of these rules:

  • 如果设置了WS_EX_APPWINDOW扩展样式,则它将显示(可见时).
  • 如果该窗口是顶级无主窗口,则它将显示(可见时).
  • 否则它不会显示.
  • If the WS_EX_APPWINDOW extended style is set, then it will show (when visible).
  • If the window is a top-level unowned window, then it will show (when visible).
  • Otherwise it doesn't show.

您试图在另一个应用程序中修改窗口的事实严重影响了您.您正在删除WS_EX_APPWINDOW扩展样式.这还不够,因为所讨论的窗口将是顶级的无主窗口(请参见项目符号要点2).创建窗口后,您将无法更改其所有者,并且由于该窗口是由另一个进程控制的,因此您几乎陷入了僵局.

The fact that you are trying to modify a window in another app severely hampers you. You are removing the WS_EX_APPWINDOW extended style. This is not enough because the window in question will be a top-level unowned window (see bullet point 2). You cannot change the owner of a window once it has been created and since the window is controlled by another process you are pretty much stuck.

剩下的唯一选择是删除WS_EX_APPWINDOW扩展样式并将其替换为WS_EX_TOOLWINDOW.实际上,这将使该窗口脱离任务栏,但它将更改窗口外观:

The only option remaining is to remove the WS_EX_APPWINDOW extended style and replace it with WS_EX_TOOLWINDOW. This will indeed get the window off the taskbar but it will change the appearance of the window:

这篇关于C#从任务栏隐藏应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 06:49