欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
設置WPF窗口相對于非WPF窗口的位置

在前一個(gè)Post當中,指出了在WPF的WindowInteropHelper類(lèi)中的一個(gè)BUG:通過(guò)WindowInteropHelper的Owner屬性不能實(shí)現把WPF窗口的Owner屬性設置為一個(gè)非WPF窗口的句柄。

在我的Post帖出后不到一天,在WPF SDK的Blog上,就針對這個(gè)BUG給出了一個(gè)非常完美的解決方案。既然不同通過(guò)設置WindowStartupLocation.CenterOwner來(lái)改變窗口的位置。那么我們就用WindowStartupLocation.Manual來(lái)手動(dòng)計算設置窗口的位置。大致的代碼如下:

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();

這段代碼理論上沒(méi)有什么問(wèn)題呢?但是WPF是支持設備獨立的。因此,在非WPF Owner窗口的某些情況下可能會(huì )因為DPI的而不能正常工作。解決這個(gè)問(wèn)題,可以利用HwndSource類(lèi)進(jìn)行窗口位置的設備獨立計算:

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的使用。這個(gè)類(lèi)需要一個(gè)窗口句柄,因此它的代碼被放在一個(gè)SourceInitialized的事件委派函數中執行。

最后,除了上面這種方法,其實(shí)我們還可以用Win32 API函數來(lái)實(shí)現,在A(yíng)TL的CWindow類(lèi)中,就有這樣的一個(gè)函數,我直接把放在下面,有興趣的朋友參考其中的實(shí)現原理: 

 

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);
}
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
父子窗口分屬不同消息循環(huán)在WinXP和WinCE的差異
MFCwin32窗口
談?wù)劯复翱诤退姓叽翱?- 兔子的技術(shù)博客 - C++博客
2.Windows 界面技術(shù)發(fā)展現狀
在非托管C++中使用GDI+
使用gcc編譯Windows程序并使用資源文件
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久