I am entering the Code7 Contest and part of my project is to mimic the network notification icon.
The trick to this is to set the caption of the window to null (not empty string) and remove the system menu – at least as far as Windows Forms goes. WPF doesn’t let you do this. I had a few options:
- Implement a template to mimic the real window template. Not too sure how glass would work here.
- Use API calls to do it instead.
The second option seemed to be more sensible. As it turns out Windows Forms is doing quite a bit of heavy lifting under the covers – you don’t set the title to null; you actually need to remove the caption (Windows Forms does this for you). In the end it’s as simple as clearing two style flags for the Window.
private class NativeMethods
{
[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
static extern IntPtr SetWindowLongPtr32(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]
static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
public static IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
{
if (IntPtr.Size == 8)
return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
else
return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
}
[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
private static extern IntPtr GetWindowLongPtr32(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")]
private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);
public static IntPtr GetWindowLong(IntPtr hWnd, int nIndex)
{
if (IntPtr.Size == 8)
return GetWindowLongPtr64(hWnd, nIndex);
else
return GetWindowLongPtr32(hWnd, nIndex);
}
const int GWL_STYLE = (-16);
const int WS_SYSMENU = 0x00080000;
const int WS_CAPTION = 0x00C00000;
public static void RemoveMenu(IntPtr hwnd)
{
int currentStyle = GetWindowLong(hwnd, GWL_STYLE).ToInt32();
currentStyle &= ~WS_SYSMENU;
currentStyle &= ~WS_CAPTION;
int ret = SetWindowLong(hwnd, GWL_STYLE, new IntPtr(currentStyle)).ToInt32();
}
}
You simply call that from OnSourceInitialized.
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
WindowInteropHelper wih = new WindowInteropHelper(this);
NativeMethods.RemoveMenu(wih.Handle);
}
And you will get the following result:




One Comment
It works great! Thanks!