将单选按钮中包含的小圆圈点的颜色更改为红色?

如何解决将单选按钮中包含的小圆圈点的颜色更改为红色?

| 在Winform Application中,如何使用VB.NET或C#将单选按钮中包含的小圆圈(点)的颜色更改为红色? 谢谢,谢谢 德威 ================================================== ======== 我将分享,可能对其他人有用。该程序有效。
Imports System.Drawing.Drawing2D

Public Class Form1

Public Class MyRadioButton
    Inherits RadioButton

    Private m_OnColor As Color
    Private m_OffColor As Color

    Public Sub New(ByVal On_Color As Color,ByVal Off_Color As Color)
        m_OnColor = On_Color
        m_OffColor = Off_Color
        SetStyle(ControlStyles.SupportsTransparentBackColor,True)
        BackColor = Color.Transparent
    End Sub

    Public Property OnColor() As Color
        Get
            Return m_OnColor
        End Get
        Set(ByVal value As Color)
            m_OnColor = value
        End Set
    End Property

    Public Property OffColor() As Color
        Get
            Return m_OffColor
        End Get
        Set(ByVal value As Color)
            m_OffColor = value
        End Set
    End Property

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        MyBase.OnPaint(e)

        Dim g As Graphics = e.Graphics
        g.SmoothingMode = SmoothingMode.AntiAlias

        Dim dotDiameter As Integer = ClientRectangle.Height - 17
        Dim innerRect As New RectangleF(1.8F,7.8F,dotDiameter,dotDiameter)

        If Me.Checked Then
            g.FillEllipse(New SolidBrush(OnColor),innerRect)
        Else
            g.FillEllipse(New SolidBrush(OffColor),innerRect)
        End If

        g.DrawString(Text,Font,New SolidBrush(ForeColor),dotDiameter + 17,1)
    End Sub

End Class


Dim objRadio As New MyRadioButton(Color.Blue,Color.Red)

Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
    objRadio.Location = New Point(100,100)
    objRadio.Visible = True
    Me.Controls.Add(objRadio)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
    If objRadio.Checked Then
        objRadio.Checked = False
    Else
        objRadio.Checked = True
    End If
End Sub


End Class
    

解决方法

        这是一个winforms示例,其中包含一个所有者绘制的列表框,该列表框模拟了可用于所需功能的单选按钮列表。 编辑:这是一个更深入的Winforms自定义控件示例。     ,        我以OP的VB代码为基础,并在MSDN帮助下使用PathGradientBrush提出了此派生的C#类。如下图所示,绿色和红色按钮正在使用我的代码,两个蓝色按钮是常规版本。
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);
                }
            }
        }
    }
}
如果您好奇并想要我用来测试笔刷功能的红色大球代码,那么您就可以...
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,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);
            Color[] colors = { Color.Transparent };
            FlareBrush.SurroundColors = colors;

            e.Graphics.FillEllipse(new SolidBrush(Color.FromArgb(255,0)),Circle);
            e.Graphics.FillEllipse(FlareBrush,Glint);
            e.Graphics.DrawEllipse(new Pen(new SolidBrush(Color.Black),1F),Circle);
        }
    }
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-