Window out of the screen when maximized using WPF shell integration library

前端 未结 7 472
无人及你
无人及你 2020-12-01 01:30

I\'m using the WPF Shell Integration Library to create a custom chrome of my wpf app. All is good, but when maximizing the app, 6 or 7 pixels are out of the screen.

7条回答
  •  不知归路
    2020-12-01 02:03

    Why is this happening?

    Raymond Chen has explained this in his note Why are the dimensions of a maximized window larger than the monitor?:

    The extra eight pixels that hang off the screen are the window borders. When you maximize a window, the window manager arranges things so that the client area of the window fills the width of your work area, and it plus the caption bar fills the height of the work area. After all, you want to see as much of your document as possible; there’s no need to show you the window borders that aren’t doing you any good. (It leaves the caption bar on the screen for obvious reasons.)

    How to handle it?

    @Joe Castro has proposed the right approach. You basically have to add a border (or specify a margin) around your window each time it maximizes. And SystemParameters.WindowResizeBorderThickness is also the right property, but ... it has a bug.

    For me, it gives 4px border instead of correct 8px for 100% display scale (can be changed in Display Settings) and approximately 3px instead of correct 7px for 175%.

    Why I'm so sure it's a bug and not just the wrong property?

    Internally SystemParameters.WindowResizeBorderThickness uses GetSystemMetrics call to obtain device pixels of the border. It queries for CXFRAME (32) and CYFRAME(33) indexes, and then converts it into logical pixels.

    Now check this old bug report:

    Take the following lines of code:

        int cx = ::GetSystemMetrics(SM_CXSIZEFRAME);
        int cy = ::GetSystemMetrics(SM_CYSIZEFRAME);
        int cp = ::GetSystemMetrics(SM_CYCAPTION);
    

    When compiled with Visual Studio 2010, on my Windows7 machine this yields: cx == 9, cy == 9 and cp == 27.

    If I compile the very same code with Vision Studio 2012 RC on the same machine, this yields: cx == 5, cy == 5 and cp == 27.

    And the answer:

    Posted by Microsoft on 7/12/2012 at 11:03 AM Thank you for your bug submission. The issue you reported appears to be a Windows issue, and we have forwarded the bug to them.

    But there's some good news from this question: GetSystemMetrics() returns different results for .NET 4.5 & .NET 4.0

    For .NET 4.5 and later you can just add SM_CXPADDEDBORDER (92) value to both CXFRAME (32) and CYFRAME(33) to get the right values.

    What's the final solution?

    Use @Joe Castro approach, but with the fixed property:

    public static class SystemParametersFix
    {
        public static Thickness WindowResizeBorderThickness
        {
            get
            {
                float dpix = GetDpi(GetDeviceCapsIndex.LOGPIXELSX);
                float dpiy = GetDpi(GetDeviceCapsIndex.LOGPIXELSY);
    
                int dx = GetSystemMetrics(GetSystemMetricsIndex.CXFRAME);
                int dy = GetSystemMetrics(GetSystemMetricsIndex.CYFRAME);
    
                // this adjustment is needed only since .NET 4.5 
                int d = GetSystemMetrics(GetSystemMetricsIndex.SM_CXPADDEDBORDER);
                dx += d;
                dy += d;
    
                var leftBorder = dx / dpix;
                var topBorder = dy / dpiy;
    
                return new Thickness(leftBorder, topBorder, leftBorder, topBorder);
            }
        }
    
        [DllImport("user32.dll")]
        private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
    
        [DllImport("user32.dll")]
        private static extern IntPtr GetDC(IntPtr hwnd);
    
        [DllImport("gdi32.dll")]
        private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
    
        private static float GetDpi(GetDeviceCapsIndex index)
        {
            IntPtr desktopWnd = IntPtr.Zero;
            IntPtr dc = GetDC(desktopWnd);
            float dpi;
            try
            {
                dpi = GetDeviceCaps(dc, (int)index);
            }
            finally
            {
                ReleaseDC(desktopWnd, dc);
            }
            return dpi / 96f;
        }
    
        private enum GetDeviceCapsIndex
        {
            LOGPIXELSX = 88,
            LOGPIXELSY = 90
        }
    
        [DllImport("user32.dll")]
        private static extern int GetSystemMetrics(GetSystemMetricsIndex nIndex);
    
        private enum GetSystemMetricsIndex
        {
            CXFRAME = 32,
            CYFRAME = 33,
            SM_CXPADDEDBORDER = 92
        }
    }
    

提交回复
热议问题