C#CheckedListBox设置item字体

效果图

 

public class MyCheckedListBox : CheckedListBox
{
    //普通字体
    static Font Font1 = new Font(new FontFamily("宋体"), 12, FontStyle.Regular);
    //中间有横线
    static Font Font2 = new Font(new FontFamily("宋体"), 12, FontStyle.Strikeout);
    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        this.Font = Font1;
        //选中
        if (this.CheckedIndices.Contains(e.Index))
        {
            //文本   
            this.Font = Font2;//这里设置每个item的字体 ,过去的某个类似的经验,让我想到这一点
        }

        DrawItemEventArgs e2 = new DrawItemEventArgs(
            e.Graphics,
            Font,//这里设置字体没有效果,猜测绘制用的 this.Font 而不是这里的
            new Rectangle(e.Bounds.Location, e.Bounds.Size),
            e.Index,
            (e.State & DrawItemState.Focus) == DrawItemState.Focus ? DrawItemState.Focus : DrawItemState.None,
            this.ForeColor, //这里可以设置字体颜色
            this.BackColor);

        base.OnDrawItem(e2);

    }
    
    protected override void OnFontChanged(EventArgs e)
    {
        //base.OnFontChanged(e);//不注释会不停闪烁
    }
}

 文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿文字少不能投稿

posted @ 2022-02-08 16:19  212的s  阅读(500)  评论(0编辑  收藏  举报