2013-10-28 96 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

相關問題