How to remove icon and add ? button in wpf window? - wpf

I have separate code of both one for remove icon and one for add '?'(help) button. but can anyone help me to do both.
my code is below.
[DllImport("user32.dll")]
static extern uint GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hwnd, int index, uint newStyle);
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
int x, int y, int width, int height, uint flags);
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hwnd, uint msg,
IntPtr wParam, IntPtr lParam);
const int GWL_EXSTYLE = -20;
const int WS_EX_DLGMODALFRAME = 0x0001;
const int WM_SETICON = 0x0080;
private const uint WS_EX_CONTEXTHELP = 0x00000400;
private const uint WS_MINIMIZEBOX = 0x00020000;
private const uint WS_MAXIMIZEBOX = 0x00010000;
private const int GWL_STYLE = -16;
private const int SWP_NOSIZE = 0x0001;
private const int SWP_NOMOVE = 0x0002;
private const int SWP_NOZORDER = 0x0004;
private const int SWP_FRAMECHANGED = 0x0020;
private const int WM_SYSCOMMAND = 0x0112;
private const int SC_CONTEXTHELP = 0xF180;
The below code is remove icon.
public static void RemoveIcon(Window window)
{
IntPtr hwnd = new WindowInteropHelper(window).Handle;
// Change the extended window style to not show a window icon
uint extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_DLGMODALFRAME | WM_SETICON);
// Update the window's non-client area to reflect the changes
SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE |
SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
}
This function is for add help icon
public static void AddHelpIcon(Window window)
{
IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(window).Handle;
uint styles = GetWindowLong(hwnd, GWL_STYLE);
styles &= 0xFFFFFFFF ^ (WS_MINIMIZEBOX | WS_MAXIMIZEBOX);
SetWindowLong(hwnd, GWL_STYLE, styles);
styles = GetWindowLong(hwnd, GWL_EXSTYLE );
styles |= WS_EX_CONTEXTHELP;
SetWindowLong(hwnd, GWL_EXSTYLE, styles | WS_EX_DLGMODALFRAME | WM_SETICON);
SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
((HwndSource)PresentationSource.FromVisual(window)).AddHook(HelpHook);
}
private static IntPtr HelpHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_SYSCOMMAND &&
((int)wParam & 0xFFF0) == SC_CONTEXTHELP)
{
MessageBox.Show("help");
handled = true;
}
return IntPtr.Zero;
}
So can anybody help me for doing both of them in a windows.

I'm using a Telerik window which is best for that. Please try it.

Related

How to remove the icon from title bar in Windows Presentation Framework(.Net)?

How to remove the icon from title bar in Windows Presentation Framework(.Net)?
I need just to remove the icon from the title bar only, not from any other place.
When i switch my application from fullscreen to normal screen then the title bar icon is again appearing
Can anyone suggest any durable solution?
Try this
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int width, int height, uint flags);
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam);
const int GWL_EXSTYLE = -20;
const int WS_EX_DLGMODALFRAME = 0x0001;
const int SWP_NOSIZE = 0x0001;
const int SWP_NOMOVE = 0x0002;
const int SWP_NOZORDER = 0x0004;
const int SWP_FRAMECHANGED = 0x0020;
const uint WM_SETICON = 0x0080;
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
IntPtr hwnd = new WindowInteropHelper(this).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);
}
}

What is the equal of HelpButton In Wpf Application Window? [duplicate]

How can add this button to the title bar in WPF, by it being so used in a lot of applications I thought it would be built in or something, but looks like it isn't. Anyway let me know if you know anything about this.
Thanks.
Edit:
Isn't there anything equivalent to this?
Basically, to have the ? icon in win forms, all you need to do is this:
public Form1()
{
InitializeComponent();
this.HelpButton = true;
this.MaximizeBox = false;
this.MinimizeBox = false;
}
Doesn't WPF have anything like that?
It's simple, just inset this code into your Window class.
This code uses interop to remove the WS_MINIMIZEBOX and WS_MAXIMIZEBOX styles and add the WS_EX_CONTEXTHELP extended style (the question mark will only show up if you remove the minimize and maximize buttons).
EDIT: added click detection on the help button, this is done by hooking into the WndProc using HwndSource.AddHook and listening for a WM_SYSCOMMAND message with wParam of SC_CONTEXTHELP.
When a click is detected this code will show a message box, changing this into an event, routed event or even a command (for MVVM apps) is left as an exercise for the reader.
private const uint WS_EX_CONTEXTHELP = 0x00000400;
private const uint WS_MINIMIZEBOX = 0x00020000;
private const uint WS_MAXIMIZEBOX = 0x00010000;
private const int GWL_STYLE = -16;
private const int GWL_EXSTYLE = -20;
private const int SWP_NOSIZE = 0x0001;
private const int SWP_NOMOVE = 0x0002;
private const int SWP_NOZORDER = 0x0004;
private const int SWP_FRAMECHANGED = 0x0020;
private const int WM_SYSCOMMAND = 0x0112;
private const int SC_CONTEXTHELP = 0xF180;
[DllImport("user32.dll")]
private static extern uint GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hwnd, int index, uint newStyle);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int width, int height, uint flags);
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
uint styles = GetWindowLong(hwnd, GWL_STYLE);
styles &= 0xFFFFFFFF ^ (WS_MINIMIZEBOX | WS_MAXIMIZEBOX);
SetWindowLong(hwnd, GWL_STYLE, styles);
styles = GetWindowLong(hwnd, GWL_EXSTYLE);
styles |= WS_EX_CONTEXTHELP;
SetWindowLong(hwnd, GWL_EXSTYLE, styles);
SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
((HwndSource)PresentationSource.FromVisual(this)).AddHook(HelpHook);
}
private IntPtr HelpHook(IntPtr hwnd,
int msg,
IntPtr wParam,
IntPtr lParam,
ref bool handled)
{
if (msg == WM_SYSCOMMAND &&
((int)wParam & 0xFFF0) == SC_CONTEXTHELP)
{
MessageBox.Show("help");
handled = true;
}
return IntPtr.Zero;
}
No help buttons come out of the box with WPF. Should'nt be a push to roll your own however.

Can a WPF-style splash screen be achieved in a Forms application?

Whilst playing around with resources in my visual studio 10 project, I came across a build action called "Splash Screen", which led me to find this article on the neat splash screen capabilities of WPF.
Hunting around, I found this MSDN article on making a splash screen in Windows Forms, but it's a different type of approach: rather than loading the splash screen using native code before the app loads, the WinForms version simply displays it whilst the main form is initialising.
Is there any way to achieve this superior type of splash screen in a WinForms app?
Yes. I did an implementation for our WPF application before it came packaged with .NET 3.5 SP1.
Basically, you create a Native Win32 window and display a BMP image whilst you are loading the assemblies and initialising your application. You can use other image formats, but BMP is preferred as it requires the least number of libraries loaded.
A quick google for "creating native splash window" came up with several articles. The most comprehensive that I found (from a quick scan) was by Bradley Grainger: Displaying a Splash Screen with C++. The article was written for WPF in mind, however the concept the same: create a native window, launch your application, close window.
I will have a look at the source for my implementation tomorrow at work and update my answer with an examples.
Until then, google and research the many examples already out there to get started.
UPDATE
As promised, below is a full example of implementing a splash screen (there is a bit to it).
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Threading;
using System.Windows.Interop;
namespace SplashScreen
{
public class SplashScreenManager
{
static SplashScreen _current = null;
static SplashScreenManager() {}
SplashScreenManager() { }
public static SplashScreen Create(Module module, int resourceID)
{
if (_current != null)
{
_current.Close();
_current.Dispose();
}
_current = new SplashScreen(module, resourceID);
return _current;
}
public static void Close()
{
if (_current == null)
return;
_current.Close();
_current.Dispose();
_current = null;
}
public static SplashScreen Current
{
get { return _current; }
}
}
public class SplashScreen : IDisposable
{
static bool IsClassRegistered = false;
static string WindowClassName = "SplashScreenWindowClass";
IntPtr _bitmapHandle = IntPtr.Zero;
int _bitmapHeight;
int _bitmapWidth;
bool _isClosed;
UnsafeNativeMethods.WndProc _splashWindowProcedureCallback;
IntPtr _windowHandle = IntPtr.Zero;
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
internal SplashScreen(Module module, int resourceID)
{
_bitmapHandle = UnsafeNativeMethods.LoadBitmap(Marshal.GetHINSTANCE(module), new IntPtr(resourceID));
_splashWindowProcedureCallback = new UnsafeNativeMethods.WndProc(SplashWindowProcedure);
}
public void Close()
{
if (_isClosed)
return;
_isClosed = true;
UnsafeNativeMethods.PostMessage(new HandleRef(this, _windowHandle), 0x10, IntPtr.Zero, IntPtr.Zero);
if (_bitmapHandle != IntPtr.Zero)
{
UnsafeNativeMethods.DeleteObject(_bitmapHandle);
_bitmapHandle = IntPtr.Zero;
}
}
public void Close(IntPtr handle)
{
if (_windowHandle != IntPtr.Zero)
UnsafeNativeMethods.SetForegroundWindow(handle);
Close();
}
void CreateWindow()
{
if (!IsClassRegistered)
RegisterClass();
if (IsClassRegistered)
{
CreateWindowInternal();
if (_windowHandle != IntPtr.Zero)
UnsafeNativeMethods.ShowWindow(_windowHandle, 5);
}
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
Close(IntPtr.Zero);
GC.SuppressFinalize(this);
}
void GetBitmapDimensions()
{
int cb = Marshal.SizeOf(typeof(UnsafeNativeMethods.BITMAP));
IntPtr lpvObject = Marshal.AllocCoTaskMem(cb);
UnsafeNativeMethods.GetObject(_bitmapHandle, cb, lpvObject);
UnsafeNativeMethods.BITMAP bitmap = (UnsafeNativeMethods.BITMAP)Marshal.PtrToStructure(lpvObject, typeof(UnsafeNativeMethods.BITMAP));
_bitmapWidth = bitmap.bmWidth;
_bitmapHeight = bitmap.bmHeight;
Marshal.FreeCoTaskMem(lpvObject);
}
void OnPaint(IntPtr hdc)
{
if (_bitmapHandle != IntPtr.Zero)
{
IntPtr ptr = UnsafeNativeMethods.CreateCompatibleDC(hdc);
IntPtr hgdiobj = UnsafeNativeMethods.SelectObject(ptr, _bitmapHandle);
UnsafeNativeMethods.BitBlt(hdc, 0, 0, _bitmapWidth, _bitmapHeight, ptr, 0, 0, 0xcc0020);
UnsafeNativeMethods.SelectObject(ptr, hgdiobj);
UnsafeNativeMethods.DeleteDC(ptr);
}
}
void CreateWindowInternal()
{
int systemMetrics = UnsafeNativeMethods.GetSystemMetrics(0);
int num4 = UnsafeNativeMethods.GetSystemMetrics(1);
uint dwStyle = 0x80000000;
uint dwExStyle = 0x188;
IntPtr moduleHandle = UnsafeNativeMethods.GetModuleHandle(null);
IntPtr desktopWindow = UnsafeNativeMethods.GetDesktopWindow();
_windowHandle = UnsafeNativeMethods.CreateWindowEx(dwExStyle, WindowClassName, "", dwStyle, (systemMetrics - _bitmapWidth) / 2, (num4 - _bitmapHeight) / 2, _bitmapWidth, _bitmapHeight, desktopWindow, IntPtr.Zero, moduleHandle, IntPtr.Zero);
}
void RegisterClass()
{
IntPtr moduleHandle = UnsafeNativeMethods.GetModuleHandle(null);
UnsafeNativeMethods.WNDCLASSEX lpwcx = new UnsafeNativeMethods.WNDCLASSEX();
lpwcx.cbSize = (uint)Marshal.SizeOf(typeof(UnsafeNativeMethods.WNDCLASSEX));
lpwcx.cbClsExtra = 0;
lpwcx.cbWndExtra = 0;
lpwcx.hbrBackground = IntPtr.Zero;
lpwcx.hCursor = IntPtr.Zero;
lpwcx.hIcon = IntPtr.Zero;
lpwcx.hIconSm = IntPtr.Zero;
lpwcx.hInstance = moduleHandle;
lpwcx.lpfnWndProc = _splashWindowProcedureCallback;
lpwcx.lpszClassName = WindowClassName;
lpwcx.lpszMenuName = null;
lpwcx.style = 0;
if (UnsafeNativeMethods.RegisterClassExW(ref lpwcx) != 0)
{
IsClassRegistered = true;
}
}
public void Show()
{
if (_windowHandle == IntPtr.Zero)
{
Thread thread = new Thread(new ThreadStart(ThreadMethod));
thread.IsBackground = true;
thread.Start();
}
}
[SuppressUnmanagedCodeSecurity]
IntPtr SplashWindowProcedure(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
if (msg == 15)
{
UnsafeNativeMethods.PAINTSTRUCT lpPaint = new UnsafeNativeMethods.PAINTSTRUCT();
IntPtr hdc = UnsafeNativeMethods.BeginPaint(hWnd, out lpPaint);
OnPaint(hdc);
UnsafeNativeMethods.EndPaint(hWnd, ref lpPaint);
return IntPtr.Zero;
}
return UnsafeNativeMethods.DefWindowProc(hWnd, msg, wParam, lParam);
}
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
void ThreadMethod()
{
if (_bitmapHandle != IntPtr.Zero)
{
GetBitmapDimensions();
CreateWindow();
MSG msg = new MSG();
while (UnsafeNativeMethods.GetMessage(ref msg, _windowHandle, 0, 0) > 0)
{
UnsafeNativeMethods.TranslateMessage(ref msg);
UnsafeNativeMethods.DispatchMessage(ref msg);
}
_windowHandle = IntPtr.Zero;
GC.KeepAlive(this);
}
}
}
[SuppressUnmanagedCodeSecurity]
internal sealed class UnsafeNativeMethods
{
// Fields
internal const int GWL_EXSTYLE = -20;
public const int LOGPIXELSX = 0x58;
public const int LOGPIXELSY = 90;
internal const uint MB_ICONASTERISK = 0x40;
internal const uint MB_ICONERROR = 0x10;
internal const uint MB_ICONEXCLAMATION = 0x30;
internal const uint MB_ICONHAND = 0x10;
internal const uint MB_ICONINFORMATION = 0x40;
internal const uint MB_ICONQUESTION = 0x20;
internal const uint MB_ICONWARNING = 0x30;
internal const uint MB_OK = 0;
internal const uint MB_OKCANCEL = 1;
internal const uint MB_SETFOREGROUND = 0x10000;
internal const uint MB_YESNO = 4;
internal const uint MB_YESNOCANCEL = 3;
internal const int SM_CXSCREEN = 0;
internal const int SM_CYSCREEN = 1;
public const int SPI_GETWORKAREA = 0x30;
internal const uint SRCCOPY = 0xcc0020;
public const int SW_HIDE = 0;
internal const int SW_SHOW = 5;
public const int SW_SHOWMAXIMIZED = 3;
public const int SW_SHOWMINIMIZED = 2;
public const int SW_SHOWNORMAL = 1;
internal const int WM_CLOSE = 0x10;
internal const uint WS_EX_TOOLWINDOW = 0x80;
internal const uint WS_EX_TOPMOST = 8;
internal const uint WS_EX_TRANSPARENT = 0x20;
internal const uint WS_EX_WINDOWEDGE = 0x100;
internal const uint WS_POPUP = 0x80000000;
UnsafeNativeMethods() {}
[DllImport("user32.dll")]
public static extern IntPtr WindowFromPoint(POINT Point);
[DllImport("user32.dll")]
public static extern IntPtr ChildWindowFromPoint(IntPtr hWndParent, POINT Point);
[DllImport("user32.dll")]
public static extern bool ScreenToClient(IntPtr hWnd, ref POINT lpPoint);
[DllImport("user32.dll")]
internal static extern IntPtr BeginPaint(IntPtr hwnd, out PAINTSTRUCT lpPaint);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("gdi32.dll")]
internal static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);
[DllImport("gdi32.dll")]
internal static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("user32.dll", EntryPoint = "CreateWindowExW", CharSet = CharSet.Unicode)]
internal static extern IntPtr CreateWindowEx(uint dwExStyle, string lpClassName, string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
[DllImport("user32.dll")]
internal static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("gdi32.dll")]
internal static extern bool DeleteDC(IntPtr hdc);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("gdi32.dll")]
internal static extern bool DeleteObject(IntPtr hObject);
[DllImport("user32.dll")]
internal static extern IntPtr DispatchMessage([In] ref MSG lpmsg);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll")]
internal static extern bool EndPaint(IntPtr hWnd, ref PAINTSTRUCT lpPaint);
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")]
internal static extern IntPtr GetDC(HandleRef hWnd);
[DllImport("user32.dll")]
internal static extern IntPtr GetDesktopWindow();
[DllImport("gdi32.dll")]
public static extern int GetDeviceCaps(IntPtr hDC, int index);
[DllImport("user32.dll", EntryPoint = "GetMessageW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int GetMessage([In, Out] ref MSG msg, IntPtr hWnd, int uMsgFilterMin, int uMsgFilterMax);
[DllImport("kernel32.dll", EntryPoint = "GetModuleHandleW", CharSet = CharSet.Unicode)]
internal static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("gdi32.dll")]
internal static extern int GetObject(IntPtr hgdiobj, int cbBuffer, IntPtr lpvObject);
[DllImport("user32.dll")]
internal static extern int GetSystemMetrics(int nIndex);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern int GetWindowLong(IntPtr handle, int index);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll")]
public static extern bool GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT lpwndpl);
[DllImport("user32.dll", EntryPoint = "LoadBitmapW", CharSet = CharSet.Unicode)]
internal static extern IntPtr LoadBitmap(IntPtr hInstance, IntPtr lpBitmapName);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll")]
internal static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[return: MarshalAs(UnmanagedType.U2)]
[DllImport("user32.dll")]
internal static extern short RegisterClassExW([In] ref WNDCLASSEX lpwcx);
[DllImport("user32.dll")]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll")]
internal static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll")]
internal static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern int SetWindowLong(IntPtr handle, int index, int dwNewLong);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll")]
public static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll")]
public static extern bool SystemParametersInfo(int nAction, int nParam, ref RECT rc, int nUpdate);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll")]
internal static extern bool TranslateMessage([In] ref MSG lpMsg);
[StructLayout(LayoutKind.Sequential)]
internal struct BITMAP
{
public int bmType;
public int bmWidth;
public int bmHeight;
public int bmWidthBytes;
public ushort bmPlanes;
public ushort bmBitsPixel;
public IntPtr bmBits;
}
[StructLayout(LayoutKind.Sequential)]
internal struct PAINTSTRUCT
{
public IntPtr hdc;
public bool fErase;
public UnsafeNativeMethods.RECT rcPaint;
public bool fRestore;
public bool fIncUpdate;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x20)]
public byte[] rgbReserved;
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
}
[StructLayout(LayoutKind.Sequential)]
internal struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct WINDOWPLACEMENT
{
public int Length;
public int Flags;
public int ShowCmd;
public UnsafeNativeMethods.POINT MinPosition;
public UnsafeNativeMethods.POINT MaxPosition;
public UnsafeNativeMethods.RECT NormalPosition;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct WNDCLASSEX
{
public uint cbSize;
public uint style;
public UnsafeNativeMethods.WndProc lpfnWndProc;
public int cbClsExtra;
public int cbWndExtra;
public IntPtr hInstance;
public IntPtr hIcon;
public IntPtr hCursor;
public IntPtr hbrBackground;
public string lpszMenuName;
public string lpszClassName;
public IntPtr hIconSm;
}
internal delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
}
}
It is possible to use a non-WPF form for this, and it works quite happily. As an aside, it was a feature in VS2008 as well.

Removing Icon from a WPF window

I am able to remove the window icon from WPF window using WinApi's, however I get the icon again in the application window when I run just the executable of the WPF project.
How do I remove the icon?
From WPFTutorial:
How to remove the icon of a WPF window
Unfortunately WPF does not provide any function to remove the icon of a window. One solution could be setting the icon to a transparent icon. But this way the extra space between the window border and title remains.
The better approach is to use a function provided by the Win32 API to remove the icon.
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
protected override void OnSourceInitialized(EventArgs e)
{
IconHelper.RemoveIcon(this);
}
}
A helper class used to remove the icon.
public static class IconHelper
{
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
int x, int y, int width, int height, uint flags);
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hwnd, uint msg,
IntPtr wParam, IntPtr lParam);
const int GWL_EXSTYLE = -20;
const int WS_EX_DLGMODALFRAME = 0x0001;
const int SWP_NOSIZE = 0x0001;
const int SWP_NOMOVE = 0x0002;
const int SWP_NOZORDER = 0x0004;
const int SWP_FRAMECHANGED = 0x0020;
const uint WM_SETICON = 0x0080;
public static void RemoveIcon(Window window)
{
// Get this window's handle
IntPtr hwnd = new WindowInteropHelper(window).Handle;
// Change the extended window style to not show a window icon
int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_DLGMODALFRAME);
// Update the window's non-client area to reflect the changes
SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE |
SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
}
}
Just add this WindowStyle="ToolWindow" to your window properties.
I have modified the sample from 'LnDCobra' so it can be used as an attached property (as 'Thomas' suggested:
<Window
...
xmlns:i="clr-namespace:namespace-to-WindowEx"
i:WindowEx.ShowIcon = "false"
...
>
Implementation of WindowEx:
public class WindowEx
{
private const int GwlExstyle = -20;
private const int SwpFramechanged = 0x0020;
private const int SwpNomove = 0x0002;
private const int SwpNosize = 0x0001;
private const int SwpNozorder = 0x0004;
private const int WsExDlgmodalframe = 0x0001;
public static readonly DependencyProperty ShowIconProperty =
DependencyProperty.RegisterAttached(
"ShowIcon",
typeof (bool),
typeof (WindowEx),
new FrameworkPropertyMetadata(true, new PropertyChangedCallback((d, e) => RemoveIcon((Window) d))));
public static Boolean GetShowIcon(UIElement element)
{
return (Boolean) element.GetValue(ShowIconProperty);
}
public static void RemoveIcon(Window window)
{
window.SourceInitialized += delegate {
// Get this window's handle
var hwnd = new WindowInteropHelper(window).Handle;
// Change the extended window style to not show a window icon
int extendedStyle = GetWindowLong(hwnd, GwlExstyle);
SetWindowLong(hwnd, GwlExstyle, extendedStyle | WsExDlgmodalframe);
// Update the window's non-client area to reflect the changes
SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SwpNomove |
SwpNosize | SwpNozorder | SwpFramechanged);
};
}
public static void SetShowIcon(UIElement element, Boolean value)
{
element.SetValue(ShowIconProperty, value);
}
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hwnd, uint msg,
IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
int x, int y, int width, int height, uint flags);
}
}
Here a simple and pure XAML solution:
<Window x:Class="...">
<Window.Icon>
<DrawingImage />
</Window.Icon>
...
</Window>
I just use very small transparent image as an icon (1x1 px) for WPF window.
Create a transparent 1 by 1 icon and replace it with a standard one
Icon = BitmapSource.Create(1, 1, 0, 0, PixelFormats.Bgra32, null, new byte[4], 4);

Help "?" button

How can add this button to the title bar in WPF, by it being so used in a lot of applications I thought it would be built in or something, but looks like it isn't. Anyway let me know if you know anything about this.
Thanks.
Edit:
Isn't there anything equivalent to this?
Basically, to have the ? icon in win forms, all you need to do is this:
public Form1()
{
InitializeComponent();
this.HelpButton = true;
this.MaximizeBox = false;
this.MinimizeBox = false;
}
Doesn't WPF have anything like that?
It's simple, just inset this code into your Window class.
This code uses interop to remove the WS_MINIMIZEBOX and WS_MAXIMIZEBOX styles and add the WS_EX_CONTEXTHELP extended style (the question mark will only show up if you remove the minimize and maximize buttons).
EDIT: added click detection on the help button, this is done by hooking into the WndProc using HwndSource.AddHook and listening for a WM_SYSCOMMAND message with wParam of SC_CONTEXTHELP.
When a click is detected this code will show a message box, changing this into an event, routed event or even a command (for MVVM apps) is left as an exercise for the reader.
private const uint WS_EX_CONTEXTHELP = 0x00000400;
private const uint WS_MINIMIZEBOX = 0x00020000;
private const uint WS_MAXIMIZEBOX = 0x00010000;
private const int GWL_STYLE = -16;
private const int GWL_EXSTYLE = -20;
private const int SWP_NOSIZE = 0x0001;
private const int SWP_NOMOVE = 0x0002;
private const int SWP_NOZORDER = 0x0004;
private const int SWP_FRAMECHANGED = 0x0020;
private const int WM_SYSCOMMAND = 0x0112;
private const int SC_CONTEXTHELP = 0xF180;
[DllImport("user32.dll")]
private static extern uint GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hwnd, int index, uint newStyle);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int width, int height, uint flags);
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
uint styles = GetWindowLong(hwnd, GWL_STYLE);
styles &= 0xFFFFFFFF ^ (WS_MINIMIZEBOX | WS_MAXIMIZEBOX);
SetWindowLong(hwnd, GWL_STYLE, styles);
styles = GetWindowLong(hwnd, GWL_EXSTYLE);
styles |= WS_EX_CONTEXTHELP;
SetWindowLong(hwnd, GWL_EXSTYLE, styles);
SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
((HwndSource)PresentationSource.FromVisual(this)).AddHook(HelpHook);
}
private IntPtr HelpHook(IntPtr hwnd,
int msg,
IntPtr wParam,
IntPtr lParam,
ref bool handled)
{
if (msg == WM_SYSCOMMAND &&
((int)wParam & 0xFFF0) == SC_CONTEXTHELP)
{
MessageBox.Show("help");
handled = true;
}
return IntPtr.Zero;
}
No help buttons come out of the box with WPF. Should'nt be a push to roll your own however.

Resources