tags:

views:

664

answers:

6

As we all know, if the icon for a wpf window is undefined then the default icon is displayed. I want to display a window without any icon in the title bar. I realise that I could use a blank image, however this would cause the text in the title bar to be offset to the right.

Does anyone know of a way to completely remove the icon?

(I tried searching for a similar question but couldn't find anything.)

+1  A: 

No, this doesn't seem to be possible. Quoting from the documentation of the Icon property (emphasis mine):

A WPF window always displays an icon. When one is not provided by setting Icon, WPF chooses an icon to display based on the following rules:

  1. Use the assembly icon, if specified.
  2. If the assembly icon is not specified, use the default Microsoft Windows icon.

If you use Icon to specify a custom window icon, you can restore the default application icon by setting Icon to null.

So, apparently a completely transparent icon seems to be your best bet here. Or perhaps hack around all this by using Windows API functions to set the appropriate style on the window. But this may interfere with WPF's window management.

Joey
+1  A: 

My first suggestion would be don't do it. In WinForms you can use types of formborderstyles to create a dialog box that has no icon, but only because that is a Windows standard. Only forms with those specific border types should have no icon; it's what users expect.

Neil Barnwell
Windows Forms also has a `ShowIcon` property.
Joey
Unfortunately, there appears to be no real distinction between windows and dialogs in WPF.
m_arnell
+2  A: 

While not exactly a proper solution, you could try one of the following things:

  1. Setting the WindowStyle-Property to ToolWindow will make the Icon disappear, but the title bar (obviously) will be smaller.

  2. Write a ControlTemplate for the whole Window. Depending on if the Window has to look like a "real" Window, there'd be much effort into trying to recreate the default Style in the Template.

Andrej
I think the WPF'ed parts of the Expression suite may draw everything (including title bars) themselves to allow for such things. Still, looking and behaving natively is a very nice thing to have for the user, imho.
Joey
+1 The "ToolWindow" WindowStyle is exactly what I was looking for!
ewall
+2  A: 

Simple, add this code to your window:

[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);
}
Nir
Had to modify a bit based on code in "How to hide close button in wpf window?", but this did work!
m_arnell
I think we have different definitions of simple..
vanja.
This seems to get rid of the "close" button as well which isn't always desired.
Keith Hill
A: 

I'm trying to create a Aero Wizard in WPF, so I have to hide the window's icon, too.To do is I am using the Win 32 API, as shown e.g. here. This works fine when debugging the appliocation inside Visual Studio, but when I start the executable wihout debugging, the Icon is still displayed.

Has anyone an idea what causes this?

Andreas
A: 

Why to do this (just to put the problem in perspective).

The Vista UX guidelines state:

Modal error message dialogs don't have title bar icons. Title bar icons are used as a visual distinction between primary windows and secondary windows.

Robin Davies