Change the color of a small circle (dot) contained within the radio button to be red?

后端 未结 2 1427
一个人的身影
一个人的身影 2021-02-09 12:03

How do I change the color of a small circle (dot) contained within the radio button to be red in Winform Application use VB.NET or C#?

Regard & Thanks, Dewi

相关标签:
2条回答
  • 2021-02-09 12:25

    Here's a winforms example of an owner drawn listbox simulating a list of radiobuttons that you could use for what you want.

    Edit: Here is a more in-depth Winforms custom control example.

    0 讨论(0)
  • 2021-02-09 12:32

    I took the OP's VB code as a base and with a bit of MSDN help on using the PathGradientBrush came up with this derrived class for C#. The green and red buttons as seen in the image below are using my code, the two blue ones are the regular version.

    Form1 - Displaying Coloured Radio Buttons

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    
    namespace WindowsFormsApplication1
    {
        public class ColouredRadioButton : RadioButton
        {
            // Fields
            private Color m_OnColour;
            private Color m_OffColour;
            private Rectangle m_glint;
            private Rectangle m_circle;
            private PathGradientBrush m_flareBrush;
            private Pen m_outline;
    
            // Properties
            public Color OnColour
            {
                get
                {
                    return m_OnColour;
                }
                set
                {
                    if ((value == Color.White) || (value == Color.Transparent))
                        m_OnColour = Color.Empty;
                    else
                        m_OnColour = value;
                }
            }
            public Color OffColour
            {
                get
                {
                    return m_OffColour;
                }
                set
                {
                    if ((value == Color.White) || (value == Color.Transparent))
                        m_OffColour = Color.Empty;
                    else
                        m_OffColour = value;
                }
            }
    
            // Constructor
            public ColouredRadioButton()
            {
                // Init
                m_circle = new Rectangle(2, 5, 7, 7 /*Magic Numbers*/);
                m_glint = new Rectangle(3, 6, 4, 4  /*Magic Numbers*/);
                m_outline = new Pen(new SolidBrush(Color.Black), 1F /*Magic Numbers*/);
    
                // Generate Glint
                GraphicsPath Path = new GraphicsPath();
                Path.AddEllipse(m_glint);
                m_flareBrush = new PathGradientBrush(Path);
                m_flareBrush.CenterColor = Color.White;
                m_flareBrush.SurroundColors = new Color[] { Color.Transparent };
                m_flareBrush.FocusScales = new PointF(0.5F, 0.5F/*Magic Numbers*/);
    
                // Allows for Overlaying
                SetStyle(ControlStyles.SupportsTransparentBackColor, true);
                BackColor = Color.Transparent;
            }
    
            // Methods
            protected override void OnPaint(PaintEventArgs e)
            {
                // Init
                base.OnPaint(e);
                Graphics g = e.Graphics;
                g.SmoothingMode = SmoothingMode.AntiAlias;
    
                // Overlay Graphic
                if (this.Checked)
                {
                    if (OnColour != Color.Empty)
                    {
                        g.FillEllipse(new SolidBrush(OnColour), m_circle);
                        g.FillEllipse(m_flareBrush, m_glint);
                        g.DrawEllipse(m_outline, m_circle);
                    }
                }
                else
                {
                    if (OffColour != Color.Empty)
                    {
                        g.FillEllipse(new SolidBrush(OffColour), m_circle);
                        g.FillEllipse(m_flareBrush, m_glint);
                        g.DrawEllipse(m_outline, m_circle);
                    }
                }
            }
        }
    }
    

    If you're curious and want the code for the big red ball that I was using it to test out the brush features, here you go...

    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 WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
    
                int Offset = 25;
                int BoxSize = 100;
                int GlintSize = (int)((double)BoxSize * ((double)3 / (double)4));
                Rectangle Circle = new Rectangle(Offset, Offset, BoxSize, BoxSize);
                Rectangle Glint = new Rectangle(Offset, Offset, GlintSize, GlintSize);
    
                //Debug
                //e.Graphics.FillRectangle(new SolidBrush(Color.Red), Circle);
                //e.Graphics.FillRectangle(new SolidBrush(Color.BlueViolet), Glint);
    
                //Generate Glint
                GraphicsPath P = new GraphicsPath();
                P.AddEllipse(Glint);
                PathGradientBrush FlareBrush = new PathGradientBrush(P);
                FlareBrush.CenterColor = Color.FromArgb(255, 255, 255, 255);
                Color[] colors = { Color.Transparent };
                FlareBrush.SurroundColors = colors;
    
                e.Graphics.FillEllipse(new SolidBrush(Color.FromArgb(255, 255, 0, 0)), Circle);
                e.Graphics.FillEllipse(FlareBrush, Glint);
                e.Graphics.DrawEllipse(new Pen(new SolidBrush(Color.Black), 1F), Circle);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题