获取默认的WIndows系统图标以最小化,最大化和关闭

时间:2016-08-04 07:46:15

标签: vb.net windows winforms icons default

我想创建一个无边框的WinForm,它有一个带有默认系统图标的自定义标题:

  • 最小化
  • 最大化

有什么办法可以在C#或VB中实现这个目的吗?

3 个答案:

答案 0 :(得分:2)

不确定您的确切目标是什么,但通常情况下,对于使用C#的“自定义”设计,我更喜欢WPF(Windows Presentation Foundation)而不是Windows Forms ......

我想在Windows Forms中也可以这样做,也许如果你想删除边框并创建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等)绘制到按钮/控件的位置。

有关详细信息,请参阅VisualStylesElement-CloseButtonControl.Paint-Event

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

答案 1 :(得分:0)

对于每个人来说,他们都希望Windows 10 Aero样式具有完全可自定义的按钮。

这里是:

首先,一个修复该错误的基类,即当表单失去焦点时,被聚焦的按钮将获得轮廓:

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 Studio 2019项目选择窗口的颜色主题。

enter image description here

值:

表格:

BackColor: #252526

关闭按钮:

Size: 46x30

BackColor:  Transparent
HoverColor: #e81123
ClickColor: #f1707a

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

MinMax按钮:

Size: 46x30

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

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

答案 2 :(得分:-1)

据我所知,您可以使用&#34; Anchor property&#34; +&#34; Doc Property&#34;通过放置带图像的三个按钮来形成窗口。

或者在这里我展示了一个示例,它显示了带控件的Windows窗体的自定义主题。

此示例在Vb.NET.Save中作为项目中的类,构建项目,然后控件将出现在工具箱区域中.Drag&amp;放弃这种控制。享受!

试试这个 T3 Vb.NET Themes Archives