在前一個(gè)Post當中,指出了在WPF的WindowInteropHelper類(lèi)中的一個(gè)BUG:通過(guò)WindowInteropHelper的Owner屬性不能實(shí)現把WPF窗口的Owner屬性設置為一個(gè)非WPF窗口的句柄。
在我的Post帖出后不到一天,在WPF SDK的Blog上,就針對這個(gè)BUG給出了一個(gè)非常完美的解決方案。既然不同通過(guò)設置WindowStartupLocation.CenterOwner來(lái)改變窗口的位置。那么我們就用WindowStartupLocation.Manual來(lái)手動(dòng)計算設置窗口的位置。大致的代碼如下:
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();
這段代碼理論上沒(méi)有什么問(wèn)題呢?但是WPF是支持設備獨立的。因此,在非WPF Owner窗口的某些情況下可能會(huì )因為DPI的而不能正常工作。解決這個(gè)問(wèn)題,可以利用HwndSource類(lèi)進(jìn)行窗口位置的設備獨立計算:
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的使用。這個(gè)類(lèi)需要一個(gè)窗口句柄,因此它的代碼被放在一個(gè)SourceInitialized的事件委派函數中執行。
最后,除了上面這種方法,其實(shí)我們還可以用Win32 API函數來(lái)實(shí)現,在A(yíng)TL的CWindow類(lèi)中,就有這樣的一個(gè)函數,我直接把放在下面,有興趣的朋友參考其中的實(shí)現原理:












































































聯(lián)系客服