bovender framework
C# framework that implements MVVM and more
Win32Window.cs
1 /* Win32Window.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.Collections.Generic;
20 using System.Linq;
21 using System.Text;
22 using System.Windows;
23 using Forms = System.Windows.Forms;
24 using System.Windows.Interop;
25 
26 namespace Bovender
27 {
32  public class Win32Window : Forms.IWin32Window
33  {
34  #region Public static delegate
35 
45  public static Func<IntPtr> MainWindowHandleProvider { get; set; }
46 
47  #endregion
48 
49  #region Public property
50 
54  public IntPtr Handle
55  {
56  get
57  {
58  IntPtr h = _handle;
59  if (h == IntPtr.Zero)
60  {
61  if (MainWindowHandleProvider == null)
62  {
63  Logger.Debug("No handle has been set, and no handle provider exists");
64  h = IntPtr.Zero;
65  }
66  else
67  {
68  Logger.Debug("Obtaining handle from MainWindowHandleProvider");
70  }
71  }
72  Logger.Info("Using main window handle 0x{0:X08}", h.ToInt64()); // Convert to 64-bit just to make sure...
73  return h;
74  }
75  }
76 
77  #endregion
78 
79  #region Constructors
80 
85  public Win32Window()
86  {
87  Logger.Debug("Creating new Win32Window instance that uses the MainWindowHandleProvider");
88  }
89 
93  public Win32Window(Forms.Form form)
94  {
95  _handle = form.Handle;
96  Logger.Debug("Creating new Win32Window instance with the form handle 0x{0:X08}", _handle.ToInt64());
97  }
98 
103  public Win32Window(Window window)
104  {
105  _handle = (new WindowInteropHelper(window)).Handle;
106  Logger.Debug("Creating new Win32Window instance with the WPF window handle 0x{0:X08}", _handle.ToInt64());
107  }
108 
112  public Win32Window(IntPtr handle)
113  {
114  _handle = handle;
115  Logger.Debug("Creating new Win32Window instance with the WPF window handle 0x{0:X08}", _handle.ToInt64());
116  }
117 
118  #endregion
119 
120  #region Private fields
121 
122  private IntPtr _handle;
123 
124  #endregion
125 
126  #region Class logger
127 
128  private static NLog.Logger Logger { get { return _logger.Value; } }
129 
130  private static readonly Lazy<NLog.Logger> _logger = new Lazy<NLog.Logger>(() => NLog.LogManager.GetCurrentClassLogger());
131 
132  #endregion
133  }
134 }
Win32Window(IntPtr handle)
Creates a new instance that wraps a handle .
Definition: Win32Window.cs:112
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
Win32Window(Forms.Form form)
Creates a new instance whose handle is obtained from a Form.
Definition: Win32Window.cs:93
Win32Window()
Creates a new instance whose handle must be provided by the MainWindowHandleProvider ...
Definition: Win32Window.cs:85
IntPtr Handle
Gets the handle that is wrapped by this class.
Definition: Win32Window.cs:55
Win32Window(Window window)
Creates a new instance whose handle is obtained from a WPF window.
Definition: Win32Window.cs:103