代码之家  ›  专栏  ›  技术社区  ›  VolkerK

如何从WPF中的可调整大小的窗口中删除最小化和最大化?

  •  72
  • VolkerK  · 技术社区  · 17 年前

    WPF不提供允许调整大小但没有最大化或最小化按钮的窗口。我想能够使这样一个窗口,这样我就可以有大小可调的对话框。

    CanMinimize CanMaximize

    7 回复  |  直到 5 年前
        1
  •  121
  •   Ian Kemp    13 年前

    我偷了一些在MSDN论坛上找到的代码,并在Window类上创建了一个扩展方法,如下所示:

    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));
        }
    }
    

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

        2
  •  102
  •   Afzaal Ahmad Zeeshan    12 年前

    ResizeMode="NoResize" enter image description here

    我希望这有帮助!

        3
  •  23
  •   Community Mohan Dere    9 年前

    不知道这是否适用于您的请求。视觉上。。这是

    <Window x:Class="DataBinding.MyWindow" ...Title="MyWindow" Height="300" Width="300" 
        WindowStyle="ToolWindow" ResizeMode="CanResizeWithGrip">
    
        4
  •  6
  •   Wai Ha Lee captain-yossarian from Ukraine    10 年前

    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;
        } 
    }
    
        5
  •  3
  •   Andrej    15 年前

    这是我正在使用的解决方案。请注意,“最大化”按钮仍然显示。

    标记:

    <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">
    

    代码隐藏:

    // Disable maximizing this window
    private void Window_StateChanged(object sender, EventArgs e)
    {
        if (this.WindowState == WindowState.Maximized)
            this.WindowState = WindowState.Normal;
    }
    
        6
  •  2
  •   Chinmay Behera    15 年前

        7
  •  2
  •   Olivier Jacot-Descombes    7 年前

    solution proposed 通过@matt,可以(并且必须)在窗口的构造函数中调用。诀窍是向 SourceInitialized 扩展方法中的事件。

    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);
    
    /// <summary>
    /// Hides the Minimize and Maximize buttons in a Window. Must be called in the constructor.
    /// </summary>
    /// <param name="window">The Window whose Minimize/Maximize buttons will be hidden.</param>
    public static void HideMinimizeAndMaximizeButtons(this Window window)
    {
        window.SourceInitialized += (s, e) => {
            IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(window).Handle;
            int currentStyle = GetWindowLong(hwnd, GWL_STYLE);
    
            SetWindowLong(hwnd, GWL_STYLE, currentStyle & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX);
        };
    }