我一直在努力让我的应用程序从任务栏中隐藏另一个应用程序。
我一直在使用SetWindowLong函数来设置/删除扩展样式中的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
这样的消息。