首页 技术 正文
技术 2022年11月14日
0 收藏 405 点赞 4,610 浏览 5075 个字

原文:设置WPF窗口相对于非WPF窗口的位置

在前一个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的事件委派函数中执行。

最后,除了上面这种方法,其实我们还可以用Win32 API函数来实现,在ATL的CWindow类中,就有这样的一个函数,我直接把放在下面,有兴趣的朋友参考其中的实现原理:

设置WPF窗口相对于非WPF窗口的位置BOOL CenterWindow(HWND hWndCenter = NULL) throw()
设置WPF窗口相对于非WPF窗口的位置设置WPF窗口相对于非WPF窗口的位置…{
设置WPF窗口相对于非WPF窗口的位置    ATLASSERT(::IsWindow(m_hWnd));
设置WPF窗口相对于非WPF窗口的位置
设置WPF窗口相对于非WPF窗口的位置    // determine owner window to center against
设置WPF窗口相对于非WPF窗口的位置    DWORD dwStyle = GetStyle();
设置WPF窗口相对于非WPF窗口的位置    if(hWndCenter == NULL)
设置WPF窗口相对于非WPF窗口的位置设置WPF窗口相对于非WPF窗口的位置    …{
设置WPF窗口相对于非WPF窗口的位置        if(dwStyle & WS_CHILD)
设置WPF窗口相对于非WPF窗口的位置            hWndCenter = ::GetParent(m_hWnd);
设置WPF窗口相对于非WPF窗口的位置        else
设置WPF窗口相对于非WPF窗口的位置            hWndCenter = ::GetWindow(m_hWnd, GW_OWNER);
设置WPF窗口相对于非WPF窗口的位置    }
设置WPF窗口相对于非WPF窗口的位置
设置WPF窗口相对于非WPF窗口的位置    // get coordinates of the window relative to its parent
设置WPF窗口相对于非WPF窗口的位置    RECT rcDlg;
设置WPF窗口相对于非WPF窗口的位置    ::GetWindowRect(m_hWnd, &rcDlg);
设置WPF窗口相对于非WPF窗口的位置    RECT rcArea;
设置WPF窗口相对于非WPF窗口的位置    RECT rcCenter;
设置WPF窗口相对于非WPF窗口的位置    HWND hWndParent;
设置WPF窗口相对于非WPF窗口的位置    if(!(dwStyle & WS_CHILD))
设置WPF窗口相对于非WPF窗口的位置设置WPF窗口相对于非WPF窗口的位置    …{
设置WPF窗口相对于非WPF窗口的位置        // don’t center against invisible or minimized windows
设置WPF窗口相对于非WPF窗口的位置        if(hWndCenter != NULL)
设置WPF窗口相对于非WPF窗口的位置设置WPF窗口相对于非WPF窗口的位置        …{
设置WPF窗口相对于非WPF窗口的位置            DWORD dwStyleCenter = ::GetWindowLong(hWndCenter, GWL_STYLE);
设置WPF窗口相对于非WPF窗口的位置            if(!(dwStyleCenter & WS_VISIBLE) || (dwStyleCenter & WS_MINIMIZE))
设置WPF窗口相对于非WPF窗口的位置                hWndCenter = NULL;
设置WPF窗口相对于非WPF窗口的位置        }
设置WPF窗口相对于非WPF窗口的位置
设置WPF窗口相对于非WPF窗口的位置        // center within screen coordinates
设置WPF窗口相对于非WPF窗口的位置        ::SystemParametersInfo(SPI_GETWORKAREA, NULL, &rcArea, NULL);
设置WPF窗口相对于非WPF窗口的位置        if(hWndCenter == NULL)
设置WPF窗口相对于非WPF窗口的位置            rcCenter = rcArea;
设置WPF窗口相对于非WPF窗口的位置        else
设置WPF窗口相对于非WPF窗口的位置            ::GetWindowRect(hWndCenter, &rcCenter);
设置WPF窗口相对于非WPF窗口的位置    }
设置WPF窗口相对于非WPF窗口的位置    else
设置WPF窗口相对于非WPF窗口的位置设置WPF窗口相对于非WPF窗口的位置    …{
设置WPF窗口相对于非WPF窗口的位置        // center within parent client coordinates
设置WPF窗口相对于非WPF窗口的位置        hWndParent = ::GetParent(m_hWnd);
设置WPF窗口相对于非WPF窗口的位置        ATLASSERT(::IsWindow(hWndParent));
设置WPF窗口相对于非WPF窗口的位置
设置WPF窗口相对于非WPF窗口的位置        ::GetClientRect(hWndParent, &rcArea);
设置WPF窗口相对于非WPF窗口的位置        ATLASSERT(::IsWindow(hWndCenter));
设置WPF窗口相对于非WPF窗口的位置        ::GetClientRect(hWndCenter, &rcCenter);
设置WPF窗口相对于非WPF窗口的位置        ::MapWindowPoints(hWndCenter, hWndParent, (POINT*)&rcCenter, 2);
设置WPF窗口相对于非WPF窗口的位置    }
设置WPF窗口相对于非WPF窗口的位置
设置WPF窗口相对于非WPF窗口的位置    int DlgWidth = rcDlg.right – rcDlg.left;
设置WPF窗口相对于非WPF窗口的位置    int DlgHeight = rcDlg.bottom – rcDlg.top;
设置WPF窗口相对于非WPF窗口的位置
设置WPF窗口相对于非WPF窗口的位置    // find dialog’s upper left based on rcCenter
设置WPF窗口相对于非WPF窗口的位置    int xLeft = (rcCenter.left + rcCenter.right) / 2 – DlgWidth / 2;
设置WPF窗口相对于非WPF窗口的位置    int yTop = (rcCenter.top + rcCenter.bottom) / 2 – DlgHeight / 2;
设置WPF窗口相对于非WPF窗口的位置
设置WPF窗口相对于非WPF窗口的位置    // if the dialog is outside the screen, move it inside
设置WPF窗口相对于非WPF窗口的位置    if(xLeft < rcArea.left)
设置WPF窗口相对于非WPF窗口的位置        xLeft = rcArea.left;
设置WPF窗口相对于非WPF窗口的位置    else if(xLeft + DlgWidth > rcArea.right)
设置WPF窗口相对于非WPF窗口的位置        xLeft = rcArea.right – DlgWidth;
设置WPF窗口相对于非WPF窗口的位置
设置WPF窗口相对于非WPF窗口的位置    if(yTop < rcArea.top)
设置WPF窗口相对于非WPF窗口的位置        yTop = rcArea.top;
设置WPF窗口相对于非WPF窗口的位置    else if(yTop + DlgHeight > rcArea.bottom)
设置WPF窗口相对于非WPF窗口的位置        yTop = rcArea.bottom – DlgHeight;
设置WPF窗口相对于非WPF窗口的位置
设置WPF窗口相对于非WPF窗口的位置    // map screen coordinates to child coordinates
设置WPF窗口相对于非WPF窗口的位置    return ::SetWindowPos(m_hWnd, NULL, xLeft, yTop, -1, -1,
设置WPF窗口相对于非WPF窗口的位置        SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294