C# 使用自定义chrome和DWM时重新绘制窗口标题

C# 使用自定义chrome和DWM时重新绘制窗口标题,c#,wpf,pinvoke,dwm,C#,Wpf,Pinvoke,Dwm,我使用的是WPFShell集成库(),但是当我使用带有Aero的定制chrome时,标题栏被删除 我知道我需要使用DrawThemeTextEx函数来重新绘制窗口标题,但是我找不到任何这样做的C#示例。我已经找到了一本指南,详细说明了这幅画的标题 我不太确定(我对pinvoke没有什么经验)如何在C#中做到这一点,以便使用正确的系统字体等。是否有人能够提供一个C#示例,我可以将其集成到WPF Shell集成库中 更新#1:我在一个Windows窗体项目中尝试了这段代码,效果很好。我注意到,如果我

我使用的是WPFShell集成库(),但是当我使用带有Aero的定制chrome时,标题栏被删除

我知道我需要使用DrawThemeTextEx函数来重新绘制窗口标题,但是我找不到任何这样做的C#示例。我已经找到了一本指南,详细说明了这幅画的标题

我不太确定(我对pinvoke没有什么经验)如何在C#中做到这一点,以便使用正确的系统字体等。是否有人能够提供一个C#示例,我可以将其集成到WPF Shell集成库中

更新#1:我在一个Windows窗体项目中尝试了这段代码,效果很好。我注意到,如果我将窗体移出屏幕,Windows窗体将丢失标题文本。所以我相信这个问题可能与此有关。我曾尝试在OnRender事件中绘制标题文本,但这并不能解决问题

我已将以下代码添加到WindowChromeWorker.cs:

    private void _DrawCustomTitle(IntPtr hwnd)
    {
        if (NativeMethods.DwmIsCompositionEnabled())
        {

            Standard.RECT rcClient = new Standard.RECT();
            NativeMethods.GetClientRect(hwnd, ref rcClient);

            Standard.RECT rcPaint = rcClient;
            rcPaint.Top += 8;
            rcPaint.Right -= 125;
            rcPaint.Left += 8;
            rcPaint.Bottom = 50;

            IntPtr destdc = NativeMethods.GetDC(hwnd);
            IntPtr Memdc = NativeMethods.CreateCompatibleDC(destdc); // Set up a memory DC where we'll draw the text.
            IntPtr bitmap;
            IntPtr bitmapOld = IntPtr.Zero;
            IntPtr logFont;

            uint uFormat = NativeMethods.DT_SINGLELINE | NativeMethods.DT_TOP | NativeMethods.DT_LEFT | NativeMethods.DT_WORD_ELLIPSIS;

            BITMAPINFO dib = new BITMAPINFO();
            dib.bmiHeader.biHeight = -(rcClient.Bottom - rcClient.Top); // negative because DrawThemeTextEx() uses a top-down DIB
            dib.bmiHeader.biWidth = rcClient.Right - rcClient.Left;
            dib.bmiHeader.biPlanes = 1;
            dib.bmiHeader.biSize = Marshal.SizeOf(typeof(BITMAPINFOHEADER));
            dib.bmiHeader.biBitCount = 32;
            dib.bmiHeader.biCompression = NativeMethods.BI_RGB;

            if (!(NativeMethods.SaveDC(Memdc) == 0))
            {
                bitmap = NativeMethods.CreateDIBSection(Memdc, ref dib, NativeMethods.DIB_RGB_COLORS, 0, IntPtr.Zero, 0);   // Create a 32-bit bmp for use in offscreen drawing when glass is on
                if (!(bitmap == IntPtr.Zero))
                {
                    bitmapOld = NativeMethods.SelectObject(Memdc, bitmap);

                    System.Drawing.Font font = new System.Drawing.Font("Segoe UI", 9f);
                    IntPtr hFont = font.ToHfont();
                    logFont = NativeMethods.SelectObject(Memdc, hFont);
                    try
                    {

                        System.Windows.Forms.VisualStyles.VisualStyleRenderer renderer = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Window.Caption.Active);

                        NativeMethods.DTTOPTS dttOpts = new NativeMethods.DTTOPTS();
                        dttOpts.dwSize = (int)Marshal.SizeOf(typeof(NativeMethods.DTTOPTS));
                        dttOpts.dwFlags = NativeMethods.DTT_COMPOSITED | NativeMethods.DTT_GLOWSIZE;
                        dttOpts.iGlowSize = 15;

                        string title = "Windows Title";
                        NativeMethods.DrawThemeTextEx(renderer.Handle, Memdc, 0, 0, title, -1, uFormat, ref rcPaint, ref dttOpts);

                        NativeMethods.BitBlt(destdc, rcClient.Left, rcClient.Top, rcClient.Right - rcClient.Left, rcClient.Bottom - rcClient.Top, Memdc, 0, 0, NativeMethods.SRCCOPY);

                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine(e.Message);
                    }

                    // Clean Up
                    NativeMethods.SelectObject(Memdc, bitmapOld);
                    NativeMethods.SelectObject(Memdc, logFont);
                    NativeMethods.DeleteObject(bitmap);
                    NativeMethods.DeleteObject(hFont);

                    NativeMethods.ReleaseDC(Memdc, -1);
                    NativeMethods.DeleteDC(Memdc);
                }

            }
        }

    }
在DWM玻璃被扩展之后,我在下面的函数中调用DrawCustomTitle。你知道为什么这行不通吗

    private void _ExtendGlassFrame()
    {
        Assert.IsNotNull(_window);

        // Expect that this might be called on OSes other than Vista.
        if (!Utility.IsOSVistaOrNewer)
        {
            // Not an error.  Just not on Vista so we're not going to get glass.
            return;
        }

        if (IntPtr.Zero == _hwnd)
        {
            // Can't do anything with this call until the Window has been shown.
            return;
        }

        // Ensure standard HWND background painting when DWM isn't enabled.
        if (!NativeMethods.DwmIsCompositionEnabled())
        {
            _hwndSource.CompositionTarget.BackgroundColor = SystemColors.WindowColor;
        }
        else
        {
            // This makes the glass visible at a Win32 level so long as nothing else is covering it.
            // The Window's Background needs to be changed independent of this.

            // Apply the transparent background to the HWND
            _hwndSource.CompositionTarget.BackgroundColor = Colors.Transparent;

            // Thickness is going to be DIPs, need to convert to system coordinates.
            Point deviceTopLeft = DpiHelper.LogicalPixelsToDevice(new Point(_chromeInfo.GlassFrameThickness.Left, _chromeInfo.GlassFrameThickness.Top));
            Point deviceBottomRight = DpiHelper.LogicalPixelsToDevice(new Point(_chromeInfo.GlassFrameThickness.Right, _chromeInfo.GlassFrameThickness.Bottom));

            var dwmMargin = new MARGINS
            {
                // err on the side of pushing in glass an extra pixel.
                cxLeftWidth = (int)Math.Ceiling(deviceTopLeft.X),
                cxRightWidth = (int)Math.Ceiling(deviceBottomRight.X),
                cyTopHeight = (int)Math.Ceiling(deviceTopLeft.Y),
                cyBottomHeight = (int)Math.Ceiling(deviceBottomRight.Y),
            };

            NativeMethods.DwmExtendFrameIntoClientArea(_hwnd, ref dwmMargin);

            this._DrawCustomTitle(_hwnd);
        }
    }

你不应该经历那么多麻烦。查看下面我的代码示例。带有
Text={TemplateBinding Title}
的标签符合您的要求

<Window x:Class="WpfApplication1.MainWindow"
    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"
    Title="MainWindow" Height="350" Width="525">
<Window.Style>
    <Style TargetType="Window">
        <Setter Property="shell:WindowChrome.WindowChrome">
            <Setter.Value>
                <shell:WindowChrome GlassFrameThickness="4,40,4,4" ResizeBorderThickness="5" CaptionHeight="30"/>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Window">
                    <Grid x:Name="PART_ComponentRoot">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="40"/>
                            <RowDefinition/>
                            <RowDefinition Height="4"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="4"/>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="4"/>
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Column="1" Text="{TemplateBinding Title}"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"/>

                        <ContentPresenter Grid.Row="1" Grid.Column="1" Content="{TemplateBinding Content}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Style>

<Grid Background="White">

</Grid>


它不起作用的原因可能是通常所说的“空域”问题。基本上,您使用的代码是用GDI渲染像素,WPF是用DirectX实现的,这两种渲染技术不知道如何共享像素。也许还有更微妙的事情发生,但乍一看我怀疑就是这样

您希望在WPF中执行此操作的方式类似于Mranz之前建议的方式。有两种简单的方法可以通过WPF获得模糊效果:

  • 使用相同的字体和文本等直接在彼此的顶部创建两个文本块(例如,在占用同一单元格的网格中),但要使底部的文本块具有所需的辉光颜色,然后对其应用模糊效果。需要注意的是,如果没有效果,它们将以相同的方式渲染,否则模糊将被禁用

  • 创建一个椭圆,或一个边缘带有椭圆的矩形,并使用相同的技巧将其放置在带有标题的文本块后面,并在其上放置模糊效果

  • 这也有点棘手,因为Windows在Vista和7之间改变了一些行为(我不知道8最终会是什么样子)。如果我没记错的话,在Vista中,当你最大化窗口时,模糊效果会消失,字体颜色会变成白色。在第七章中,这种情况不再发生了。至少在8(和Office 2010)的开发者预览中,标题文本现在居中。此外,当你自己做这件事时,你需要小心不要遮住标题按钮,因为它可能不会在你期望的位置开始剪切文本

    如果你对这两种方法中的任何一种都有问题,请随时打电话给我。我可能会编写一些示例代码,但手边没有样式。此外,如果您解决了问题,请发布您的解决方案,以便其他人能够受益:)


    希望这有帮助,

    我真的希望标题栏文本与其他窗口中的标题栏一样。为了实现标题栏文本背后的航空玻璃辉光,我需要使用P援乐和DouthMeTeXEX方法。你也可以使用着色器效果,我已经达到了足够的效果,可以使用外发光效果或模糊效果,谢谢。我真的很想知道为什么示例代码和pinvoke不起作用。是否可以指定自定义标题栏背景(例如红色)?我尝试了您的示例,但CloseMinimize按钮不可用。@Lonli Lokli您可以将“PART_ComponentRoot”网格的背景设置为所需的颜色,并将内容演示器恢复为原始颜色。它可能会满足您的需求。