How to remove minimization and maximize from resizable window in WPF?

WPF does not provide the ability to have a window that allows you to resize, but does not have an increase or decrease button. I would like to make such a window so that I have resized dialog boxes.

I know that a solution will mean using pinvoke, but I'm not sure what to call and how. Finding pinvoke.net did not bring me anything that I needed, basically I'm sure because Windows Forms provides the CanMinimize and CanMaximize in its windows.

Can someone point me or provide a code (preferred C # option) on how to do this?

+66
user-interface resize wpf pinvoke
Dec 04 '08 at 4:51
source share
6 answers

I stole the code I found on the MSDN forums and made an extension method in the Window class, for example:

 internal static class WindowExtensions { // from winuser.h private const int GWL_STYLE = -16, WS_MAXIMIZEBOX = 0x10000, WS_MINIMIZEBOX = 0x20000; [DllImport("user32.dll")] extern private static int GetWindowLong(IntPtr hwnd, int index); [DllImport("user32.dll")] extern private static int SetWindowLong(IntPtr hwnd, int index, int value); internal static void HideMinimizeAndMaximizeButtons(this Window window) { IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(window).Handle; var currentStyle = GetWindowLong(hwnd, GWL_STYLE); SetWindowLong(hwnd, GWL_STYLE, (currentStyle & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX)); } } 

The only thing to remember is that for some reason this does not work from the window constructor. I went around this by inserting this into the constructor:

 this.SourceInitialized += (x, y) => { this.HideMinimizeAndMaximizeButtons(); }; 

Hope this helps!

+92
Dec 04 '08 at 5:00
source share

One way is to set ResizeMode="NoResize" . He will behave like that. enter image description here

Hope this helps!

+72
Oct 06 2018-11-11T00:
source share

ResizableToolWindow.jpg

Don't know if this works for your req. visually .. This

 <Window x:Class="DataBinding.MyWindow" ...Title="MyWindow" Height="300" Width="300" WindowStyle="ToolWindow" ResizeMode="CanResizeWithGrip"> 
+16
Dec 04 '08 at 5:33
source share

Here is the solution I am using. Note that the maximize button is still displayed.

Markup:

 <Window x:Class="Example" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Example" StateChanged="Window_StateChanged"> 

Code behind:

 // Disable maximizing this window private void Window_StateChanged(object sender, EventArgs e) { if (this.WindowState == WindowState.Maximized) this.WindowState = WindowState.Normal; } 
+3
May 10 '11 at 19:33
source share

If someone uses the Devexpress window (DXWindow), the accepted answer does not work. One ugly approach -

 public partial class MyAwesomeWindow : DXWindow { public MyAwesomeWIndow() { Loaded += OnLoaded; } private void OnLoaded(object sender, RoutedEventArgs routedEventArgs) { // hides maximize button Button button = (Button)DevExpress.Xpf.Core.Native.LayoutHelper.FindElementByName(this, DXWindow.ButtonParts.PART_Maximize.ToString()); button.IsHitTestVisible = false; button.Opacity = 0; // hides minimize button button = (Button)DevExpress.Xpf.Core.Native.LayoutHelper.FindElementByName(this, DXWindow.ButtonParts.PART_Minimize.ToString()); button.IsHitTestVisible = false; button.Opacity = 0; // hides close button button = (Button)DevExpress.Xpf.Core.Native.LayoutHelper.FindElementByName(this, DXWindow.ButtonParts.PART_CloseButton.ToString()); button.IsHitTestVisible = false; button.Opacity = 0; } } 
+3
Mar 28 '13 at 9:52
source share

You can set the ResizeMode = "NoResize" window if you want to remove the minimize and enlarge button

+2
Sep 15 '10 at 4:38
source share



All Articles