gpt4 book ai didi

c# - 从任务栏隐藏应用程序

转载 作者:可可西里 更新时间:2023-11-01 08:26:03 25 4
gpt4 key购买 nike

我一直在努力从我的应用程序的任务栏中隐藏另一个应用程序。
我一直在使用 SetWindowLong函数以在 extended style 上设置/删除 WS_EX_APPWINDOW .

我尝试过分别设置和删除属性,以及获取当前的 WindowLong,然后将其删除/添加到那个属性中,如下所示:

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

并尝试像这样删除它:

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

还尝试了这两种方法,但没有先让窗口变长。这是我的全部代码:

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 的消息。

最佳答案

确定哪些窗口在任务栏上有按钮的规则记录在 MSDN 上. Raymond Chen 给出以下 summary这些规则:

There are some basic rules on which windows go into the taskbar. In short:

  • 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)。一旦创建了窗口,您就无法更改其所有者,并且由于该窗口由另一个进程控制,因此您几乎陷入困境。

剩下的唯一选择是删除 WS_EX_APPWINDOW 扩展样式并将其替换为 WS_EX_TOOLWINDOW。这确实会使窗口离开任务栏,但它会改变 appearance of the window :

The window is intended to be used as a floating toolbar. A tool window has a title bar that is shorter than a normal title bar, and the window title is drawn using a smaller font. A tool window does not appear in the taskbar or in the dialog that appears when the user presses ALT+TAB. If a tool window has a system menu, its icon is not displayed on the title bar. However, you can display the system menu by right-clicking or by typing ALT+SPACE.

关于c# - 从任务栏隐藏应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8243588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com