设置WPF窗口相对于非WPF窗口的位置

原文:设置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类中,就有这样的一个函数,我直接把放在下面,有兴趣的朋友参考其中的实现原理:

BOOL CenterWindow(HWND hWndCenter = NULL) throw()
...{
    ATLASSERT(::IsWindow(m_hWnd));

    // determine owner window to center against
    DWORD dwStyle = GetStyle();
    if(hWndCenter == NULL)
    ...{
        if(dwStyle & WS_CHILD)
            hWndCenter = ::GetParent(m_hWnd);
        else
            hWndCenter = ::GetWindow(m_hWnd, GW_OWNER);
    }

    // get coordinates of the window relative to its parent
    RECT rcDlg;
    ::GetWindowRect(m_hWnd, &rcDlg);
    RECT rcArea;
    RECT rcCenter;
    HWND hWndParent;
    if(!(dwStyle & WS_CHILD))
    ...{
        // don‘t center against invisible or minimized windows
        if(hWndCenter != NULL)
        ...{
            DWORD dwStyleCenter = ::GetWindowLong(hWndCenter, GWL_STYLE);
            if(!(dwStyleCenter & WS_VISIBLE) || (dwStyleCenter & WS_MINIMIZE))
                hWndCenter = NULL;
        }

        // center within screen coordinates
        ::SystemParametersInfo(SPI_GETWORKAREA, NULL, &rcArea, NULL);
        if(hWndCenter == NULL)
            rcCenter = rcArea;
        else
            ::GetWindowRect(hWndCenter, &rcCenter);
    }
    else
    ...{
        // center within parent client coordinates
        hWndParent = ::GetParent(m_hWnd);
        ATLASSERT(::IsWindow(hWndParent));

        ::GetClientRect(hWndParent, &rcArea);
        ATLASSERT(::IsWindow(hWndCenter));
        ::GetClientRect(hWndCenter, &rcCenter);
        ::MapWindowPoints(hWndCenter, hWndParent, (POINT*)&rcCenter, 2);
    }

    int DlgWidth = rcDlg.right - rcDlg.left;
    int DlgHeight = rcDlg.bottom - rcDlg.top;

    // find dialog‘s upper left based on rcCenter
    int xLeft = (rcCenter.left + rcCenter.right) / 2 - DlgWidth / 2;
    int yTop = (rcCenter.top + rcCenter.bottom) / 2 - DlgHeight / 2;

    // if the dialog is outside the screen, move it inside
    if(xLeft < rcArea.left)
        xLeft = rcArea.left;
    else if(xLeft + DlgWidth > rcArea.right)
        xLeft = rcArea.right - DlgWidth;

    if(yTop < rcArea.top)
        yTop = rcArea.top;
    else if(yTop + DlgHeight > rcArea.bottom)
        yTop = rcArea.bottom - DlgHeight;

    // map screen coordinates to child coordinates
    return ::SetWindowPos(m_hWnd, NULL, xLeft, yTop, -1, -1,
        SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}

原文地址:https://www.cnblogs.com/lonelyxmas/p/10799241.html

时间: 2024-08-04 17:33:02

设置WPF窗口相对于非WPF窗口的位置的相关文章

Android窗口管理服务WindowManagerService对窗口的组织方式分析

文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8498908 我们知道,在Android系统中,Activity是以堆栈的形式组织在ActivityManagerService服务中的.与 Activity类似,Android系统中的窗口也是以堆栈的形式组织在WindowManagerService服务中的,其中,Z轴位置较低的窗口 位于Z轴位置较高的窗口的下面.在本文中,我们就详细分析Win

[WPF疑难] 模式窗口被隐藏后重新显示时变成了非模式窗口

原文:[WPF疑难] 模式窗口被隐藏后重新显示时变成了非模式窗口 [WPF疑难] 模式窗口被隐藏后重新显示时变成了非模式窗口 周银辉 现象: 大家可以试试下面这个很有趣但会带来Defect的现象:当我们将子窗口按照ShowDialog()的方式显示出来的时候,很明显该窗口是模式化的(即主窗口等待该窗口的返回,主窗口将不相应用户输入),但如果此时将子窗口的Visibility设置成Visibility.Hidden来隐藏窗口,然后再将Visibility设置成Visibility.Visible来

模式窗口和非模式窗口

JS弹出模式窗口 和非模式窗口JS弹出模式窗口 一般来说,显示在最前面的窗口都是因为被激活获得焦点,要是窗口永远显示在最前面, 可以人为的设置窗口在blur时立刻focus,或者用模式.无模式对话框实现. 具体步骤  方法一:弹出窗口<body>里加上代码:onblur="self.focus()":      <body onblur="self.focus()">  方法二:用showModelDialog方法简历模式对话框,他的参数说明

模态窗口和非模态窗口

对话框一般分为两种类型:模态类型(modal)与非模态类型(modeless).所谓模态对话框,就是指除非采取有效的关闭手段,用户的鼠标焦点或者输入光标将一直停留在其上的对话框.非模态对话框则不会强制此种特性,用户可以在当前对话框以及其他窗口间进行切换.本文介绍如何使用JavaScript语言来创建这两种类型的对话框.控制其大小和位置.改变其外观以及在对话框间的数据传递.(引用) 一.创建模态和非模态对话框 创建模态对话框:(会缓存最近一次页面的值,通过一些设置可绕过系统的判断) vReturn

统计图钻取的明细报表在非模态窗口中显示

润乾报表的统计图可以在图例 / 图形上设置超链接,从而实现钻取到明细报表的操作,钻取得到的报表可以在新窗口或者当前窗口中打开.关于如何在统计图的图形 / 图例上设置超链接,在用户手册中有常用示例的介绍.今天小编要给大家介绍的是如何在非模态窗口中打开图形超链接钻取得到的报表.首先创建一张带有统计图的报表,报表文件名为 planWorkView2.rpx.增加数据集 ds1, 下面是 ds1 的数据: 在 A2 单元格中增加一个柱形图,统计图的分类和系列值数据分别来源于数据集 ds1 的 ORG_N

模态对话框与非模态窗口

模态对话框就是在没有关闭它之前,不能再与同一个应用程序的其他窗口进行交互,比如新建项目时弹出的对话框. #include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); QDialog dialog; dialog.setWindowTitle("模态

VC项目开发之单文档多视图实现-非分割窗口[转]

k_eckel:http://www.mscenter.edu.cn/blog/k_eckel 多视图是VC开发中经常要用到的技术之一,一般地实现单文档多视图有两种方式 1)通过视图分割的技术(使用CSplitterWnd实现),将窗口分割为多个部分,每个部分显示各自显示不同的视图,这种技术实现起来比较简单,并且相关的资料也很多. 2)通过一个文档关联多个视图,窗口显示整个视图. 第二种实现较第一种复杂,这里给出详细的实现方法. Step 1:使用VC 6.0新建一个Project,命名为:Mu

MFC 模态、非模态窗口引发的系列事件及函数

标题一:创建模态窗口 CAdditionDlg dlg; // 定义对话框类CAdditionDlg的对象dlg INT_PTR nResponse = dlg.DoModal();//获取子窗口的结束方式 switch(nResponse) { case IDOK: case IDCANCEL: case IDDESTROY: } 标题二:创建非模态子窗口(完美解决非模态窗口单例问题) 步骤一:创建子窗口指针类型的公有成员 public: CMyTip* m_mytip; 步骤二:父类通过按钮

Ruby操作VBA的注意事项和技巧:乱码、获取VBA活动和非活动窗口的名称与路径、文件路径的智能拼接与截取(写入日期)、宏里调用和控制窗体以及窗体上的控件、不同workbook之间的宏互相调用、

1.VBA编辑器复制粘贴出来的代码乱码     解决方法:切换到中文输入模式再复制出来就行了 2.获取VBA活动和非活动窗口的名称与路径 1 1 Dim wbpath, filename As String 2 2 wbpath = ThisWorkbook.Path ’这个获取的是宏所在的workbook的路径 3 3 'filename = ThisWorkbook.Name '这个是宏所在的workbook的名字,不带路径 4 4 filename = ActiveWindow.Capti