回答編集履歴

1

Behavior版を追記しました。

2019/03/20 04:11

投稿

draq
draq

スコア2573

test CHANGED
@@ -6,6 +6,16 @@
6
6
 
7
7
 
8
8
 
9
+
10
+
11
+ (ベヘイビア版を追記しました。)
12
+
13
+
14
+
15
+ ---
16
+
17
+ (コードビハインド版)
18
+
9
19
  - MainWindow.xaml
10
20
 
11
21
  ```XAML
@@ -157,3 +167,247 @@
157
167
  }
158
168
 
159
169
  ```
170
+
171
+
172
+
173
+ ---
174
+
175
+ (Behavior版を追記)
176
+
177
+ - MainWindow.xaml
178
+
179
+ ```XAML
180
+
181
+ <Window x:Class="WpfApp1.MainWindow"
182
+
183
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
184
+
185
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
186
+
187
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
188
+
189
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
190
+
191
+ xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
192
+
193
+ xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
194
+
195
+ xmlns:local="clr-namespace:WpfApp1"
196
+
197
+ mc:Ignorable="d"
198
+
199
+ Title="MainWindow"
200
+
201
+ Height="450"
202
+
203
+ Width="800">
204
+
205
+
206
+
207
+ <i:Interaction.Behaviors>
208
+
209
+ <local:MaximizedWindowBehavior MaximizedBounds="200,500,1920,1080" />
210
+
211
+ </i:Interaction.Behaviors>
212
+
213
+
214
+
215
+ <Grid>
216
+
217
+ </Grid>
218
+
219
+ </Window>
220
+
221
+ ```
222
+
223
+
224
+
225
+ - MainWindow.xaml.cs
226
+
227
+ 既定のまま変更なし
228
+
229
+
230
+
231
+ - MaximizedWindowBehavior.cs
232
+
233
+ ```C#
234
+
235
+ using System;
236
+
237
+ using System.ComponentModel;
238
+
239
+ using System.Runtime.InteropServices;
240
+
241
+ using System.Windows;
242
+
243
+ using System.Windows.Interactivity;
244
+
245
+ using System.Windows.Interop;
246
+
247
+
248
+
249
+ namespace WpfApp1 {
250
+
251
+ class MaximizedWindowBehavior: Behavior<Window> {
252
+
253
+
254
+
255
+ #region MaximizedBounds Property
256
+
257
+
258
+
259
+ public Rect MaximizedBounds {
260
+
261
+ get => (Rect)this.GetValue( MaximizedBoundsProperty );
262
+
263
+ set => this.SetValue( MaximizedBoundsProperty, value );
264
+
265
+ }
266
+
267
+
268
+
269
+ public static readonly DependencyProperty MaximizedBoundsProperty =
270
+
271
+ DependencyProperty.Register( nameof( MaximizedBounds ), typeof( Rect ), typeof( MaximizedWindowBehavior ), new PropertyMetadata( Rect.Empty ) );
272
+
273
+
274
+
275
+ #endregion
276
+
277
+
278
+
279
+ public MaximizedWindowBehavior() { }
280
+
281
+
282
+
283
+ protected override void OnAttached() {
284
+
285
+ base.OnAttached();
286
+
287
+ this.AssociatedObject.Loaded += this.WindowLoaded;
288
+
289
+ this.AssociatedObject.Closing += this.WindowClosing;
290
+
291
+ }
292
+
293
+
294
+
295
+ protected override void OnDetaching() {
296
+
297
+ this.AssociatedObject.Loaded -= this.WindowLoaded;
298
+
299
+ this.AssociatedObject.Closing -= this.WindowClosing;
300
+
301
+ base.OnDetaching();
302
+
303
+ }
304
+
305
+
306
+
307
+ private void WindowLoaded( object sender, RoutedEventArgs e ) {
308
+
309
+ var hwnd = new WindowInteropHelper( this.AssociatedObject ).Handle;
310
+
311
+ var source = HwndSource.FromHwnd( hwnd );
312
+
313
+ source.AddHook( this.WndProc );
314
+
315
+ }
316
+
317
+
318
+
319
+ private void WindowClosing( object sender, CancelEventArgs e ) {
320
+
321
+ var hwnd = new WindowInteropHelper( this.AssociatedObject ).Handle;
322
+
323
+ var source = HwndSource.FromHwnd( hwnd );
324
+
325
+ source.RemoveHook( this.WndProc );
326
+
327
+ }
328
+
329
+
330
+
331
+ #region Win32
332
+
333
+
334
+
335
+ private struct POINT {
336
+
337
+ public int x;
338
+
339
+ public int y;
340
+
341
+ }
342
+
343
+
344
+
345
+ #pragma warning disable CS0649
346
+
347
+ private struct MINMAXINFO {
348
+
349
+ public POINT ptReserved;
350
+
351
+ public POINT ptMaxSize; // 最大化フォームのサイズ
352
+
353
+ public POINT ptMaxPosition; // 最大化フォームの位置
354
+
355
+ public POINT ptMinTrackSize; // フォームの最大サイズ
356
+
357
+ public POINT ptMaxTrackSize; // フォームの最小サイズ
358
+
359
+ }
360
+
361
+ #pragma warning restore CS0649
362
+
363
+
364
+
365
+ private const int WM_GETMINMAXINFO = 0x0024;
366
+
367
+
368
+
369
+ #endregion
370
+
371
+
372
+
373
+ private IntPtr WndProc( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled ) {
374
+
375
+ if( msg == WM_GETMINMAXINFO ) {
376
+
377
+ handled = true;
378
+
379
+
380
+
381
+ var info = Marshal.PtrToStructure<MINMAXINFO>( lParam );
382
+
383
+ info.ptMaxPosition.x = (int)this.MaximizedBounds.Left;
384
+
385
+ info.ptMaxPosition.y = (int)this.MaximizedBounds.Top;
386
+
387
+ if( 0 < this.MaximizedBounds.Width )
388
+
389
+ info.ptMaxSize.x = (int)this.MaximizedBounds.Width;
390
+
391
+ if( 0 < this.MaximizedBounds.Height )
392
+
393
+ info.ptMaxSize.y = (int)this.MaximizedBounds.Height;
394
+
395
+ Marshal.StructureToPtr( info, lParam, true );
396
+
397
+
398
+
399
+ return IntPtr.Zero;
400
+
401
+ }
402
+
403
+ return IntPtr.Zero;
404
+
405
+ }
406
+
407
+
408
+
409
+ }
410
+
411
+ }
412
+
413
+ ```