扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
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的事件委派函数中执行。
最后,除了上面这种方法,其实我们还可以用Win32 API函数来实现,在ATL的CWindow类中,就有这样的一个函数,我直接把放在下面,有兴趣的朋友参考其中的实现原理:
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者