获取用于最小化、最大化和关闭的默认 WIndows 系统图标

Get the Default WIndows System Icons for minimize, maximize and close

我想创建一个无边界的 WinForm,它有一个自定义的 header 和以下的默认系统图标:

有什么方法可以在 C# 或 VB 中实现吗?

据我所知,您可以使用 windows 形式的 "Anchor property" + "Doc Property",方法是放置三个带有图像的按钮。

或者我在这里展示了一个示例,它显示了带有控件的 windows 表单的自定义主题。

此示例在 Vb.NET.Save 中作为 class 在您的项目中,构建您的项目,然后控件将出现在工具箱中 area.Drag 并删除该控件。享受吧!

试试这个 T3 Vb.NET Themes Archives

不确定您的确切目标是什么,但一般来说,对于使用 C# 的 'custom' 设计,我更喜欢 WPF(Windows Presentation Foundation)而不是 Windows Forms...

我想这在 Windows 表单中也是可能的,也许如果您根据需要删除边框并创建 3 个按钮,使用常见的 Windows 符号作为它们的背景?但我不确定它是否有效 ;)

编辑:

使用控件 'paint'-事件,您应该能够达到您的目标:

private void button_Paint(object sender, PaintEventArgs e)
{
    if (VisualStyleRenderer.IsElementDefined(VisualStyleElement.Window.CloseButton.Normal))
    {
        VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.Window.CloseButton.Normal);
        Rectangle rectangle1 = new Rectangle(button.Location.X, button.Location.Y, button.Width, button.Height);
        renderer.DrawBackground(e.Graphics, rectangle1);
    }
}

这是检查您是否能够使用样式,然后将选定的 VisualStyleElement(例如 CloseButton、MinButton 等)绘制到 button/control 的位置。

有关详细信息,请参阅 VisualStylesElement-CloseButton and Control.Paint-Event

对我来说很好,希望这是你一直在寻找的...

每个人都希望在 windows 10 航空风格中有一个完全可定制的按钮。

这里是:

首先,修复错误的基础 class,当表单失去焦点时,聚焦的按钮得到轮廓:

using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsControls {
    /// <summary>
    /// Modified button which has no focus rectangles when the form which contains this button loses fucus while the button was focused.
    /// </summary>
    [ToolboxItem(typeof(NoFocusCueBotton))]
    public class NoFocusCueBotton : Button {
        protected override bool ShowFocusCues => false;

        /// <summary>
        /// Creates a new instance of a <see cref="NoFocusCueBotton"/>
        /// </summary>
        public NoFocusCueBotton() { }

        public override void NotifyDefault(bool value) {
            base.NotifyDefault(false);
        }
    }
}

下一个实际按钮:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsControls {
    /// <summary>
    /// Button which represents the default close, minimize or maximize buttons of the windows 10 aero theme.
    /// </summary>
    [ToolboxItem(true)]
    public class WindowsDefaultTitleBarButton : NoFocusCueBotton {
        /// <summary>
        /// Represents the 3 possible types of the windows border buttons.
        /// </summary>
        public enum Type {
            Close,
            Maximize,
            Minimize
        }

        private Pen activeIconColorPen;
        private Brush activeIconColorBrush;
        private Brush activeColorBrush;

        /// <summary>
        /// The type which defines the buttons behaviour.
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Browsable(true)]
        [DefaultValue(Type.Close)]
        [Category("Appearance")]
        [Description("The type which defines the buttons behaviour.")]
        public Type ButtonType { get; set; }

        /// <summary>
        /// The background color of the button when the mouse is inside the buttons bounds.
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Browsable(true)]
        [DefaultValue(null)]
        [Category("Appearance")]
        [Description("The background color of the button when the mouse is inside the buttons bounds.")]
        public Color HoverColor { get; set; }

        /// <summary>
        /// The background color of the button when the button is clicked.
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Browsable(true)]
        [DefaultValue(null)]
        [Category("Appearance")]
        [Description("The background color of the button when the button is clicked.")]
        public Color ClickColor { get; set; }

        /// <summary>
        /// The default color of the icon.
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Browsable(true)]
        [DefaultValue(null)]
        [Category("Appearance")]
        [Description("The default color of the icon.")]
        public Color IconColor { get; set; }

        /// <summary>
        /// The color of the icon when the mouse is inside the buttons bounds.
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Browsable(true)]
        [DefaultValue(null)]
        [Category("Appearance")]
        [Description("The color of the icon when the mouse is inside the buttons bounds.")]
        public Color HoverIconColor { get; set; }

        /// <summary>
        /// The color of the icon when the button is clicked.
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Browsable(true)]
        [DefaultValue(null)]
        [Category("Appearance")]
        [Description("The color of the icon when the button is clicked.")]
        public Color ClickIconColor { get; set; }

        /// <summary>
        /// Property which returns the active background color of the button depending on if the button is clicked or hovered.
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Browsable(false)]
        public virtual Color ActiveColor {
            get {
                if (this.Clicked)
                    return this.ClickColor;

                if (this.Hovered)
                    return this.HoverColor;

                return BackColor;
            }
        }

        /// <summary>
        /// Property which returns the active color of the buttons icon depending on if the button is clicked or hovered.
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Browsable(false)]
        public virtual Color ActiveIconColor {
            get {
                if (this.Clicked)
                    return this.ClickIconColor;

                if (this.Hovered)
                    return this.HoverIconColor;

                return IconColor;
            }
        }

        /// <summary>
        /// Property which indicates if the mouse is currently inside the bounds of the button.
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Browsable(false)]
        [DefaultValue(false)]
        public bool Hovered { get; set; }

        /// <summary>
        /// Property which indicates if the left mouse button was pressed down inside the buttons bounds. Can be true before the click event is triggered.
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Browsable(false)]
        [DefaultValue(false)]
        public bool Clicked { get; set; }

        public WindowsDefaultTitleBarButton() { }

        protected override void OnMouseEnter(EventArgs e) {
            base.OnMouseEnter(e);
            Hovered = true;
        }

        protected override void OnMouseLeave(EventArgs e) {
            base.OnMouseLeave(e);
            Hovered = false;
        }

        protected override void OnMouseDown(MouseEventArgs mevent) {
            base.OnMouseDown(mevent);
            Clicked = true;
        }

        protected override void OnMouseUp(MouseEventArgs mevent) {
            base.OnMouseUp(mevent);
            Clicked = false;
        }

        protected override void OnClick(EventArgs e) {
            if (ButtonType == Type.Close)
                this.FindForm()?.Close();
            else if (ButtonType == Type.Maximize)
                this.FindForm().WindowState = this.FindForm().WindowState == FormWindowState.Maximized ? FormWindowState.Normal : FormWindowState.Maximized;
            else
                this.FindForm().WindowState = FormWindowState.Minimized;

            base.OnClick(e);
        }

        protected override void OnPaint(PaintEventArgs pevent) {
            System.Diagnostics.Trace.WriteLine(pevent.ClipRectangle.ToString());

            activeColorBrush?.Dispose();
            activeColorBrush = new SolidBrush(ActiveColor);

            pevent.Graphics.FillRectangle(new SolidBrush(ActiveColor), pevent.ClipRectangle);
            pevent.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            activeIconColorBrush?.Dispose();
            activeIconColorPen?.Dispose();

            activeIconColorBrush = new SolidBrush(ActiveIconColor);
            activeIconColorPen = new Pen(activeIconColorBrush, 1.0f);

            if (ButtonType == Type.Close)
                drawCloseIcon(pevent, new Rectangle(0, 0, this.Width, this.Height));
            else if (ButtonType == Type.Maximize)
                drawMaximizeIcon(pevent, new Rectangle(0, 0, this.Width, this.Height));
            else
                drawMinimizeIcon(pevent, new Rectangle(0, 0, this.Width, this.Height));
        }

        protected virtual void drawCloseIcon(PaintEventArgs e, Rectangle drawRect) {
            e.Graphics.DrawLine(
                activeIconColorPen,
                drawRect.X + drawRect.Width / 2 - 5,
                drawRect.Y + drawRect.Height / 2 - 5,
                drawRect.X + drawRect.Width / 2 + 5,
                drawRect.Y + drawRect.Height / 2 + 5);

            e.Graphics.DrawLine(
                activeIconColorPen,
                drawRect.X + drawRect.Width / 2 - 5,
                drawRect.Y + drawRect.Height / 2 + 5,
                drawRect.X + drawRect.Width / 2 + 5,
                drawRect.Y + drawRect.Height / 2 - 5); ;
        }

        protected virtual void drawMaximizeIcon(PaintEventArgs e, Rectangle drawRect) {
            if (this.FindForm().WindowState == FormWindowState.Normal) {
                e.Graphics.DrawRectangle(
                    activeIconColorPen,
                    new Rectangle(
                        drawRect.X + drawRect.Width / 2 - 5,
                        drawRect.Y + drawRect.Height / 2 - 5,
                        10, 10));
            } else if (this.FindForm().WindowState == FormWindowState.Maximized) {
                e.Graphics.DrawRectangle(
                    activeIconColorPen,
                    new Rectangle(
                        drawRect.X + drawRect.Width / 2 - 3,
                        drawRect.Y + drawRect.Height / 2 - 5,
                        8, 8));

                Rectangle rect = new Rectangle(
                    drawRect.X + drawRect.Width / 2 - 5,
                    drawRect.Y + drawRect.Height / 2 - 3,
                    8, 8);

                e.Graphics.FillRectangle(activeIconColorBrush, rect);
                e.Graphics.DrawRectangle(activeIconColorPen, rect);
            }
        }

        protected virtual void drawMinimizeIcon(PaintEventArgs e, Rectangle drawRect) {
            e.Graphics.DrawLine(
                activeIconColorPen,
                drawRect.X + drawRect.Width / 2 - 5,
                drawRect.Y + drawRect.Height / 2,
                drawRect.X + drawRect.Width / 2 + 5,
                drawRect.Y + drawRect.Height / 2);
        }
    }
}

六种颜色使按钮在外观上完全可自定义。

使用 WindowsDefaultTitleBarButton.Type ButtonType 选择按钮应具有的行为和图标。

示例: 使用visual studio2019项目选择window.

的颜色主题

值:

形式:

BackColor: #252526

关闭按钮:

Size: 46x30

BackColor:  Transparent
HoverColor: #e81123
ClickColor: #f1707a

IconColor:      #f1f1f1
HoverIconColor: #ffffff
ClickIconColor: #ffffff

最小最大按钮:

Size: 46x30

BackColor:  Transparent
HoverColor: #3f3f40
ClickColor: #007acc

IconColor:      #f1f1f1
HoverIconColor: #ffffff
ClickIconColor: #ffffff