如何更改复选框上的检查图像
它有文字,图像,然后是复选框,
我想使用更好的图像进行检查,但无法找到更改已检查和未检查图像的方法
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;
谁知道一个不需要我自己控制的人呢?
如果您正在寻找如何在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)); } } }
它非常简单,但它为您提供了基本的想法。
对于那些不想重写OnPaint的人来说,还有另一种解决方案:
- 添加
ImageList
控件并用图像填充它以反映已检查/未检查的状态。 - 将
Checkbox
控件的Appearance
属性设置为Button
(以摆脱标准的CheckBox图标) - 将其
FlatStyle
属性设置为Flat
(以便控件看起来不像按钮)。
注意:您可能还想检查其’FlatAppearance
属性组。 即CheckedBackColor
,MouseDownBackColor
,MouseOverBackColor
,即将它们全部设置为Control
值。 - 将
Checkbox
控件的ImageList
属性设置为ImageList
控件的名称。 - 设置
Checkbox
控件的Imageindex
和ImageAlign
属性以反映其当前状态。 - 最后也是最重要的是设置
Checkbox
控件的TextImageRelation
属性(除非您需要,否则此值不会让文本和图像重叠)。 即ImageBeforetext
值表示常见的CheckBox图标位置。
现在唯一要做的就是在状态改变时更改图像,如下所示:
private void chkMyCheckBoxWithAnImage_CheckedChanged(object sender, EventArgs e) { if (chkMyCheckBoxWithAnImage.Checked) chkMyCheckBoxWithAnImage.ImageIndex = 1; else chkMyCheckBoxWithAnImage.ImageIndex = 0; }
我以不同的方式解决这个问题,我使用背景图像并将其居中,然后在更改检查时更改主图像。 这看起来像我想要的。
这有一个问题,如果背景图像的尺寸不合适,则检查图像不足,因此看起来不正确。
正如icemanind所描述的那样,正确的解决方案。
一个简单的:
覆盖复选框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示例