0

I have a WPF app with tear off windows and I want to be able to hide the minimize button (on a tear off) - I don't want to hide the maximize, only the minimize. This SO question shows how to hide BOTH, but when I change to code just to hide the minimize it is displayed but disabled.

This only disables the button:

 internal static void HideMinimizeButtons(this Window window)
 {
     var hwnd = new WindowInteropHelper(window).Handle;
     var currentStyle = GetWindowLong(hwnd, GWL_STYLE);

     SetWindowLong(hwnd, GWL_STYLE, (currentStyle & ~WS_MINIMIZEBOX));
 }

How can I get it to hide the minimize button only?

Community
  • 1
  • 1
AwkwardCoder
  • 24,893
  • 27
  • 82
  • 152
  • 1
    Doesn't exactly answer your issue, rather than writing some tricky and flaky code...I would use your above code to first disable the minimize button and then put some solid color image on top of minimize button :) – ganesh Mar 12 '14 at 17:05
  • that sir is the definition of a 'hack' :) – AwkwardCoder Mar 12 '14 at 18:42

1 Answers1

3

AFAIK the Minimum and Maximum buttons can be hidden as a group, but not individually.

Even back in the Windows Forms days, the MimimizeBox property worked this way.

this.MinimizeBox = false;  // still visible, but disabled
this.MaximizeBox = false;  // add this line and both buttons disappear 

The System menu is a different story. It's possible to hide a menu item with this code.

CODE

public partial class MainWindow : Window {
    private const int MF_BYPOSITION = 0x400;

    [DllImport("User32")]
    private static extern int RemoveMenu(IntPtr hMenu, int position, int flags);
    [DllImport("User32")]
    private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool revert);
    [DllImport("User32")]
    private static extern int GetMenuItemCount(IntPtr hWnd);

    private enum SystemMenu : int {
      Restore,
      Move,
      Size,
      Minimize,
      Maximize
    }

    public MainWindow() {
      InitializeComponent();

      this.Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e) {
      WindowInteropHelper helper = new WindowInteropHelper(this);
      IntPtr menuPtr = GetSystemMenu(helper.Handle, false);
      int menuItemCount = GetMenuItemCount(menuPtr);
      RemoveMenu(menuPtr, (int)SystemMenu.Minimize, MF_BYPOSITION);
    }
}

Before Screenshot

enter image description here

After Screenshot

enter image description here

Using the

SetWindowLong(hwnd, GWL_STYLE, (currentStyle & ~WS_MINIMIZEBOX));

disables both the menu and the button.

This prevents the user from minimizing the tear off windows, but doesn't have the look you want.

I've seen replacement titles bars used to solve this issue, but its a lot of work to get right.

Walt Ritscher
  • 6,977
  • 1
  • 28
  • 35