// ----------------------------------------------------------------------- // // TODO: Update copyright text. // // ----------------------------------------------------------------------- namespace WPFCustomMessageBox { using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; /// /// TODO: Update summary. /// internal static class Util { [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; internal static ImageSource ToImageSource(this Icon icon) { ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon( icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); return imageSource; } /// /// Keyboard Accellerators are used in Windows to allow easy shortcuts to controls like Buttons and /// MenuItems. These allow users to press the Alt key, and a shortcut key will be highlighted on the /// control. If the user presses that key, that control will be activated. /// This method checks a string if it contains a keyboard accellerator. If it doesn't, it adds one to the /// beginning of the string. If there are two strings with the same accellerator, Windows handles it. /// The keyboard accellerator character for WPF is underscore (_). It will not be visible. /// /// /// internal static string? TryAddKeyboardAccellerator(this string? input) { if (input == null) return input; const string accellerator = "_"; // This is the default WPF accellerator symbol - used to be & in WinForms // If it already contains an accellerator, do nothing if (input.Contains(accellerator)) return input; return accellerator + input; } /// /// Removes the icon from the given window. /// See https://stackoverflow.com/a/18581096 /// /// The window to remove the icon from. internal 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); } } }