winforms 一种圆角纽扣

zpqajqem  于 2023-10-23  发布在  其他
关注(0)|答案(1)|浏览(96)

在C#7.3版本中是否可以编写一个圆角矩形按钮?我试过这个:

public class RoundCorneredButton : Button
{
    protected override void OnPaint(PaintEventArgs e)
    {
        GraphicsPath path = new GraphicsPath();
        int cornerRadius = 10; // Adjust this value to control the roundness of the corners.

        // Define the points to create a parallelepiped shape.
        Point[] points =
    {
        new Point(0, Height / 2),
        new Point(cornerRadius, 0),
        new Point(Width - cornerRadius, 0),
        new Point(Width, Height / 2),
        new Point(Width - cornerRadius, Height),
        new Point(cornerRadius, Height)
    };

        // Add the points to the GraphicsPath.
        path.AddPolygon(points);

        // Create a rounded rectangle using the GraphicsPath.
        Region roundedRectangle = new Region(path);

        // Draw the button's text and focus rectangle.
        base.OnPaint(e);
    }
}
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        DoubleBuffered = true;
        _customButton = new RoundCorneredButton();
        _customButton.Text = "Custom Button";
        _customButton.Size = new Size(200, 100);
        _customButton.Location = new Point(50, 50);
        _customButton.FlatStyle = FlatStyle.Standard;
        Controls.Add(_customButton);
    }

    private RoundCorneredButton _customButton;
}

程序将运行并显示一个按钮。问题是角没有倒圆。

lymnna71

lymnna711#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace CustomButton
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            DoubleBuffered = true;

            _borderThickness = 5;
            _borderColor = Color.Red;
            _cornerRadius = 30;
            _foregroundColor = Color.Blue;
            _backgroundColor = Color.Yellow;
            _customButton = new RoundCorneredButton(_borderThickness, _borderColor, _cornerRadius, _foregroundColor, _backgroundColor);
            _customButton.Text = "Custom Button";
            _customButton.Size = new Size(200, 100);
            _customButton.Location = new Point(50, 50);
            _customButton.Visible = true;
            Controls.Add(_customButton);
        }

        private RoundCorneredButton _customButton;
        private int _borderThickness;
        private Color _borderColor;
        private int _cornerRadius;
        private Color _foregroundColor;
        private Color _backgroundColor;
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing;

namespace CustomButton
{
    public class RoundCorneredButton : Button
    {
        private int _borderThickness;
        private Color _borderColor;
        private int _cornerRadius;
        private Color _backgroundColor;

        public RoundCorneredButton(int borderThickness, Color borderColor, int cornerRadius, Color foregroundColor, Color backgroundColor)
        {
            _borderThickness = borderThickness;
            _borderColor = borderColor;
            _cornerRadius = cornerRadius;
            _backgroundColor = backgroundColor;

            FlatStyle = FlatStyle.Flat;
            FlatAppearance.BorderSize = 0; // Disable the default border
            FlatAppearance.MouseDownBackColor = backgroundColor;
            FlatAppearance.MouseOverBackColor = backgroundColor;

            ForeColor = foregroundColor;

            Size = new Size(200, 100);

            // Set default background colors.
            BackColor = Color.Transparent;
            _normalBackColor = backgroundColor;
            _mouseOverBackColor = Color.LightGray;
            _mouseDownBackColor = Color.Gray;
        }

        private Color _normalBackColor;
        private Color _mouseOverBackColor;
        private Color _mouseDownBackColor;
        private bool _isMouseOver;
        private bool _isMouseDown;

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

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            _isMouseOver = false;
            _isMouseDown = false;
            Invalidate();
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                _isMouseDown = true;
                Invalidate();
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            _isMouseDown = false;
            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            // Calculate the adjusted bounds for the border and background.
            Rectangle adjustedBounds = new Rectangle(_borderThickness / 2, _borderThickness / 2, Width - _borderThickness, Height - _borderThickness);

            using (GraphicsPath path = new GraphicsPath())
            {
                int diameter = 2 * _cornerRadius;

                // Adjust the corner rectangle positions.
                RectangleF topLeftCornerRect = new RectangleF(adjustedBounds.Left, adjustedBounds.Top, diameter, diameter);
                RectangleF topRightCornerRect = new RectangleF(adjustedBounds.Right - diameter, adjustedBounds.Top, diameter, diameter);
                RectangleF bottomRightCornerRect = new RectangleF(adjustedBounds.Right - diameter, adjustedBounds.Bottom - diameter, diameter, diameter);
                RectangleF bottomLeftCornerRect = new RectangleF(adjustedBounds.Left, adjustedBounds.Bottom - diameter, diameter, diameter);

                path.AddArc(topLeftCornerRect, 180, 90);
                path.AddArc(topRightCornerRect, 270, 90);
                path.AddArc(bottomRightCornerRect, 0, 90);
                path.AddArc(bottomLeftCornerRect, 90, 90);
                path.CloseAllFigures();

                using (Pen borderPen = new Pen(_borderColor, _borderThickness))
                {
                    e.Graphics.DrawPath(borderPen, path);
                }
            }

            Color buttonColor = _normalBackColor;

            if (_isMouseOver)
            {
                buttonColor = _mouseOverBackColor;
            }

            if (_isMouseDown)
            {
                buttonColor = _mouseDownBackColor;
            }

            using (Brush brush = new SolidBrush(buttonColor))
            {
                e.Graphics.FillPath(brush, RoundedRectangle(adjustedBounds, _cornerRadius));
            }

            TextRenderer.DrawText(e.Graphics, Text, Font, adjustedBounds, ForeColor, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
        }

        private GraphicsPath RoundedRectangle(Rectangle bounds, int radius)
        {
            GraphicsPath path = new GraphicsPath();

            int diameter = radius * 2;
            Rectangle arcRect = new Rectangle(bounds.Location, new Size(diameter, diameter));

            // Top left arc.
            path.AddArc(arcRect, 180, 90);

            // Top right arc.
            arcRect.X = bounds. Right - diameter;
            path.AddArc(arcRect, 270, 90);

            // Bottom right arc.
            arcRect.Y = bounds. Bottom - diameter;
            path.AddArc(arcRect, 0, 90);

            // Bottom left arc.
            arcRect.X = bounds. Left;
            path.AddArc(arcRect, 90, 90);

            path.CloseFigure();

            return path;
        }
    }
}

相关问题