VB.NET:图像作为复选框状态框

时间:2013-01-14 13:18:17

标签: vb.net checkbox background

是否可以使用图像作为复选框“已选中”指示符方块?

我知道我可以使用背景图像,但这也是标签的背后,也是不可能(据我所知)对齐它。

如何使用图片而不是正方形并保留标签和所有其他自定义设置?

提前致谢!

3 个答案:

答案 0 :(得分:4)

你看起来像这样?

Dim frm As New Form
frm.Size = New Size(320, 200)

Dim iList As New ImageList
iList.Images.Add(Image.FromFile("check.png"), Color.White)
iList.Images.Add(Image.FromFile("uncheck.png"), Color.White)

Dim chk As New CheckBox
chk.Text = "Check Box With Image"
chk.AutoSize = False
chk.Size = New Size(350, 20)
chk.ImageList = iList
chk.ImageIndex = 1
chk.CheckAlign = ContentAlignment.MiddleRight
chk.ImageAlign = ContentAlignment.MiddleLeft
chk.TextImageRelation = TextImageRelation.ImageBeforeText
chk.Location = New Point(32, 32)

frm.Controls.Add(chk)

AddHandler chk.CheckStateChanged,
    Sub(sender1 As Object, e1 As EventArgs)
        chk.ImageIndex = IIf(chk.Checked, 0, 1)
    End Sub

frm.ShowDialog()

答案 1 :(得分:1)

UPDATE#1 :实际上,下面的@brahm解决方案比我的要好得多!

更新#2 :实际上,事实并非如此。现在,我看到他是如何做到的:他将复选框从可见的Form区域移开,将其移出视线。不是一个好的解决方案......


理想的解决方案是将CheckBox控件子类化,并通过覆盖OnPaint方法进行自己的渲染。

一个更简单但更麻烦的解决方案是将一个PictureBox放在复选框上,并通过PictureBox的{​​{1}}事件控制CheckBox中的图像。

另一种选择: 您仍然可以按照建议使用CheckedChange按钮模式(CheckBox),但随后在其旁边添加一个标签。 然后,处理标签上的Appearance = Button事件以切换Click的已检查状态。然后,最终结果应该为您提供您正在寻找的内容。

答案 2 :(得分:0)

Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.VisualStyles

Public Class ImageCheckBox

  Public State As CheckBoxState = CheckBoxState.UncheckedNormal
  Public Hot As Boolean = False
  Public Pressed As Boolean = False
  Public ImageDictionary As Dictionary(Of CheckBoxState, Image) = New Dictionary(Of CheckBoxState, Image)
  Private Const PaddingModifier As Integer = 2

  Sub New()
    Me.New(New Dictionary(Of CheckBoxState, Image) From {
      {CheckBoxState.CheckedDisabled, My.Resources.form_checkbox_checked},
      {CheckBoxState.CheckedHot, My.Resources.form_checkbox_checked},
      {CheckBoxState.CheckedNormal, My.Resources.form_checkbox_checked},
      {CheckBoxState.CheckedPressed, My.Resources.form_checkbox_checked},
      {CheckBoxState.UncheckedDisabled, My.Resources.form_checkbox_unchecked},
      {CheckBoxState.UncheckedHot, My.Resources.form_checkbox_unchecked},
      {CheckBoxState.UncheckedNormal, My.Resources.form_checkbox_unchecked},
      {CheckBoxState.UncheckedPressed, My.Resources.form_checkbox_unchecked}})
  End Sub

  Sub New(imageDictionary As Dictionary(Of CheckBoxState, Image))

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.


    Me.ImageDictionary = imageDictionary
  End Sub

  Sub CheckBox_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    'Return if the specific Image is not found
    If Not ImageDictionary.ContainsKey(State) Then Return

    'Get the Size of the CheckBox
    Dim glyphSize As Size = CheckBoxRenderer.GetGlyphSize(e.Graphics, State)

    'Get the Location of the CheckBox in relation to the Alignment of it
    Dim glyphLocation As Point
    Select Case Me.CheckAlign
      Case Drawing.ContentAlignment.TopLeft
        glyphLocation = New Point(Me.Padding.Left, Me.Padding.Top)
        Exit Select
      Case Drawing.ContentAlignment.TopCenter
        glyphLocation = New Point(Me.Padding.Left + (Me.Width - glyphSize.Width) / 2, Me.Padding.Top)
        Exit Select
      Case Drawing.ContentAlignment.TopRight
        glyphLocation = New Point(Me.Padding.Left + Me.Width - glyphSize.Width, Me.Padding.Top)
        Exit Select
      Case Drawing.ContentAlignment.MiddleLeft
        glyphLocation = New Point(Me.Padding.Left, Me.Padding.Top + (Me.Height - glyphSize.Height) / 2)
        Exit Select
      Case Drawing.ContentAlignment.MiddleCenter
        glyphLocation = New Point(Me.Padding.Left + (Me.Width - glyphSize.Width) / 2, Me.Padding.Top + (Me.Height - glyphSize.Height) / 2)
        Exit Select
      Case Drawing.ContentAlignment.MiddleRight
        glyphLocation = New Point(Me.Padding.Left + Me.Width - glyphSize.Width, Me.Padding.Top + (Me.Height - glyphSize.Height) / 2)
        Exit Select
      Case Drawing.ContentAlignment.BottomLeft
        glyphLocation = New Point(Me.Padding.Left, Me.Padding.Top + Me.Height - glyphSize.Height)
        Exit Select
      Case Drawing.ContentAlignment.BottomCenter
        glyphLocation = New Point(Me.Padding.Left + (Me.Width - glyphSize.Width) / 2, Me.Padding.Top + Me.Height - glyphSize.Height)
        Exit Select
      Case Drawing.ContentAlignment.BottomRight
        glyphLocation = New Point(Me.Padding.Left + Me.Width - glyphSize.Width, Me.Padding.Top + Me.Height - glyphSize.Height)
        Exit Select
    End Select

    'Set the drawing Area
    Dim glyphRectangle As Rectangle = New Rectangle(glyphLocation, glyphSize)

    'Enlarge the Rectangle to completely hide default symbol
    Dim clearRectangle As Rectangle = New Rectangle(glyphLocation.X - PaddingModifier,
                                                    glyphLocation.Y - PaddingModifier,
                                                    glyphSize.Width + 2 * PaddingModifier,
                                                    glyphSize.Height + 2 * PaddingModifier)

    'Draw the Parent Background over the default CheckBox to clear it
    CheckBoxRenderer.DrawParentBackground(e.Graphics, clearRectangle, Me)

    Debug.WriteLine(State)
    'Finally draw the custom CheckBox image on the position of the default one
    e.Graphics.DrawImage(ImageDictionary(State), glyphRectangle)
  End Sub

  Sub CheckBox_MouseClick(sender As Object, e As EventArgs) Handles Me.MouseClick
    Me.Checked = Not Me.Checked
  End Sub
  Sub CheckBox_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
    Me.Pressed = True
  End Sub
  Sub CheckBox_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
    Me.Pressed = False
  End Sub
  Sub CheckBox_MouseEnter(sender As Object, e As EventArgs) Handles Me.MouseEnter
    Me.Hot = True
  End Sub
  Sub CheckBox_MouseLeave(sender As Object, e As EventArgs) Handles Me.MouseLeave
    Me.Hot = False
  End Sub

  Public Sub updateState() Handles Me.MouseClick, Me.MouseDown, Me.MouseUp, Me.MouseEnter, Me.MouseLeave, Me.EnabledChanged
    Debug.WriteLine(Me.Checked & " " & Me.Enabled & " " & Me.Hot & " " & Me.Pressed)
    Me.State = CurrentState()
    Me.Refresh()
    Debug.WriteLine(State)
  End Sub

  Public Function CurrentState() As CheckBoxState
    If (Me.Checked) Then
      If (Not Me.Enabled) Then Return CheckBoxState.CheckedDisabled
      If (Me.Pressed) Then Return CheckBoxState.CheckedPressed
      If (Me.Hot) Then Return CheckBoxState.CheckedHot
      Return CheckBoxState.CheckedNormal
    Else
      If (Not Me.Enabled) Then Return CheckBoxState.UncheckedDisabled
      If (Me.Pressed) Then Return CheckBoxState.UncheckedPressed
      If (Me.Hot) Then Return CheckBoxState.UncheckedHot
      Return CheckBoxState.UncheckedNormal
    End If
  End Function

End Class