How to Draw Content in Non-Client Area Properly with Regular Caption Buttons

那年仲夏 提交于 2020-01-15 09:51:47

问题


I am trying to make a window with the default caption buttons and title bar, and still be able to place arbitrary content within the window chrome; however, all the solutions I have tried leave an approximately 5 unit margin to the right of the caption buttons. Here is a basic example:

<Window x:Class="SemiCustomWindowChrome.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:SemiCustomWindowChrome"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" Name="Window">
<WindowChrome.WindowChrome>
    <WindowChrome UseAeroCaptionButtons="True" ResizeBorderThickness="5" GlassFrameThickness="1,28,1,1"/>
</WindowChrome.WindowChrome>
<Window.Template>
    <ControlTemplate>
        <AdornerDecorator>
            <ContentPresenter Content="{Binding ElementName=Window, Path=Content}"/>
        </AdornerDecorator>
    </ControlTemplate>
</Window.Template>
<Grid>
    <StackPanel Orientation="Horizontal" VerticalAlignment="Top">
        <Button Content="Click me daddy!" WindowChrome.IsHitTestVisibleInChrome="True" Height="28"/>
        <TextBlock Text="Custom Window Test" Margin="6.5"/>
    </StackPanel>
    <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" FontFamily="MS Calibri" FontWeight="ExtraLight" Text="Hello There"/>
</Grid>

The above code produces this.

To be clear, I would like to remove the white padding to the right of the caption buttons in the window chrome so that the window is exactly the same as any other, with the only difference that I can put controls in the non-client area (the window chrome). All help is appreciated!

If there is a more stable way to do it with DWM that does not include the use of a WindowChrome element I would appreciate if someone could give me an example.


回答1:


WindowChrome.WindowChrome>
    <WindowChrome GlassFrameThickness="0.5,28,0,0.5" NonClientFrameEdges="Right"/>
</WindowChrome.WindowChrome>
<Window.Template>
    <ControlTemplate>
        <ContentPresenter Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, Path=Content}"/>
    </ControlTemplate>
</Window.Template>

This took a very long time to find. Originally I thought that the WindowChrome class was just completely broken, and it is to an extent, but I managed to use it anyways. The other solutions I came up with were a lot more complicated, and eventually, it just seemed a lot easier to just try and mess around with WindowChrome. I will work on a modified version of the class specifically for Windows 10, but until then, this works. I hope this helps somebody.

To use this code, just put it in at the top of the content area in the XAML for a window.



来源:https://stackoverflow.com/questions/42968743/how-to-draw-content-in-non-client-area-properly-with-regular-caption-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!