在前一個Post當中,指出了在WPF的WindowInteropHelper類中的一個BUG:通過WindowInteropHelper的Owner屬性不能實現把WPF窗口的Owner屬性設置為一個非WPF窗口的句柄。
在我的Post帖出后不到一天,在WPF SDK的Blog上,就針對這個BUG給出了一個非常完美的解決方案。既然不同通過設置WindowStartupLocation.CenterOwner來改變窗口的位置。那么我們就用WindowStartupLocation.Manual來手動計算設置窗口的位置。大致的代碼如下:
using System.Windows;
using System.Windows.Interop; // WindowInteropHelper
...
// Instantiate the owned WPF window
Window cw = new Window();
// Set the owned WPF window’s owner with the non-WPF owner window
IntPtr ownerWindowHandle = ...;
// Set the owned WPF window’s owner with the non-WPF owner window
WindowInteropHelper helper = new WindowInteropHelper(cw);
helper.Owner = ownerWindowHandle;
// Manually calculate Top/Left to appear centered
int nonWPFOwnerLeft = ...; // Get non-WPF owner’s Left
int nonWPFOwnerWidth = ...; // Get non-WPF owner’s Width
int nonWPFOwnerTop = ...; // Get non-WPF owner’s Top
int nonWPFOwnerHeight = ...; // Get non-WPF owner’s Height
cw.WindowStartupLocation = WindowStartupLocation.Manual;
cw.Left = nonWPFOwnerLeft + (nonWPFOwnerWidth - cw.Width) / 2;
cw.Top = nonWPFOwnerTop + (nonWPFOwnerHeight - cw.Height) / 2;
// Show the owned WPF window
cw.Show();
這段代碼理論上沒有什么問題呢?但是WPF是支持設備獨立的。因此,在非WPF Owner窗口的某些情況下可能會因為DPI的而不能正常工作。解決這個問題,可以利用HwndSource類進行窗口位置的設備獨立計算:
using System.Windows; // Window, WindowStartupLocation, Point
using System.Windows.Interop; // WindowInteropHelper, HwndSource
using System.Windows.Media; // Matrix
...
// Instantiate the owned WPF window
CenteredWindow cw = new CenteredWindow();
// Get the handle to the non-WPF owner window
IntPtr ownerWindowHandle = ...; // Get hWnd for non-WPF window
// Set the owned WPF window’s owner with the non-WPF owner window
WindowInteropHelper helper = new WindowInteropHelper(cw);
helper.Owner = ownerWindowHandle;
// Center window
// Note - Need to use HwndSource to get handle to WPF owned window,
// and the handle only exists when SourceInitialized has been
// raised
cw.SourceInitialized += delegate
{
// Get WPF size and location for non-WPF owner window
int nonWPFOwnerLeft = ...; // Get non-WPF owner’s Left
int nonWPFOwnerWidth = ...; // Get non-WPF owner’s Width
int nonWPFOwnerTop = ...; // Get non-WPF owner’s Top
int nonWPFOwnerHeight = ...; // Get non-WPF owner’s Height
// Get transform matrix to transform non-WPF owner window
// size and location units into device-independent WPF
// size and location units
HwndSource source = HwndSource.FromHwnd(helper.Handle);
if (source == null) return;
Matrix matrix = source.CompositionTarget.TransformFromDevice;
Point ownerWPFSize = matrix.Transform(
new Point(nonWPFOwnerWidth, nonWPFOwnerHeight));
Point ownerWPFPosition = matrix.Transform(
new Point(nonWPFOwnerLeft, nonWPFOwnerTop));
// Center WPF window
cw.WindowStartupLocation = WindowStartupLocation.Manual;
cw.Left = ownerWPFPosition.X + (ownerWPFSize.X - cw.Width) / 2;
cw.Top = ownerWPFPosition.Y + (ownerWPFSize.Y - cw.Height) / 2;
};
// Show WPF owned window
cw.Show();
在上面的代碼中需要注意的是HwndSource的使用。這個類需要一個窗口句柄,因此它的代碼被放在一個SourceInitialized的事件委派函數中執(zhí)行。
最后,除了上面這種方法,其實我們還可以用Win32 API函數來實現,在ATL的CWindow類中,就有這樣的一個函數,我直接把放在下面,有興趣的朋友參考其中的實現原理: