如何使用常规字幕按钮正确绘制非客户区域中的内容

时间:2017-03-23 06:27:38

标签: c# wpf xaml window window-chrome

我正在尝试使用默认的标题按钮和标题栏创建一个窗口,并且仍然可以在窗口chrome中放置任意内容;但是,我尝试的所有解决方案都在标题按钮的右侧留下了大约5个单位的边距。这是一个基本的例子:

<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>

以上代码生成this

要清楚,我想删除窗口镶边中标题按钮右侧的白色边框,以便窗口与其他窗口完全相同,唯一的区别是我可以将控件放在非 - 客户区(窗口铬)。 感谢所有帮助!

如果有一个更稳定的方法来使用不包含使用WindowChrome元素的DWM,我会很感激,如果有人能给我一个例子。

1 个答案:

答案 0 :(得分: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>

这需要很长时间才能找到。最初我认为WindowChrome类完全被破坏了,并且它在一定程度上,但我还是设法使用它。我提出的其他解决方案要复杂得多,最终,尝试使用WindowChrome似乎要容易得多。我将专门为Windows 10修改该类的修改版本,但在此之前,这是有效的。我希望这有助于某人。

要使用此代码,只需将其放在XAML中窗口内容区域的顶部即可。