【发布时间】:2011-09-21 12:14:11
【问题描述】:
如何在使用 VB.NET 或 C# 的 Winform 应用程序中将单选按钮中包含的小圆圈(点)的颜色更改为红色?
问候和感谢, 迪威
================================================ ============
我会分享,可能对其他人有用。这个程序有效。
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
【问题讨论】:
标签: c# vb.net winforms radio-button