bovender framework
C# framework that implements MVVM and more
WindowExtensions.cs
1 /* WindowExtensions.cs
2  * part of Bovender framework
3  *
4  * Copyright 2014-2018 Daniel Kraus
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 using System;
19 using System.Windows;
20 using System.Windows.Interop;
21 
22 namespace Bovender.Extensions
23 {
27  public static class WindowExtensions
28  {
29  #region Public extension methods for Window
30 
36  public static void SetOwnerForm(this Window window, IntPtr ownerForm)
37  {
38  // This method should not throw an exception when ownerForm is zero.
39  if (ownerForm != IntPtr.Zero)
40  {
41  WindowInteropHelper h = new WindowInteropHelper(window);
42  h.Owner = ownerForm;
43  }
44  }
45 
49  public static bool? ShowDialogInForm(this Window window, IntPtr parentForm)
50  {
51  try
52  {
53  window.SetOwnerForm(parentForm);
54  return window.ShowDialog();
55  }
56  catch (Exception e)
57  {
58  Logger.Warn("ShowDialogInForm: Could not show dialog with owner form 0x{0:X08}; falling back to show dialog without owner",
59  parentForm);
60  Logger.Warn(e);
61  window.SetOwnerForm(IntPtr.Zero);
62  return window.ShowDialog();
63  }
64  }
65 
70  public static bool? ShowDialogInForm(this Window window)
71  {
72  IntPtr mainWindowHandle = IntPtr.Zero;
74  {
75  mainWindowHandle = Win32Window.MainWindowHandleProvider();
76  }
77  else
78  {
79  Logger.Warn("ShowDialogInForm: Win32Window.MainWindowHandleProvider is not set!");
80  }
81  if (mainWindowHandle != IntPtr.Zero)
82  {
83  return window.ShowDialogInForm(mainWindowHandle);
84  }
85  else
86  {
87  Logger.Warn("ShowDialogInForm: No form handle, falling back to showing dialog outside of form!");
88  return window.ShowDialog();
89  }
90  }
91 
95  public static void ShowInForm(this Window window, IntPtr parentForm)
96  {
97  try
98  {
99  window.SetOwnerForm(parentForm);
100  window.Show();
101  }
102  catch (Exception e)
103  {
104  Logger.Warn("ShowInForm: Could not show window with owner form 0x{0:X08}; falling back to show dialog without owner",
105  parentForm);
106  Logger.Warn(e);
107  window.SetOwnerForm(IntPtr.Zero);
108  window.Show();
109  }
110  }
111 
116  public static void ShowInForm(this Window window)
117  {
118  window.ShowInForm((new Win32Window(window)).Handle);
119  }
120 
121  #endregion
122 
123  #region Class logger
124 
125  private static NLog.Logger Logger { get { return _logger.Value; } }
126 
127  private static readonly Lazy<NLog.Logger> _logger = new Lazy<NLog.Logger>(() => NLog.LogManager.GetCurrentClassLogger());
128 
129  #endregion
130  }
131 }
static Func< IntPtr > MainWindowHandleProvider
Gets or sets a function that provides the global window handle of the main window of an application...
Definition: Win32Window.cs:45
Provides access to the window handle of a WPF window or a Form or a custom handle.
Definition: Win32Window.cs:32
Extension methods for the WPF Window class.
static void ShowInForm(this Window window, IntPtr parentForm)
Shows the Window as a dialog that belongs to a Windows form parent.
static void ShowInForm(this Window window)
Shows the Window as a dialog that belongs to a Windows form parent.
static bool ShowDialogInForm(this Window window, IntPtr parentForm)
Shows the Window as a dialog that belongs to a Windows form parent.
static void SetOwnerForm(this Window window, IntPtr ownerForm)
Make a Windows Form the Owner of a WPF Window.
static bool ShowDialogInForm(this Window window)
Shows the Window as a dialog that belongs to a Windows form parent.