簡體   English   中英

CheckedListBox:如何增加復選框的大小並更改刻度線的顏色

[英]CheckedListBox: How to increase size of checkbox and change color of tick mark

我想覆蓋CheckedListBox的默認外觀,如下所示: 列表框中的大復選框

請注意復選框和彩色刻度線的增加。

為此,您需要通過繼承CheckedListbox創建自己的自定義控件,並且需要覆蓋OnDrawItem(DrawItemEventArgs e)事件

以下是代碼:

class BigCheckedListBox : CheckedListBox
{
    public BigCheckedListBox()
    {
        ForeColor = Color.Turquoise;
        Font = new Font("Segoe UI", 12f);
    }
    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        e.DrawBackground();

        var b = e.Bounds;
        var state = GetItemChecked(e.Index) ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal;
        Size glyphSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, state);
        int checkPad = (b.Height - glyphSize.Height) / 2;
        var pt = new Point(b.X + checkPad, b.Y + checkPad);

        Rectangle rect = new Rectangle(pt, new Size(20, 20));
        e.Graphics.DrawRectangle(Pens.Green, rect);//This is for Checkbox rectangle

        //This is for drawing string text
        using (SolidBrush brush = new SolidBrush(ForeColor))
            e.Graphics.DrawString(this.Items[e.Index].ToString(), Font, brush, pt.X + 27f, pt.Y); 


        if (state == CheckBoxState.CheckedNormal)
        {
            using (SolidBrush brush = new SolidBrush(ForeColor))
            using (Font wing = new Font("Wingdings", 17f, FontStyle.Bold))
                e.Graphics.DrawString("ü", wing, brush, pt.X-4, pt.Y-1); //This is For tick mark
        }
    }
}

希望這能達到目的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM