在前一个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);
}
本文转自赖仪灵博客园博客,原文链接:http://www.cnblogs.com/YilingLai/archive/2007/05/15/746706.html,如需转载请自行联系原作者。

设置WPF窗口相对于非WPF窗口的位置相关推荐

  1. QT设置非主窗口的背景图片和主窗口的背景图片(详细版)

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.主窗口设置背景图片 二.非主窗口设置背景图片 前言 网上关于设置窗口背景图片大多数都是对于主窗口MainWindo ...

  2. MFC模态窗口与非模态窗口

    MFC模态窗口与非模态窗口 开发工具与关键技术:C++.VisualStudio 作者:何任贤 撰写时间:2019年07月25日 模态窗口的意思是指主窗口在打开模态窗口后,没法再操作主窗口,这就是模态 ...

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

    原文:[WPF疑难] 模式窗口被隐藏后重新显示时变成了非模式窗口 [WPF疑难] 模式窗口被隐藏后重新显示时变成了非模式窗口 周银辉 现象: 大家可以试试下面这个很有趣但会带来Defect的现象:当我 ...

  4. WPF中用于嵌入其他进程窗口的自定义控件(AppContainer)

    原文:WPF中用于嵌入其他进程窗口的自定义控件(AppContainer) 版权声明:本文为博主原创文章,转载请注明作者和出处 https://blog.csdn.net/ZZZWWWPPP11199 ...

  5. WPF疑难杂症之二(全屏幕窗口)

    近日的学习中遇到一个非常奇怪的问题:用XAML文件创建了一个全屏幕窗口,然后,在窗口中建立了一个非常简单的动画.一切都在我的掌控之中,实现非常的顺利. WPF中用XAML创建全屏幕窗口非常简单,只需要 ...

  6. wpf异形按钮_WPF Window异形窗口演示

    我们先通过简单的效果展示,切换展示不同图片: 我们先定义图片资源文件,我们可以在window资源中定义,下面的在app.xaml文件来定义: xmlns="http://schemas.mi ...

  7. 【msdn wpf forum翻译】获取当前窗口焦点所在的元素

    原文:[msdn wpf forum翻译]获取当前窗口焦点所在的元素 原文地址: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6b ...

  8. win11非活动窗口如何设置 Windows11设置非活动窗口的步骤方法

    在win11系统当中是能够对非活动窗口进行一些操作的,我们需要进行手动设置才行,那么Win11要怎样才能设置非活动窗口呢?下面就和小编一起来看看要如何去操作吧.更多Windows11安装教程可以参考小 ...

  9. WPF 闹钟定时弹出提醒窗口

    WPF 闹钟定时弹出提醒窗口 选择定时模式,选择提醒模式,输入你想在提醒窗口上显示的内容.点击START按钮即可开始定时.当到达指定定时时间,就会弹出提醒窗口. 背景 现在人们的工作压力越来越大,很多 ...

最新文章

  1. Warning: post-commit hook failed (exit code 255) with no output.
  2. Ubuntu 12.04搭建MTK 6577 安卓开发环境
  3. Py之keras-resnet:keras-resnet的简介、安装、使用方法之详细攻略
  4. uva 10245 The Closest Pair Problem_枚举
  5. C语言 extern “C” - C语言零基础入门教程
  6. xp sp3 安装不了ie8
  7. docker学习5--docker数据卷(volume)
  8. RS 纠删码为什么可以提高分布式存储可靠性?| 原力计划
  9. App开发流程之图像处理工具类
  10. C语言 实现面向对象
  11. android 直播 app下载地址,朵朵直播app下载地址
  12. matlab排序算法,相同位置返回元素排名
  13. 从苹果供应商看2018年全球电子产业链的中国势力变化
  14. webview的硬件加速
  15. WinDbg手动修复堆栈
  16. 专家修炼-学习的方法
  17. 02.集线器,网桥,交换机
  18. 从企业钉钉的接口获取数据
  19. Auto.js 手机版 图色工具
  20. vue给标签动态绑定title

热门文章

  1. 字符串html在线互转,将string 的字符串转换为HTML的两种方法
  2. 火力发电厂与变电站设计防火标准_火力发电厂与变电站设计防火规范
  3. java中的排序算法——简单选择排序,树形选择排序与堆排序(一)
  4. 内存分析工具MAT的使用
  5. Mysql搭建主从服务器
  6. java类中定义索引器,C#面向对象基础——字段、属性和索引器
  7. python拟牛顿法迭代点绘制_拟牛顿法python
  8. android界面布局题,【填空题】Android 系统中, 用于定义布局显示在界面上的风格。...
  9. oracle nodemanage,Linux 下Weblogic集群搭建-04通过nodemanage进行节点的启动与关闭
  10. Windows7 IIS 500-内部服务器错误的解决方法