12

它有文本、图像,然后是复选框,

我想使用更好的图像进行检查,但找不到更改已检查和未检查图像的方法

this.checkBox1.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
this.checkBox1.Checked = true;
this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBox1.Image = global::ClientExam.Properties.Resources.action32;
this.checkBox1.Location = new System.Drawing.Point(145, 140);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(273, 127);
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "checkBox1";
this.checkBox1.TextImageRelation = System.Windows.Forms.TextImageRelation.TextBeforeImage;
this.checkBox1.UseVisualStyleBackColor = true;

有人知道不需要我自己编写控件的吗?

4

5 回答 5

20

对于不想覆盖 OnPaint 的任何人,有一个替代解决方案:

  1. 添加一个ImageList控件并用图像填充它以反映选中/未选中状态。
  2. Checkbox控件的Appearance属性设置为Button(摆脱标准 CheckBox 图标)
  3. 将它的FlatStyle属性设置为Flat(这样控件看起来就不像一个按钮)。
    注意:您可能还想检查它FlatAppearance的属性组。即CheckedBackColor, MouseDownBackColor, MouseOverBackColor, 即将它们全部设置为Control值。
  4. Checkbox控件的ImageList属性设置为控件的名称ImageList
  5. 设置Checkbox控件ImageindexImageAlign属性以反映其当前状态。
  6. 最后也是最重要的设置Checkbox控件的TextImageRelation属性(此值不会让文本和图像重叠,除非您希望它们重叠)。ieImageBeforetext值代表常见的 CheckBox 图标位置。

现在唯一要做的就是在状态改变时改变图像,像这样:

    private void chkMyCheckBoxWithAnImage_CheckedChanged(object sender, EventArgs e)
    {
        if (chkMyCheckBoxWithAnImage.Checked)
            chkMyCheckBoxWithAnImage.ImageIndex = 1;
        else
            chkMyCheckBoxWithAnImage.ImageIndex = 0;
    }
于 2016-06-01T09:19:43.230 回答
10

如果您正在寻找如何在 Winforms 中执行此操作,简单的答案是创建一个派生自 CheckBox 的新复选框类,然后重写OnPaint方法。

这是一个关于如何通过覆盖该OnPaint方法来创建自定义外观复选框的示例:

public class CustomCheckBox : CheckBox
{
    public CustomCheckBox()
    {
        this.SetStyle(ControlStyles.UserPaint, true);
        this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    }

    protected override void OnPaint(PaintEventArgs pevent)
    {
        base.OnPaint(pevent);
        if (this.Checked)
        {
            pevent.Graphics.FillRectangle(new SolidBrush(Color.Blue), new Rectangle(0, 0, 16, 16));
        }
        else
        {
            pevent.Graphics.FillRectangle(new SolidBrush(Color.Red), new Rectangle(0, 0, 16, 16));
        }
    }
}

这很简单,但它给了你基本的想法。

于 2013-04-04T14:24:31.930 回答
0

一个简单的:

覆盖复选框OnPaint(PaintEventArgs e)如下:

Graphics g = e.Graphics;

        base.OnPaint(e);
        //// Fill the background
        //SetControlSizes();

        // Paint the outer rounded rectangle
        g.SmoothingMode = SmoothingMode.AntiAlias;
        using (GraphicsPath outerPath = GeneralUtilities.RoundedRectangle(mLabelRect, 1, 0))
        {
            using (LinearGradientBrush outerBrush = new LinearGradientBrush(mLabelRect,
                   mGradientTop, mGradientBottom, LinearGradientMode.Vertical))
            {
                g.FillPath(outerBrush, outerPath);
            }
            using (Pen outlinePen = new Pen(mGradientTop, mRectOutlineWidth))
            {
                outlinePen.Alignment = PenAlignment.Inset;
                g.DrawPath(outlinePen, outerPath);
            }
        }

        //// Paint the gel highlight
        using (GraphicsPath innerPath = GeneralUtilities.RoundedRectangle(mHighlightRect, mRectCornerRadius, mHighlightRectOffset))
        {
            using (LinearGradientBrush innerBrush = new LinearGradientBrush(mHighlightRect,
                   Color.FromArgb(mHighlightAlphaTop, Color.White),
                   Color.FromArgb(mHighlightAlphaBottom, Color.White), LinearGradientMode.Vertical))
            {
                g.FillPath(innerBrush, innerPath);
            }
        }
        // Paint the text
        TextRenderer.DrawText(g, Text, Font, mLabelRect, Color.White, Color.Transparent,
        TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis);

但是如果你想拥有一个好的你必须使用wpf CheckBox ControlTemplate Example

于 2013-04-04T14:28:50.820 回答
0

我以不同的方式解决了这个问题,我使用背景图像并将其居中,然后在检查更改时更改主图像。这似乎是我想要的。

这样做有一个问题,如果背景图像尺寸不合适,与检查图像重叠,那么它看起来就不正确。

正确的解决方案是 icemanind 描述的。

于 2014-01-09T14:56:44.647 回答
0

自定义复选框

完整的自定义 CheckBox 控件,支持主题化

  1. 预定义样式(VS 2019 Dark and Light)。

  2. 支持 AutoSize 属性。

  3. 支持焦点、鼠标输入和鼠标按下事件中的自定义颜色。

  4. 支持自定义复选标记(形状和颜色)。

  5. 用于自定义 CheckBox 控件外观的公共属性。

    'Copyright (c) Smart PC Utilities, Ltd.
    'All rights reserved.
    
    #Region "References"
    
    Imports System.ComponentModel
    Imports System.Drawing.Drawing2D
    
    #End Region
    
    Namespace FlatUI
    
    ''' <summary>
    ''' Flat CheckBox control
    ''' </summary>
    Public NotInheritable Class FlatCheckBox
        Inherits CheckBox
    
    #Region "Private Members"
    
        Dim _theme As UITheme = UITheme.LightBlue
    
        Private _boxNormalBackColor As Color = Color.FromArgb(243, 249, 255)
        Private _boxFocusBackColor As Color = Color.FromArgb(243, 249, 255)
        Private _boxMouseDownBackColor As Color = Color.FromArgb(51, 153, 255)
    
        Private _boxNormalBorderColor As Color = Color.FromArgb(30, 30, 30)
        Private _boxFocusBorderColor As Color = Color.FromArgb(51, 153, 255)
        Private _boxMouseDownBorderColor As Color = Color.FromArgb(51, 153, 255)
    
        Private _checkMarkNormalColor As Color = Color.FromArgb(30, 30, 30)
        Private _checkMarkFocusColor As Color = Color.FromArgb(30, 30, 30)
        Private _checkMarkMouseDownColor As Color = Color.FromArgb(243, 249, 255)
    
        Private _focusCuesColor As Color = Color.Gray
    
        Private _isMouseEnter As Boolean
        Private _isMouseDown As Boolean
    
    #End Region
    
    #Region "Constants"
    
        Const TEXT_X = 21
        Const TEXT_Y = 2
    
        Const BOX_X = 4
        Const BOX_Y = 4
    
        Const BOX_WIDTH = 12
        Const BOX_HEIGHT = 12
    
    #End Region
    
    #Region "Public Methods"
    
        Public Sub New()
    
            SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
    
            BackColor = Color.FromArgb(251, 251, 251)
            ForeColor = Color.FromArgb(30, 30, 30)
            Appearance = Appearance.Button
            FlatStyle = FlatStyle.Flat
            FlatAppearance.BorderSize = 0
            DoubleBuffered = True
            UseVisualStyleBackColor = False
    
        End Sub
    
    #End Region
    
    #Region "Public Properties"
    
        <Localizable(True)> <Category("Flat CheckBox")> <Description("The box backcolor in the normal state.")> <DefaultValue(GetType(Color), "243, 249, 255")>
        Public Property BoxNormalBackColor As Color
            Get
                Return _boxNormalBackColor
            End Get
            Set(value As Color)
                _boxNormalBackColor = value
                Invalidate()
            End Set
        End Property
    
        <Localizable(True)> <Category("Flat CheckBox")> <Description("The box backcolor when the CheckBox control is focused.")> <DefaultValue(GetType(Color), "243, 249, 255")>
        Public Property BoxFocusBackColor As Color
            Get
                Return _boxFocusBackColor
            End Get
            Set(value As Color)
                _boxFocusBackColor = value
                Invalidate()
            End Set
        End Property
    
        <Localizable(True)> <Category("Flat CheckBox")> <Description("The box backcolor when the CheckBox control is clicked.")> <DefaultValue(GetType(Color), "51, 153, 255")>
        Public Property BoxMouseDownBackColor As Color
            Get
                Return _boxMouseDownBackColor
            End Get
            Set(value As Color)
                _boxMouseDownBackColor = value
                Invalidate()
            End Set
        End Property
    
        <Localizable(True)> <Category("Flat CheckBox")> <Description("The box border color in the normal state.")> <DefaultValue(GetType(Color), "30, 30, 30")>
        Public Property BoxNormalBorderColor As Color
            Get
                Return _boxNormalBorderColor
            End Get
            Set(value As Color)
                _boxNormalBorderColor = value
                Invalidate()
            End Set
        End Property
    
        <Localizable(True)> <Category("Flat CheckBox")> <Description("The box border color when the CheckBox control is focused.")> <DefaultValue(GetType(Color), "51, 153, 255")>
        Public Property BoxFocusBorderColor As Color
            Get
                Return _boxFocusBorderColor
            End Get
            Set(value As Color)
                _boxFocusBorderColor = value
                Invalidate()
            End Set
        End Property
    
        <Localizable(True)> <Category("Flat CheckBox")> <Description("The box border color when the CheckBox control is clicked.")> <DefaultValue(GetType(Color), "51, 153, 255")>
        Public Property BoxMouseDownBorderColor As Color
            Get
                Return _boxMouseDownBorderColor
            End Get
            Set(value As Color)
                _boxMouseDownBorderColor = value
                Invalidate()
            End Set
        End Property
    
        <Localizable(True)> <Category("Flat CheckBox")> <Description("The CheckBox check-mark color in the normal state.")> <DefaultValue(GetType(Color), "30, 30, 30")>
        Public Property CheckMarkNormalColor As Color
            Get
                Return _checkMarkNormalColor
            End Get
            Set(value As Color)
                _checkMarkNormalColor = value
                Invalidate()
            End Set
        End Property
    
        <Localizable(True)> <Category("Flat CheckBox")> <Description("The CheckBox check-mark color when the CheckBox control is focused.")> <DefaultValue(GetType(Color), "30, 30, 30")>
        Public Property CheckMarkFocusColor As Color
            Get
                Return _checkMarkFocusColor
            End Get
            Set(value As Color)
                _checkMarkFocusColor = value
                Invalidate()
            End Set
        End Property
    
        <Localizable(True)> <Category("Flat CheckBox")> <Description("The CheckBox check-mark color when the CheckBox control is clicked.")> <DefaultValue(GetType(Color), "243, 249, 255")>
        Public Property CheckMarkMouseDownColor As Color
            Get
                Return _checkMarkMouseDownColor
            End Get
            Set(value As Color)
                _checkMarkMouseDownColor = value
                Invalidate()
            End Set
        End Property
    
        <Localizable(True)> <Category("Flat CheckBox")> <Description("The CheckBox control focus cues color.")> <DefaultValue(GetType(Color), "Gray")>
        Public Property FocusCuesColor As Color
            Get
                Return _focusCuesColor
            End Get
            Set(value As Color)
                _focusCuesColor = value
                Invalidate()
            End Set
        End Property
    
        <Category("Flat CheckBox")> <Description("The general style of the Flat CheckBox control.")> <DefaultValue(GetType(UITheme), "1")> Public Property Theme As UITheme
            Get
                Return _theme
            End Get
            Set(value As UITheme)
                _theme = value
    
                If _theme = UITheme.DarkBlue Then
    
                    BackColor = Color.FromArgb(37, 37, 38)
                    ForeColor = Color.Gainsboro
                    BoxNormalBackColor = Color.FromArgb(31, 31, 32)
                    BoxFocusBackColor = Color.FromArgb(31, 31, 32)
                    BoxMouseDownBackColor = Color.FromArgb(0, 122, 204)
                    BoxNormalBorderColor = Color.Gainsboro
                    BoxFocusBorderColor = Color.FromArgb(0, 122, 204)
                    BoxMouseDownBorderColor = Color.FromArgb(0, 122, 204)
                    CheckMarkNormalColor = Color.FromArgb(241, 241, 241)
                    CheckMarkFocusColor = Color.FromArgb(241, 241, 241)
                    CheckMarkMouseDownColor = Color.FromArgb(241, 241, 241)
                    FocusCuesColor = Color.Gray
    
                ElseIf _theme = UITheme.LightBlue Then
    
                    BackColor = Color.FromArgb(251, 251, 251)
                    ForeColor = Color.FromArgb(30, 30, 30)
                    BoxNormalBackColor = Color.FromArgb(243, 249, 255)
                    BoxFocusBackColor = Color.FromArgb(243, 249, 255)
                    BoxMouseDownBackColor = Color.FromArgb(51, 153, 255)
                    BoxNormalBorderColor = Color.FromArgb(30, 30, 30)
                    BoxFocusBorderColor = Color.FromArgb(51, 153, 255)
                    BoxMouseDownBorderColor = Color.FromArgb(51, 153, 255)
                    CheckMarkNormalColor = Color.FromArgb(30, 30, 30)
                    CheckMarkFocusColor = Color.FromArgb(30, 30, 30)
                    CheckMarkMouseDownColor = Color.FromArgb(243, 249, 255)
                    FocusCuesColor = Color.Gray
    
                End If
    
                Invalidate()
    
            End Set
        End Property
    
    #End Region
    
    #Region "Private Methods"
    
        Private Sub ResizeForAutoSize()
            If AutoSize Then SetBoundsCore(Left, Top, Width, Height, BoundsSpecified.Size)
        End Sub
    
        Private Function GetAutoSize() As Size
    
            Dim g = CreateGraphics()
            Dim textSize = g.MeasureString(Text, Font)
            Dim cntrlSize As New Size(CInt(textSize.Width + TEXT_X), CInt(textSize.Height + (TEXT_Y * 2)))
    
            Return cntrlSize
    
        End Function
    
    #End Region
    
    #Region "Overridden Methods"
    
        Protected Overrides Sub OnPaint(e As PaintEventArgs)
    
    #Disable Warning CA1062 ' Validate arguments of public methods
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
    #Enable Warning CA1062 ' Validate arguments of public methods
            e.Graphics.Clear(BackColor)
    
            'Draw CheckBox text
    
            Using b As New SolidBrush(ForeColor)
                e.Graphics.DrawString(Text, Font, b, TEXT_X, TEXT_Y)
            End Using
    
            'Draw box background
    
            Dim boxBackgroundColor As Color
    
            If _isMouseDown Then
                boxBackgroundColor = _boxMouseDownBackColor
            ElseIf Focused OrElse _isMouseEnter Then
                boxBackgroundColor = _boxFocusBackColor
            Else
                boxBackgroundColor = _boxNormalBackColor
            End If
    
            Using b As New SolidBrush(boxBackgroundColor)
                e.Graphics.FillRectangle(b, BOX_X, BOX_Y, BOX_WIDTH, BOX_HEIGHT)
            End Using
    
            'Draw check mark
    
            If Checked Then
    
                Dim checkMarkColor As Color
    
                If _isMouseDown Then
                    checkMarkColor = _checkMarkMouseDownColor
                ElseIf Focused OrElse _isMouseEnter Then
                    checkMarkColor = _checkMarkFocusColor
                Else
                    checkMarkColor = _checkMarkNormalColor
                End If
    
                Dim rectCheck = New Rectangle(BOX_X + 2, BOX_Y + 3, BOX_WIDTH - 4, BOX_HEIGHT - 6)
    
                Using p = New Pen(checkMarkColor, 2)
                    e.Graphics.DrawLines(p, New Point() {New Point(rectCheck.Left, rectCheck.Bottom - CInt(rectCheck.Height / 2)), New Point(rectCheck.Left + CInt(rectCheck.Width / 3), rectCheck.Bottom), New Point(rectCheck.Right, rectCheck.Top)})
                End Using
    
            End If
    
            'Draw box border
    
            Dim boxBorderColor As Color
    
            If _isMouseDown Then
                boxBorderColor = _boxMouseDownBorderColor
            ElseIf Focused OrElse _isMouseEnter Then
                boxBorderColor = _boxFocusBorderColor
            Else
                boxBorderColor = _boxNormalBorderColor
            End If
    
            Using p = New Pen(boxBorderColor)
                e.Graphics.DrawRectangle(p, BOX_X, BOX_Y, BOX_WIDTH, BOX_HEIGHT)
            End Using
    
            'Draw focus cues
    
            If Focused Then
    
                Dim textSize = e.Graphics.MeasureString(Text, Font)
    
                Dim focusRect = New Rectangle(TEXT_X - 1, TEXT_Y - 1, CInt(textSize.Width), CInt(textSize.Height))
                focusRect.Inflate(-1, -1)
    
                Using p As New Pen(_focusCuesColor) With {.DashStyle = DashStyle.Dot}
                    e.Graphics.SmoothingMode = SmoothingMode.Default
                    e.Graphics.DrawRectangle(p, focusRect)
                End Using
    
            End If
    
            ResizeForAutoSize()
    
        End Sub
    
        Public Overrides Function GetPreferredSize(proposedSize As Size) As Size
            Return GetAutoSize()
        End Function
    
        Protected Overrides Sub SetBoundsCore(x As Integer, y As Integer, width As Integer, height As Integer, specified As BoundsSpecified)
    
            If AutoSize AndAlso (specified And BoundsSpecified.Size) <> 0 Then
                Dim cntrlSize = GetAutoSize()
                width = cntrlSize.Width
                height = cntrlSize.Height
            End If
    
            MyBase.SetBoundsCore(x, y, width, height, specified)
    
        End Sub
    
        Protected Overrides Sub OnResize(e As EventArgs)
            MyBase.OnResize(e)
            Invalidate()
        End Sub
    
        Protected Overrides Sub OnMouseEnter(eventargs As EventArgs)
    
            MyBase.OnMouseEnter(eventargs)
    
            _isMouseEnter = True
            Invalidate()
    
        End Sub
    
        Protected Overrides Sub OnMouseLeave(eventargs As EventArgs)
    
            MyBase.OnMouseLeave(eventargs)
    
            _isMouseEnter = False
            Invalidate()
    
        End Sub
    
        Protected Overrides Sub OnMouseDown(mevent As MouseEventArgs)
    
            MyBase.OnMouseDown(mevent)
    
            _isMouseDown = True
            Invalidate()
    
        End Sub
    
        Protected Overrides Sub OnMouseUp(mevent As MouseEventArgs)
    
            MyBase.OnMouseUp(mevent)
    
            _isMouseDown = False
            Invalidate()
    
        End Sub
    
        <DefaultValue(GetType(Color), "251, 251, 251")> Public Overrides Property BackColor As Color
            Get
                Return MyBase.BackColor
            End Get
            Set(value As Color)
                MyBase.BackColor = value
            End Set
        End Property
    
        <DefaultValue(GetType(Color), "30, 30, 30")> Public Overrides Property ForeColor As Color
            Get
                Return MyBase.ForeColor
            End Get
            Set(value As Color)
                MyBase.ForeColor = value
            End Set
        End Property
    
    #End Region
    
    End Class
    
    End Namespace
    

主题枚举

'Copyright (c) Smart PC Utilities, Ltd.
'All rights reserved.

Namespace FlatUI

    Public Enum UITheme As Integer

        ''' <summary>
        ''' Undefined style
        ''' </summary>
        Undefined = -1

        ''' <summary>
        ''' Visual Studio 2019 Dark style
        ''' </summary>
        DarkBlue = 0

        ''' <summary>
        ''' Visual Studio 2019 Light style
        ''' </summary>
        LightBlue = 1

    End Enum

End Namespace
于 2021-12-07T11:52:39.543 回答