1个回答
展开全部
为了你这个问题想了很多,没有分真是浪费时间了。好了,好人做到底,我给你我忙了后的代码,其实就是重写OnDrawItem而已。
这个是FORM1,其中Form1.Designer.cs为空代码。下图演示就是选中时为背景为透明色
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private System.ComponentModel.IContainer components = null;
private ColorCodedCheckedListBox checkedListBox1;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.checkedListBox1 = new WindowsFormsApplication2.ColorCodedCheckedListBox();
this.SuspendLayout();
this.checkedListBox1.FormattingEnabled = true;
this.checkedListBox1.Items.AddRange(new object[] {"选项1","选项2"});
this.checkedListBox1.Location = new System.Drawing.Point(12, 12);
this.checkedListBox1.Name = "checkedListBox1";
this.checkedListBox1.Size = new System.Drawing.Size(128, 36);
this.checkedListBox1.TabIndex = 0;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(164, 67);
this.Controls.Add(this.checkedListBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
}
class ColorCodedCheckedListBox : CheckedListBox
{
//Color.Orange为颜色,你可修改
protected override void OnDrawItem(DrawItemEventArgs e)
{
DrawItemEventArgs e2 = new DrawItemEventArgs(e.Graphics, e.Font, new Rectangle(e.Bounds.Location, e.Bounds.Size), e.Index, (e.State & DrawItemState.Focus) == DrawItemState.Focus ? DrawItemState.Focus : DrawItemState.None, Color.Orange, this.BackColor);
base.OnDrawItem(e2);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询