2013-10-28 48 views
2

我用下面的代码从任务栏隐藏...隐藏/显示任务栏从Windows应用程序中使用C#

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 const int WS_EX_TOOLWINDOW = 0x0080; 

[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); 
bool isShow = true; 
private void toggle(Process p) 
{ 
    if (isShow) 
    { 
     isShow = false; 
     SetWindowLong(p.MainWindowHandle, GWL_EXSTYLE, WS_EX_APPWINDOW); 
     ShowWindow(p.MainWindowHandle, SW_SHOW); 
     ShowWindow(p.MainWindowHandle, SW_HIDE); 
     //Hide: working 

    } 
    else 
    { 
     isShow = true; 
     SetWindowLong(p.MainWindowHandle, GWL_EXSTYLE, WS_EX_APPWINDOW); 
     ShowWindow(p.MainWindowHandle, SW_HIDE); 
     ShowWindow(p.MainWindowHandle, SW_SHOW); 
     //Show: not working 
    } 
} 

但现在我想在任务栏再次显示我的计划 - 任何人都知道该怎么做它?

+0

我们可以看到调用切换方法的代码吗? – Baldrick

回答

4

通过调用SetWindowLongWS_EX_APPWINDOW参数,您不设置或删除标志,您将用WS_EX_APPWINDOW完全替换扩展样式。您可能没有注意到它,因为您没有使用任何其他扩展样式。

添加一个风格标志与SetWindowLong的正确的方法是:

SetWindowLong(p.MainWindowHandle, GWL_EXSTYLE, 
    GetWindowLong(p.MainWindowHandle, GWL_EXSTYLE) | WS_EX_APPWINDOW); 

删除标志的正确方法是:

SetWindowLong(p.MainWindowHandle, GWL_EXSTYLE, 
    GetWindowLong(p.MainWindowHandle, GWL_EXSTYLE) & ~WS_EX_APPWINDOW); 

阅读关于位运算理解为什么这是正确的做法做到这一点。

作为一个便笺,您从任务栏隐藏窗口的方式非常糟糕。首先,WS_EX_APPWINDOW不仅可以隐藏任务栏上的按钮,还可以更改窗口边框样式。此外,你隐藏和重新显示窗口,没有一个很好的理由。

从任务栏隐藏按钮的正确方法是使用ITaskbarList::DeleteTab method

相关问题