2009-10-12 108 views
28

衆所周知,如果wpf窗口的圖標未定義,則會顯示默認圖標。我想在標題欄中顯示沒有任何圖標的窗口。我意識到我可以使用空白圖像,但是這會導致標題欄中的文本向右偏移。是否可以在標題欄中顯示沒有圖標的wpf窗口?

有誰知道一種方法來完全刪除圖標?

(我試圖尋找一個類似的問題,但找不到任何東西。)

回答

26

簡單,將此代碼添加到您的窗口:

[DllImport("user32.dll")] 
static extern uint GetWindowLong(IntPtr hWnd, int nIndex); 

[DllImport("user32.dll")] 
static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong); 

private const int GWL_STYLE = -16; 

private const uint WS_SYSMENU = 0x80000; 

protected override void OnSourceInitialized(EventArgs e) 
{ 
    IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle; 
    SetWindowLong(hwnd, GWL_STYLE, 
     GetWindowLong(hwnd, GWL_STYLE) & (0xFFFFFFFF^WS_SYSMENU)); 

    base.OnSourceInitialized(e); 
} 
+0

不得不根據「如何隱藏wpf窗口中的關閉按鈕?」中的代碼修改一些內容,但這確實奏效! – 2009-10-13 11:52:14

+35

我認爲我們有簡單的不同定義.. – 2009-11-04 09:37:23

+13

這似乎擺脫了「關閉」按鈕,這並不總是需要的。 – 2010-08-18 19:36:12

10

不,這似乎並不可能。從Icon property(重點煤礦)的文檔引用:

WPF窗口始終顯示的圖標。當一個沒有被設置圖標提供,WPF選擇顯示基於以下規則的圖標:如果指定

  1. 使用組件圖標。
  2. 如果未指定程序集圖標,請使用默認的Microsoft Windows圖標。

如果您使用圖標指定自定義窗口圖標,則可以通過將圖標設置爲null來恢復默認應用程序圖標。

所以,顯然一個完全透明的圖標似乎是你最好的選擇。或者可能通過使用Windows API函數在窗口上設置適當的樣式來解決所有這些問題。但這可能干擾WPF的窗口管理。

-1

我的第一個建議是不這樣做。在WinForms中,您可以使用formborderstyles類型來創建一個沒有圖標的對話框,但只是因爲它是Windows標準。只有那些特定邊框類型的表單才應該沒有圖標;這是用戶期望的。

+1

Windows Forms還具有'ShowIcon'屬性。 – Joey 2009-10-12 10:11:46

+1

不幸的是,WPF中的窗口和對話框似乎沒有真正的區別。 – 2009-10-12 10:20:18

+3

[Windows錯誤對話框設計指南](https://msdn.microsoft.com/en-gb/library/windows/desktop/dn742471.aspx#icons)另有聲明:「_Modal錯誤消息對話框沒有標題bar icons_「 – 2015-11-05 09:20:55

17

雖然不完全是一個妥善的解決辦法,你可以嘗試以下情況之一:

  1. 的WindowStyle-屬性設置爲工具窗口將會使圖標消失,但標題欄(顯然)將變小。

  2. 爲整個窗口寫一個ControlTemplate。根據窗口是否看起來像一個「真實」窗口,嘗試在模板中重新創建默認樣式需要很多努力。

+0

我認爲表達式套件的WPF處理部分可以自己繪製一切(包括標題欄)以允許這樣的事情。儘管如此,對於用戶來說,自然而然地看待和表現對於用戶來說是一件非常好的事情。 – Joey 2009-10-12 10:18:25

+0

+1「ToolWindow」WindowStyle正是我所需要的! – ewall 2009-12-29 20:12:58

9

我知道這是回答,但是Dan Rigsby's blog有一篇文章,說明如何做到這一點不隱藏最小化/最大化框。

,我發現這是爲我所用的物品(herehere令人沮喪的我但它一直隱藏所有的按鈕,當SYSMENU被隱藏,幫助我創造了這個幫手其中如上所示呼叫OnSourceInitialized

public static class WpfWindowHelper { 

    [DllImport("user32.dll")] 
    public static extern int GetWindowLong(IntPtr hwnd, int index); 
    [DllImport("user32.dll")] 
    public static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle); 
    [DllImport("user32.dll")] 
    public static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int width, int height, uint flags); 

    public const int GWL_EXSTYLE = -20; 
    public const int WS_EX_DLGMODALFRAME = 0x0001; 
    public const int SWP_NOSIZE = 0x0001; 
    public const int SWP_NOMOVE = 0x0002; 
    public const int SWP_NOZORDER = 0x0004; 
    public const int SWP_FRAMECHANGED = 0x0020; 
    public const int GWL_STYLE = -16; 
    public const int WS_MAXIMIZEBOX = 0x00010000; 
    public const int WS_MINIMIZEBOX = 0x00020000; 
    public const int WS_SYSMENU = 0x00080000; 

    public static void HideSysMenu(this Window w) { 
     IntPtr hwnd = new WindowInteropHelper(w).Handle; 
     int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE); 
     SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_DLGMODALFRAME); 
     SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); 
    } 

    public static void HideMinimizeBox(this Window w) { 
     IntPtr hwnd = new WindowInteropHelper(w).Handle; 
     SetWindowLong(hwnd, GWL_STYLE, 
      GetWindowLong(hwnd, GWL_STYLE) & ~(WS_MINIMIZEBOX)); 
    } 

    public static void HideMaximizeBox(this Window w) { 
     IntPtr hwnd = new WindowInteropHelper(w).Handle; 
     SetWindowLong(hwnd, GWL_STYLE, 
      GetWindowLong(hwnd, GWL_STYLE) & ~(WS_MAXIMIZEBOX)); 
    } 

    public static void HideMinimizeAndMaximizeBoxes(this Window w) { 
     IntPtr hwnd = new WindowInteropHelper(w).Handle; 
     SetWindowLong(hwnd, GWL_STYLE, 
      GetWindowLong(hwnd, GWL_STYLE) & ~(WS_MAXIMIZEBOX | WS_MINIMIZEBOX)); 
    } 

} 
+0

HideSysMenu(WS_EX_DLGMODALFRAME)做的事情!隱藏圖標,但保持關閉按鈕不變。 – HelloSam 2012-09-14 05:34:56

+2

對不起,它沒有做到這一點。圖標和關閉按鈕仍然存在。根據微軟自己的UI對話窗口指南,我只想讓圖標消失。 – ygoe 2013-09-02 19:31:29

7

您可以使用一個空的PNG圖像,並將其轉換爲圖標,並將其設置爲您的窗口的圖標!

enter image description here

+1

這是最簡單和最好的解決方案。 +1 – hamed 2016-10-31 07:16:24

+0

儘管圖像仍然佔用了空間 – 2017-04-24 16:16:17

相關問題