課題
画面を拡大した時の最大サイズの指定仕方に苦戦をしています。(最大サイズを1024×768にしたい。)
使用している手段
ViewをwpfのXAMLのPageタグ、ViewModel,ModelをC♯で開発中
###フレームワーク
.NET Framework 4.5.2
試したこと
画面の拡大ボタンを押下時にイベント「SizeChanged」でプロパティ「WindowHeight, WindowWideth」の値を変更するイベントを作成した結果、下記の状態なった。
現在の状態
小サイズ⇨画面の最大サイズ⇨1024×768⇨画面の最大サイズ⇨小サイズ⇨・・・
解決方法・ヒントがわかる方ございましたらご指導のほどよろしくお願い致します。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答2件
0
ベストアンサー
Win32APIの利用を許容できるなら、WM_GETMINMAXINFO メッセージ受信時に最大化したウインドウのサイズと位置を指定することができます。
下記のサンプルコードはこちらの回答を WPF 向けに書き換えたものです。
(ベヘイビア版を追記しました。)
(コードビハインド版)
- MainWindow.xaml
XAML
1<Window x:Class="WpfApp1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp1" 7 mc:Ignorable="d" 8 Title="MainWindow" 9 Height="450" 10 Width="800" 11 Loaded="Window_Loaded"> 12 <Grid> 13 </Grid> 14</Window>
- MainWindow.xaml.cs
C#
1using System; 2using System.Runtime.InteropServices; 3using System.Windows; 4using System.Windows.Interop; 5 6namespace WpfApp1 { 7 public partial class MainWindow: Window { 8 9 public MainWindow() { 10 InitializeComponent(); 11 } 12 13 private void Window_Loaded( object sender, RoutedEventArgs e ) { 14 var hwnd = new WindowInteropHelper( this ).Handle; 15 var source = HwndSource.FromHwnd( hwnd ); 16 source.AddHook( this.WndProc ); 17 } 18 19 #region Win32 20 21 private struct POINT { 22 public int x; 23 public int y; 24 } 25 26 private struct MINMAXINFO { 27 public POINT ptReserved; 28 public POINT ptMaxSize; // 最大化フォームのサイズ 29 public POINT ptMaxPosition; // 最大化フォームの位置 30 public POINT ptMinTrackSize; // フォームの最大サイズ 31 public POINT ptMaxTrackSize; // フォームの最小サイズ 32 } 33 34 private const int WM_GETMINMAXINFO = 0x0024; 35 36 #endregion 37 38 private IntPtr WndProc( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled ) { 39 if( msg == WM_GETMINMAXINFO ) { 40 handled = true; 41 42 var info = Marshal.PtrToStructure<MINMAXINFO>( lParam ); 43 info.ptMaxPosition.x = 300; 44 info.ptMaxPosition.y = 100; 45 info.ptMaxSize.x = 1024; 46 info.ptMaxSize.y = 768; 47 Marshal.StructureToPtr( info, lParam, true ); 48 49 return IntPtr.Zero; 50 } 51 return IntPtr.Zero; 52 } 53 54 } 55}
(Behavior版を追記)
- MainWindow.xaml
XAML
1<Window x:Class="WpfApp1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 7 xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 8 xmlns:local="clr-namespace:WpfApp1" 9 mc:Ignorable="d" 10 Title="MainWindow" 11 Height="450" 12 Width="800"> 13 14 <i:Interaction.Behaviors> 15 <local:MaximizedWindowBehavior MaximizedBounds="200,500,1920,1080" /> 16 </i:Interaction.Behaviors> 17 18 <Grid> 19 </Grid> 20</Window>
- MainWindow.xaml.cs
既定のまま変更なし
- MaximizedWindowBehavior.cs
C#
1using System; 2using System.ComponentModel; 3using System.Runtime.InteropServices; 4using System.Windows; 5using System.Windows.Interactivity; 6using System.Windows.Interop; 7 8namespace WpfApp1 { 9 class MaximizedWindowBehavior: Behavior<Window> { 10 11 #region MaximizedBounds Property 12 13 public Rect MaximizedBounds { 14 get => (Rect)this.GetValue( MaximizedBoundsProperty ); 15 set => this.SetValue( MaximizedBoundsProperty, value ); 16 } 17 18 public static readonly DependencyProperty MaximizedBoundsProperty = 19 DependencyProperty.Register( nameof( MaximizedBounds ), typeof( Rect ), typeof( MaximizedWindowBehavior ), new PropertyMetadata( Rect.Empty ) ); 20 21 #endregion 22 23 public MaximizedWindowBehavior() { } 24 25 protected override void OnAttached() { 26 base.OnAttached(); 27 this.AssociatedObject.Loaded += this.WindowLoaded; 28 this.AssociatedObject.Closing += this.WindowClosing; 29 } 30 31 protected override void OnDetaching() { 32 this.AssociatedObject.Loaded -= this.WindowLoaded; 33 this.AssociatedObject.Closing -= this.WindowClosing; 34 base.OnDetaching(); 35 } 36 37 private void WindowLoaded( object sender, RoutedEventArgs e ) { 38 var hwnd = new WindowInteropHelper( this.AssociatedObject ).Handle; 39 var source = HwndSource.FromHwnd( hwnd ); 40 source.AddHook( this.WndProc ); 41 } 42 43 private void WindowClosing( object sender, CancelEventArgs e ) { 44 var hwnd = new WindowInteropHelper( this.AssociatedObject ).Handle; 45 var source = HwndSource.FromHwnd( hwnd ); 46 source.RemoveHook( this.WndProc ); 47 } 48 49 #region Win32 50 51 private struct POINT { 52 public int x; 53 public int y; 54 } 55 56#pragma warning disable CS0649 57 private struct MINMAXINFO { 58 public POINT ptReserved; 59 public POINT ptMaxSize; // 最大化フォームのサイズ 60 public POINT ptMaxPosition; // 最大化フォームの位置 61 public POINT ptMinTrackSize; // フォームの最大サイズ 62 public POINT ptMaxTrackSize; // フォームの最小サイズ 63 } 64#pragma warning restore CS0649 65 66 private const int WM_GETMINMAXINFO = 0x0024; 67 68 #endregion 69 70 private IntPtr WndProc( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled ) { 71 if( msg == WM_GETMINMAXINFO ) { 72 handled = true; 73 74 var info = Marshal.PtrToStructure<MINMAXINFO>( lParam ); 75 info.ptMaxPosition.x = (int)this.MaximizedBounds.Left; 76 info.ptMaxPosition.y = (int)this.MaximizedBounds.Top; 77 if( 0 < this.MaximizedBounds.Width ) 78 info.ptMaxSize.x = (int)this.MaximizedBounds.Width; 79 if( 0 < this.MaximizedBounds.Height ) 80 info.ptMaxSize.y = (int)this.MaximizedBounds.Height; 81 Marshal.StructureToPtr( info, lParam, true ); 82 83 return IntPtr.Zero; 84 } 85 return IntPtr.Zero; 86 } 87 88 } 89}
投稿2019/03/19 16:29
編集2019/03/20 04:11総合スコア2573
0
期待する動きかはわかりませんが、
WindowのMaxWidth、MaxHeightをXAML上で指定してみてはいかがでしょうか?
投稿2019/03/19 08:33
総合スコア8356
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/19 08:37 編集
2019/03/19 08:38
2019/03/19 08:40
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。