惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Palo Alto Networks Blog
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
有赞技术团队
有赞技术团队
The GitHub Blog
The GitHub Blog
C
Cisco Blogs
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
Simon Willison's Weblog
Simon Willison's Weblog
T
Tenable Blog
Know Your Adversary
Know Your Adversary
Spread Privacy
Spread Privacy
WordPress大学
WordPress大学
月光博客
月光博客
Latest news
Latest news
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
I
InfoQ
D
Darknet – Hacking Tools, Hacker News & Cyber Security
W
WeLiveSecurity
Hacker News - Newest:
Hacker News - Newest: "LLM"
酷 壳 – CoolShell
酷 壳 – CoolShell
U
Unit 42
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
Google DeepMind News
Google DeepMind News
Apple Machine Learning Research
Apple Machine Learning Research
Attack and Defense Labs
Attack and Defense Labs
罗磊的独立博客
T
The Exploit Database - CXSecurity.com
I
Intezer
GbyAI
GbyAI
Jina AI
Jina AI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Google Online Security Blog
Google Online Security Blog
Engineering at Meta
Engineering at Meta
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
小众软件
小众软件
云风的 BLOG
云风的 BLOG
爱范儿
爱范儿
Project Zero
Project Zero

博客园 - 赖仪灵

分享几个简单的WPF控件(代码) 捕捉WPF应用程序中XAML代码解析异常 上海.NET俱乐部--微软社区巡展VSTS上海专题活动 WPF关于WindowInteropHelper的一个BUG WPF全景体验 最优化WPF 3D性能(基于“Tier-2”硬件) Windows Vista桌面窗口管理器(3) 闲话WPF之二六(WPF性能优化点) 闲话WPF之二五(WPF中的ControlTemplate [3]) 闲话WPF之二四(WPF中的ControlTemplate [2]) Windows Vista桌面窗口管理器(2) 闲话WPF之二三(WPF中的ControlTemplate [1]) 闲话WPF之二二(WPF中的Style) Windows Vista桌面窗口管理器(1) 闲话WPF之二一(WPF中的数据处理 [3]) 闲话WPF之二十(WPF中的传递事件 [2] ) 闲话WPF之十九(WPF中的传递事件 [1] ) 闲话WPF之十八(WPF中的资源 [4] ) 闲话WPF之十七(WPF中的资源 [3])
设置WPF窗口相对于非WPF窗口的位置
赖仪灵 · 2007-05-15 · via 博客园 - 赖仪灵

在前一个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);
}