衆所周知,如果wpf窗口的圖標未定義,則會顯示默認圖標。我想在標題欄中顯示沒有任何圖標的窗口。我意識到我可以使用空白圖像,但是這會導致標題欄中的文本向右偏移。是否可以在標題欄中顯示沒有圖標的wpf窗口?
有誰知道一種方法來完全刪除圖標?
(我試圖尋找一個類似的問題,但找不到任何東西。)
衆所周知,如果wpf窗口的圖標未定義,則會顯示默認圖標。我想在標題欄中顯示沒有任何圖標的窗口。我意識到我可以使用空白圖像,但是這會導致標題欄中的文本向右偏移。是否可以在標題欄中顯示沒有圖標的wpf窗口?
有誰知道一種方法來完全刪除圖標?
(我試圖尋找一個類似的問題,但找不到任何東西。)
簡單,將此代碼添加到您的窗口:
[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);
}
不,這似乎並不可能。從Icon property(重點煤礦)的文檔引用:
WPF窗口始終顯示的圖標。當一個沒有被設置圖標提供,WPF選擇顯示基於以下規則的圖標:如果指定
- 使用組件圖標。
- 如果未指定程序集圖標,請使用默認的Microsoft Windows圖標。
如果您使用圖標指定自定義窗口圖標,則可以通過將圖標設置爲
null
來恢復默認應用程序圖標。
所以,顯然一個完全透明的圖標似乎是你最好的選擇。或者可能通過使用Windows API函數在窗口上設置適當的樣式來解決所有這些問題。但這可能干擾WPF的窗口管理。
我的第一個建議是不這樣做。在WinForms中,您可以使用formborderstyles類型來創建一個沒有圖標的對話框,但只是因爲它是Windows標準。只有那些特定邊框類型的表單才應該沒有圖標;這是用戶期望的。
Windows Forms還具有'ShowIcon'屬性。 – Joey 2009-10-12 10:11:46
不幸的是,WPF中的窗口和對話框似乎沒有真正的區別。 – 2009-10-12 10:20:18
[Windows錯誤對話框設計指南](https://msdn.microsoft.com/en-gb/library/windows/desktop/dn742471.aspx#icons)另有聲明:「_Modal錯誤消息對話框沒有標題bar icons_「 – 2015-11-05 09:20:55
雖然不完全是一個妥善的解決辦法,你可以嘗試以下情況之一:
的WindowStyle-屬性設置爲工具窗口將會使圖標消失,但標題欄(顯然)將變小。
爲整個窗口寫一個ControlTemplate。根據窗口是否看起來像一個「真實」窗口,嘗試在模板中重新創建默認樣式需要很多努力。
我知道這是回答,但是Dan Rigsby's blog有一篇文章,說明如何做到這一點不隱藏最小化/最大化框。
,我發現這是爲我所用的物品(here和here令人沮喪的我但它一直隱藏所有的按鈕,當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));
}
}
您可以使用一個空的PNG圖像,並將其轉換爲圖標,並將其設置爲您的窗口的圖標!
這是最簡單和最好的解決方案。 +1 – hamed 2016-10-31 07:16:24
儘管圖像仍然佔用了空間 – 2017-04-24 16:16:17
不得不根據「如何隱藏wpf窗口中的關閉按鈕?」中的代碼修改一些內容,但這確實奏效! – 2009-10-13 11:52:14
我認爲我們有簡單的不同定義.. – 2009-11-04 09:37:23
這似乎擺脫了「關閉」按鈕,這並不總是需要的。 – 2010-08-18 19:36:12