How to Draw Windows Classic Style Window Elements

We create several custom “windows” in our program, and when VisualStylesenabled, we can find each element of the window and their size and draw them ourselves, including the minimize and close buttons using the corresponding Renderers.

We would like to do the same when VisualStylesdisabled and currently draw our own windows, but they are pretty ugly. Is it possible to draw windows classic windows in winforms c #? I found ClassicBorderDecorator, but for WPF.

Or, otherwise, how can we get the pixel sizes of window decorations, which we do as follows:

// 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;
}
+4
source share
1

Windows , . SystemInformation Color.FromKnownColor() .

- . , , . Webdings .

, , Windows 8 . , , , :) :

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));
    }
}

, , :)

enter image description here

+8

Source: https://habr.com/ru/post/1535811/


All Articles