如何使用常规标题按钮在非客户区正确绘制内容 [英] How to Draw Content in Non-Client Area Properly with Regular Caption Buttons

查看:24
本文介绍了如何使用常规标题按钮在非客户区正确绘制内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用默认的标题按钮和标题栏制作一个窗口,并且仍然能够在窗口镶边中放置任意内容;但是,我尝试过的所有解决方案都在标题按钮的右侧留下了大约 5 个单位的边距.这是一个基本示例:

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>

以上代码产生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!

如果有更稳定的 DWM 方法来实现它,不包括使用 WindowChrome 元素,如果有人能给我一个例子,我将不胜感激.

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.

推荐答案

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 开发该类的修改版本,但在那之前,这是有效的.我希望这对某人有所帮助.

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.

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

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

这篇关于如何使用常规标题按钮在非客户区正确绘制内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆