WPF 固定窗口大小
1.自定义DependencyProperty:
public static class WPFWindowHelper { private const Int32 GWL_STYLE = -16; private const Int32 WS_MAXIMIZEBOX = 0x00010000; private const Int32 WS_MINIMIZEBOX = 0x00020000; [DllImport("User32.dll", EntryPoint = "GetWindowLong")] private extern static Int32 GetWindowLongPtr(IntPtr hWnd, Int32 nIndex); [DllImport("User32.dll", EntryPoint = "SetWindowLong")] private extern static Int32 SetWindowLongPtr(IntPtr hWnd, Int32 nIndex, Int32 dwNewLong); /// <summary> /// Disables the maximize functionality of a WPF window. /// </summary> /// <param name="window">The WPF window to be modified.</param> public static void DisableMaximize(Window window) { lock (window) { IntPtr hWnd = new WindowInteropHelper(window).Handle; Int32 windowStyle = GetWindowLongPtr(hWnd, GWL_STYLE); SetWindowLongPtr(hWnd, GWL_STYLE, windowStyle & ~WS_MAXIMIZEBOX); } } /// <summary> /// Disables the minimize functionality of a WPF window. /// </summary> /// <param name="window">The WPF window to be modified.</param> public static void DisableMinimize(Window window) { lock (window) { IntPtr hWnd = new WindowInteropHelper(window).Handle; Int32 windowStyle = GetWindowLongPtr(hWnd, GWL_STYLE); SetWindowLongPtr(hWnd, GWL_STYLE, windowStyle & ~WS_MINIMIZEBOX); } } /// <summary> /// Enables the maximize functionality of a WPF window. /// </summary> /// <param name="window">The WPF window to be modified.</param> public static void EnableMaximize(Window window) { lock (window) { IntPtr hWnd = new WindowInteropHelper(window).Handle; Int32 windowStyle = GetWindowLongPtr(hWnd, GWL_STYLE); SetWindowLongPtr(hWnd, GWL_STYLE, windowStyle | WS_MAXIMIZEBOX); } } /// <summary> /// Enables the minimize functionality of a WPF window. /// </summary> /// <param name="window">The WPF window to be modified.</param> public static void EnableMinimize(Window window) { lock (window) { IntPtr hWnd = new WindowInteropHelper(window).Handle; Int32 windowStyle = GetWindowLongPtr(hWnd, GWL_STYLE); SetWindowLongPtr(hWnd, GWL_STYLE, windowStyle | WS_MINIMIZEBOX); } } /// <summary> /// Toggles the enabled state of a WPF window's maximize functionality. /// </summary> /// <param name="window">The WPF window to be modified.</param> public static void ToggleMaximize(Window window) { lock (window) { IntPtr hWnd = new WindowInteropHelper(window).Handle; Int32 windowStyle = GetWindowLongPtr(hWnd, GWL_STYLE); if ((windowStyle | WS_MAXIMIZEBOX) == windowStyle) { SetWindowLongPtr(hWnd, GWL_STYLE, windowStyle & ~WS_MAXIMIZEBOX); } else { SetWindowLongPtr(hWnd, GWL_STYLE, windowStyle | WS_MAXIMIZEBOX); } } } /// <summary> /// Toggles the enabled state of a WPF window's minimize functionality. /// </summary> /// <param name="window">The WPF window to be modified.</param> public static void ToggleMinimize(Window window) { lock (window) { IntPtr hWnd = new WindowInteropHelper(window).Handle; Int32 windowStyle = GetWindowLongPtr(hWnd, GWL_STYLE); if ((windowStyle | WS_MINIMIZEBOX) == windowStyle) { SetWindowLongPtr(hWnd, GWL_STYLE, windowStyle & ~WS_MINIMIZEBOX); } else { SetWindowLongPtr(hWnd, GWL_STYLE, windowStyle | WS_MINIMIZEBOX); } } } }
public static class WindowCustomizer { #region CanMaximize public static readonly DependencyProperty CanMaximize = DependencyProperty.RegisterAttached("CanMaximize", typeof(bool), typeof(Window), new PropertyMetadata(true, new PropertyChangedCallback(OnCanMaximizeChanged))); private static void OnCanMaximizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Window window = d as Window; if (window != null) { RoutedEventHandler loadedHandler = null; loadedHandler = delegate { if ((bool)e.NewValue) { //WindowHelper. WPFWindowHelper.EnableMaximize(window); } else { WPFWindowHelper.DisableMaximize(window); } window.Loaded -= loadedHandler; }; if (!window.IsLoaded) { window.Loaded += loadedHandler; } else { loadedHandler(null, null); } } } public static void SetCanMaximize(DependencyObject d, bool value) { d.SetValue(CanMaximize, value); } public static bool GetCanMaximize(DependencyObject d) { return (bool)d.GetValue(CanMaximize); } #endregion CanMaximize #region CanMinimize public static readonly DependencyProperty CanMinimize = DependencyProperty.RegisterAttached("CanMinimize", typeof(bool), typeof(Window), new PropertyMetadata(true, new PropertyChangedCallback(OnCanMinimizeChanged))); private static void OnCanMinimizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Window window = d as Window; if (window != null) { RoutedEventHandler loadedHandler = null; loadedHandler = delegate { if ((bool)e.NewValue) { WPFWindowHelper.EnableMinimize(window); } else { WPFWindowHelper.DisableMinimize(window); } window.Loaded -= loadedHandler; }; if (!window.IsLoaded) { window.Loaded += loadedHandler; } else { loadedHandler(null, null); } } } public static void SetCanMinimize(DependencyObject d, bool value) { d.SetValue(CanMinimize, value); } public static bool GetCanMinimize(DependencyObject d) { return (bool)d.GetValue(CanMinimize); } #endregion CanMinimize }
2.在xaml中设置一些属性:
<Window x:Class="Sleven.WPF.WindowTest.CustomSizeWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:slevenwpfcustom="clr-namespace:Sleven.WPF.Custom" ResizeMode="CanResize" Height="322" Width="397" DataContext="{Binding}" Loaded="Window_Loaded" Title="CustomSizeWindow"> <!-- xmlns:local="clr-namespace:TestlogAnalysis" local:WindowCustomizer.CanMaximize="False" ResizeMode="CanResize" Title="Testlog Analysis" Height="322" Width="397" DataContext="{Binding}" Loaded="Window_Loaded"> --> <Grid> </Grid> </Window>
public partial class CustomSizeWindow : Window { private readonly int minWidth = 800; private readonly int minHeight = 600; public CustomSizeWindow() { InitializeComponent(); } protected void Window_Loaded(object sender, RoutedEventArgs e) { //this.MaxHeight = this.Height; this.MinHeight = minHeight; //this.MaxWidth = this.Width; this.MinWidth = minWidth; } }

浙公网安备 33010602011771号