首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获取默认的WIndows系统图标,以便最小化、最大化和关闭

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

Stack Overflow用户
提问于 2016-08-04 07:46:15
回答 3查看 3.7K关注 0票数 0

我想要创建一个无边界的WinForm,它有一个具有默认系统图标的自定义头:

  • 极小化
  • 最大化
  • 正在关闭

有没有办法在C#或VB中实现这一点?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-08-04 11:39:52

不知道你的确切目标是什么,但从本质上讲,对于使用C#的“自定义”设计,我更喜欢WPF ()而不是Windows .

我想在Windows窗体中也是可能的,也许您可以按需要删除边框并创建3个按钮,使用常见的Windows符号作为背景?但我不确定它是否起作用;)

编辑:

使用控件“画图”-事件,您应该能够达到目标:

代码语言:javascript
运行
复制
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-关闭按钮Control.Paint-Event

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

票数 2
EN

Stack Overflow用户

发布于 2020-10-11 04:02:43

对于每个人,谁都想要一个完全可定制的按钮在windows 10航空风格。

下面是:

首先,修复错误的基类,即当窗体失去焦点时,焦点突出的按钮获得大纲:

代码语言:javascript
运行
复制
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);
        }
    }
}

接下来是实际按钮:

代码语言:javascript
运行
复制
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选择按钮应该具有的行为和图标。

示例:使用VisualStudio2019项目选择窗口的颜色主题。

值:

表格:

代码语言:javascript
运行
复制
BackColor: #252526

关闭按钮:

代码语言:javascript
运行
复制
Size: 46x30

BackColor:  Transparent
HoverColor: #e81123
ClickColor: #f1707a

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

MinMax按钮:

代码语言:javascript
运行
复制
Size: 46x30

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

IconColor:      #f1f1f1
HoverIconColor: #ffffff
ClickIconColor: #ffffff
票数 2
EN

Stack Overflow用户

发布于 2016-08-04 09:05:54

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

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

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

试试这个T3 Vb.NET主题档案

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38761391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档