Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

wpf

.net-4.0

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.

This is the code I'm using:

<Style TargetType="{x:Type local:MainWindow}">
        <Setter Property="shell:WindowChrome.WindowChrome">
            <Setter.Value>
                <shell:WindowChrome
                    ResizeBorderThickness="6"
                    CaptionHeight="10"
                    CornerRadius="0"
                    GlassFrameThickness="1"/>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:MainWindow}">
                    <Grid>
                        <Border BorderThickness="1" BorderBrush="#389FD1" Background="#389FD1">
                            <ContentPresenter Margin="0,22,0,0" Content="{TemplateBinding Content}"/>
                        </Border>
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" >
                            <TextBlock Text="{Binding NombreUsuario}" Foreground="White" Margin="5,5,20,5" Opacity=".8" />
                            <Button Style="{StaticResource ImageButton}" Height="20" Width="20" Margin="0" Click="WindowMinimize" shell:WindowChrome.IsHitTestVisibleInChrome="True">
                                <Image Height="10" Width="10" Source="/Resources/Images/minimize.png" />
                            </Button>
                            <Button Style="{StaticResource ImageButton}" Height="20" Width="20" Margin="0" Click="WindowMaximizeRestore" shell:WindowChrome.IsHitTestVisibleInChrome="True" >
                                <Image Height="10" Width="10" Source="/Resources/Images/maximize.png" />
                            </Button>
                            <Button Style="{StaticResource ImageButton}" Height="20" Width="20" Margin="0" Click="WindowClose" shell:WindowChrome.IsHitTestVisibleInChrome="True">
                                <Image Height="10" Width="10" Source="/Resources/Images/close.png" />
                            </Button>
                        </StackPanel>

                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
like image 568
Eduardo Molteni Avatar asked Jun 03 '10 15:06

Eduardo Molteni


4 Answers

Windows crops the edges of the window when it's maximized to obscure what would normally be the resize edges. You can get around this by putting a proxy border between the window and your content and then inflate the thickness when it's maximized.

I modified the example that came with the lib to do this, the same basic change could be made to your sample:

<ControlTemplate TargetType="{x:Type local:SelectableChromeWindow}">
  <Border BorderBrush="Green">
    <Border.Style>
      <Style TargetType="{x:Type Border}">
        <Setter Property="BorderThickness" Value="0"/>
        <Style.Triggers>
          <DataTrigger Binding="{Binding ElementName=ThisWindow, Path=WindowState}" Value="Maximized">
            <Setter Property="BorderThickness" Value="{Binding Source={x:Static shell:SystemParameters2.Current}, Path=WindowResizeBorderThickness}"/>
          </DataTrigger>
        </Style.Triggers>
      </Style>
    </Border.Style>
    <Grid [...]/>
  </Border>
</ControlTemplate>

I hope that helps.

For .net 4.5 and above, the SystemParameters are a little different, e.g.:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}, Path=WindowState}" Value="Maximized">
    <Setter Property="BorderThickness" Value="{Binding Source={x:Static SystemParameters.WindowResizeBorderThickness}}"/>
</DataTrigger>
like image 134
Joe Castro Avatar answered Nov 05 '22 20:11

Joe Castro


this worked for me:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:shell="clr-namespace:Microsoft.Windows.Shell;assembly=Microsoft.Windows.Shell"
                xmlns:Views="clr-namespace:BorderLessWpf.Views">

<Style TargetType="{x:Type Views:ShellView}" >
    <Setter Property="shell:WindowChrome.WindowChrome">
        <Setter.Value>
            <shell:WindowChrome
                ResizeBorderThickness="6"
                CaptionHeight="10"
                CornerRadius="0"
                GlassFrameThickness="1"/>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="WindowState" Value="Maximized">
            <Setter Property="BorderThickness" Value="6" />
        </Trigger>
    </Style.Triggers>
</Style>

like image 22
Bas Avatar answered Nov 05 '22 18:11

Bas


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
    }
}
like image 6
astef Avatar answered Nov 05 '22 18:11

astef


In my project, I have CaptionHeight set to 0 and ResizeMode set to CanResizeWithGrip. This is the code I came up with for the proper thickness.

            Thickness maximizeFix = new Thickness(SystemParameters.WindowNonClientFrameThickness.Left +
                SystemParameters.WindowResizeBorderThickness.Left,
                SystemParameters.WindowNonClientFrameThickness.Top +
                SystemParameters.WindowResizeBorderThickness.Top
                - SystemParameters.CaptionHeight,
                SystemParameters.WindowNonClientFrameThickness.Right +
                SystemParameters.WindowResizeBorderThickness.Right,
                SystemParameters.WindowNonClientFrameThickness.Bottom +
                SystemParameters.WindowResizeBorderThickness.Bottom);

I'm just going to use a border like Joe Castro did and then bind the thickness to a property that I update when the window state changes.

Code seems janky but I haven't found another solution yet.

like image 3
Shaun Lammers Avatar answered Nov 05 '22 20:11

Shaun Lammers