/// <summary> /// Registers the an element within a window, allowing the user to use it to drag the window. /// Make sure that this method is called after the window has loaded; placing it in the /// window's constructor will not work. /// </summary> /// <param name="window">The window.</param> /// <param name="element">The element.</param> public static void Register(Window window, UIElement element) { var windowHandle = new WindowInteropHelper(window).Handle; if (windowHandle.ToInt64() > 0) { element.MouseMove += delegate(object sender, MouseEventArgs e) { if (e.Source == element) { Action moveWindow = () => { if (e.LeftButton == MouseButtonState.Pressed) { ReleaseCapture(); SendMessage( windowHandle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); } }; // This may seem a bit odd; I was encountering an odd exception with the // moveWindow code when I did not use the dispatcher and tried to drag off // of a control that was not the window itself. // More details here: http://stackoverflow.com/questions/7442943/dispatcher-throws-invalidoperationexception-on-messagebox-show-in-textchanged-ev#answer-7443519 Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.Normal, moveWindow); } }; } }
/// <summary> /// Flashes the window in the task bar. /// </summary> /// <param name="window">The Window instance.</param> /// <returns></returns> public static bool Flash(this Window window) { IntPtr hWnd = new WindowInteropHelper(window).Handle; if (hWnd.ToInt64() != 0) { FLASHWINFO fInfo = new FLASHWINFO(); fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo)); fInfo.hwnd = hWnd; fInfo.dwFlags = FLASHW_TIMER | FLASHW_TRAY; fInfo.uCount = 3; fInfo.dwTimeout = 0; return FlashWindowEx(ref fInfo); } return false; }
/// <summary> /// Hides the icon in the window title bar. /// </summary> /// <param name="window">The Window instance.</param> public static void HideIcon(this Window window) { IntPtr hwnd = new WindowInteropHelper(window).Handle; if (hwnd.ToInt64() == 0) { window.SourceInitialized += (s, e) => HideIcon(window); } else { uint exStyle = GetWindowLong(hwnd, GWL_EXSTYLE); SetWindowLong(hwnd, GWL_EXSTYLE, exStyle | WS_EX_DLGMODALFRAME); SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); SendMessage(hwnd, WM_SETICON, new IntPtr(1), IntPtr.Zero); // Important if there's a native icon resource in the .exe file SendMessage(hwnd, WM_SETICON, IntPtr.Zero, IntPtr.Zero); } }
/// <summary> /// Hides the maximize button in the window title bar. /// </summary> /// <param name="window">The Window instance.</param> public static void HideMaximizeBox(this Window window) { IntPtr hwnd = new WindowInteropHelper(window).Handle; if (hwnd.ToInt64() == 0) { window.SourceInitialized += (s, e) => HideMaximizeBox(window); } else { SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~(WS_MAXIMIZEBOX)); } }