code_junkie

How do I remove minimize and maximize from a resizable window in WPF?

Nov 12th, 2011
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. internal static class WindowExtensions
  2. {
  3. [DllImport("user32.dll")]
  4. internal extern static int SetWindowLong(IntPtr hwnd, int index, int value);
  5.  
  6. [DllImport("user32.dll")]
  7. internal extern static int GetWindowLong(IntPtr hwnd, int index);
  8.  
  9. internal static void HideMinimizeAndMaximizeButtons(this Window window)
  10. {
  11. const int GWL_STYLE = -16;
  12.  
  13. IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(window).Handle;
  14. long value = GetWindowLong(hwnd, GWL_STYLE);
  15.  
  16. SetWindowLong(hwnd, GWL_STYLE, (int)(value & -131073 & -65537));
  17.  
  18. }
  19. }
  20.  
  21. this.SourceInitialized += (x, y) =>
  22. {
  23. this.HideMinimizeAndMaximizeButtons();
  24. };
  25.  
  26. <Window x:Class="DataBinding.MyWindow" ...Title="MyWindow" Height="300" Width="300"
  27. WindowStyle="ToolWindow" ResizeMode="CanResizeWithGrip">
  28.  
  29. <Window x:Class="Example"
  30. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  31. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  32. Title="Example"
  33. StateChanged="Window_StateChanged">
  34.  
  35. // Disable maximizing this window
  36. private void Window_StateChanged(object sender, EventArgs e)
  37. {
  38. if (this.WindowState == WindowState.Maximized)
  39. this.WindowState = WindowState.Normal;
  40. }
Add Comment
Please, Sign In to add comment