Last active
March 21, 2019 07:11
-
-
Save draqoon/3a39f35b0702da87aa7e6f2ada80b844 to your computer and use it in GitHub Desktop.
[WPF][ウインドウを最大化した時のサイズと位置を制限する Behavior とその使用サンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="WpfApp1.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" | |
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" | |
xmlns:local="clr-namespace:WpfApp1" | |
mc:Ignorable="d" | |
Title="MainWindow" | |
Height="450" | |
Width="800"> | |
<i:Interaction.Behaviors> | |
<local:MaximizedWindowBehavior MaximizedBounds="200,500,1920,1080" /> | |
</i:Interaction.Behaviors> | |
<Grid> | |
</Grid> | |
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel; | |
using System.Runtime.InteropServices; | |
using System.Windows; | |
using System.Windows.Interactivity; | |
using System.Windows.Interop; | |
namespace WpfApp1 { | |
class MaximizedWindowBehavior: Behavior<Window> { | |
#region MaximizedBounds Property | |
public Rect MaximizedBounds { | |
get => (Rect)this.GetValue( MaximizedBoundsProperty ); | |
set => this.SetValue( MaximizedBoundsProperty, value ); | |
} | |
public static readonly DependencyProperty MaximizedBoundsProperty = | |
DependencyProperty.Register( nameof( MaximizedBounds ), typeof( Rect ), typeof( MaximizedWindowBehavior ), new PropertyMetadata( Rect.Empty ) ); | |
#endregion | |
public MaximizedWindowBehavior() { } | |
protected override void OnAttached() { | |
base.OnAttached(); | |
this.AssociatedObject.Loaded += this.WindowLoaded; | |
this.AssociatedObject.Closing += this.WindowClosing; | |
} | |
protected override void OnDetaching() { | |
this.AssociatedObject.Loaded -= this.WindowLoaded; | |
this.AssociatedObject.Closing -= this.WindowClosing; | |
base.OnDetaching(); | |
} | |
private void WindowLoaded( object sender, RoutedEventArgs e ) { | |
var hwnd = new WindowInteropHelper( this.AssociatedObject ).Handle; | |
var source = HwndSource.FromHwnd( hwnd ); | |
source.AddHook( this.WndProc ); | |
} | |
private void WindowClosing( object sender, CancelEventArgs e ) { | |
var hwnd = new WindowInteropHelper( this.AssociatedObject ).Handle; | |
var source = HwndSource.FromHwnd( hwnd ); | |
source.RemoveHook( this.WndProc ); | |
} | |
#region Win32 | |
private struct POINT { | |
public int x; | |
public int y; | |
} | |
#pragma warning disable CS0649 | |
private struct MINMAXINFO { | |
public POINT ptReserved; | |
public POINT ptMaxSize; // 最大化フォームのサイズ | |
public POINT ptMaxPosition; // 最大化フォームの位置 | |
public POINT ptMinTrackSize; // フォームの最大サイズ | |
public POINT ptMaxTrackSize; // フォームの最小サイズ | |
} | |
#pragma warning restore CS0649 | |
private const int WM_GETMINMAXINFO = 0x0024; | |
#endregion | |
private IntPtr WndProc( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled ) { | |
if( msg == WM_GETMINMAXINFO ) { | |
handled = true; | |
var info = Marshal.PtrToStructure<MINMAXINFO>( lParam ); | |
info.ptMaxPosition.x = (int)this.MaximizedBounds.Left; | |
info.ptMaxPosition.y = (int)this.MaximizedBounds.Top; | |
if( 0 < this.MaximizedBounds.Width ) | |
info.ptMaxSize.x = (int)this.MaximizedBounds.Width; | |
if( 0 < this.MaximizedBounds.Height ) | |
info.ptMaxSize.y = (int)this.MaximizedBounds.Height; | |
Marshal.StructureToPtr( info, lParam, true ); | |
return IntPtr.Zero; | |
} | |
return IntPtr.Zero; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment