C# 如何绘制Windows经典样式的窗口元素

C# 如何绘制Windows经典样式的窗口元素,c#,windows,winforms,paint,window-decoration,C#,Windows,Winforms,Paint,Window Decoration,我们在程序中创建了一些自定义“窗口”,当启用了VisualStyles时,我们能够找到窗口的每个元素及其大小,并自己绘制它们,包括使用适当的渲染器最小化和关闭按钮 当VisualStyles被禁用并且当前正在绘制我们自己的窗口时,我们也会做同样的事情,但它们非常难看。是否可以在WinForms C中绘制Windows经典样式的窗口?我找到了ClassicBorderDecorator,但它是用于WPF的 或者,如果做不到这一点,我们如何获得窗口装饰的像素大小,我们可以通过以下方式: // Get

我们在程序中创建了一些自定义“窗口”,当启用了
VisualStyles
时,我们能够找到窗口的每个元素及其大小,并自己绘制它们,包括使用适当的渲染器最小化和关闭按钮

VisualStyles
被禁用并且当前正在绘制我们自己的窗口时,我们也会做同样的事情,但它们非常难看。是否可以在WinForms C中绘制Windows经典样式的窗口?我找到了
ClassicBorderDecorator
,但它是用于WPF的

或者,如果做不到这一点,我们如何获得窗口装饰的像素大小,我们可以通过以下方式:

// Get the height of the window caption.
if (SetRenderer(windowElements["windowCaption"]))
{
  captionHeight = renderer.GetPartSize(graphics.Graphics, ThemeSizeType.True).Height;
}

// Get the thickness of the left, bottom, 
// and right window frame.
if (SetRenderer(windowElements["windowLeft"]))
{
  frameThickness = renderer.GetPartSize(graphics.Graphics, ThemeSizeType.True).Width;
}

Windows没有为经典风格提供渲染器,您必须自己制作。使用SystemInformation类获取度量值,Color.FromKnownColor()获取颜色

唯一棘手的部分是让框架按钮看起来很好。最好的办法是使用字体中的字形,而不是自己绘制。Webdings字体非常适合这种情况

我无法验证匹配的程度,我的机器将启动Windows8,不再支持经典风格。否则,强烈提示您可能不应该在这方面投入太多时间:)一些示例代码:

protected override void OnPaintBackground(PaintEventArgs e) {
    base.OnPaintBackground(e);
    var captionHeight = SystemInformation.CaptionHeight;
    int border = SystemInformation.Border3DSize.Width;
    var color1 = Color.FromKnownColor(activated ? KnownColor.ActiveCaption : KnownColor.InactiveCaption);
    var color2 = Color.FromKnownColor(activated ? KnownColor.GradientActiveCaption : KnownColor.GradientInactiveCaption);
    var captionrc = new Rectangle(0, 0, this.ClientSize.Width, captionHeight);
    using (var brush = new LinearGradientBrush(captionrc, color1, color2, 0, false)) {
        e.Graphics.FillRectangle(brush, captionrc);
    }
    int textx = border;
    if (this.Icon != null) {
        int height = SystemInformation.SmallIconSize.Height;
        var iconrc = new Rectangle(border, (captionHeight - height)/2, height, height);
        textx += height + border;
        e.Graphics.DrawIcon(this.Icon, iconrc);
    }
    var color = Color.FromKnownColor(activated ? KnownColor.ActiveCaptionText : KnownColor.InactiveCaptionText);
    using (var font = new Font(this.Font.FontFamily, SystemInformation.CaptionHeight - 4 * border, GraphicsUnit.Pixel)) {
        TextRenderer.DrawText(e.Graphics, this.Text, font, new Point(textx, border), color);
    }
    using (var font = new Font(new FontFamily("Webdings"), captionHeight - 4 * border, GraphicsUnit.Pixel)) {
        var glyphs = this.WindowState == FormWindowState.Maximized ? "\u0030\u0031\u0072" : "\u0030\u0031\u0072";
        var width = TextRenderer.MeasureText(glyphs, font).Width;
        TextRenderer.DrawText(e.Graphics, glyphs, font, 
            new Point(this.ClientSize.Width - width, border),
            Color.FromKnownColor(KnownColor.WindowFrame));
    }
}
在我的机器上看起来像这样,并不完全难看:)