[英]How to change DataGridCheckBoxColumn checkbox check mark colour
[英]How can I change the color of the check mark of a CheckBox?
我想更改邊框顏色和方塊的背景以及復選標記的顏色,而不是文本。 為了更好地理解,我想要完成的是以下示例:
checkBox1.Checked = false
checkBox1.Checked = true
非常感謝大家的回應!
您只需在Paint事件中繪制復選標記:
private void checkBox1_Paint(object sender, PaintEventArgs e)
{
Point pt = new Point(e.ClipRectangle.Left + 2, e.ClipRectangle.Top + 4);
Rectangle rect = new Rectangle(pt, new Size(22, 22));
if (checkBox1.Checked)
{
using (Font wing = new Font("Wingdings", 14f))
e.Graphics.DrawString("ü", wing, Brushes.DarkOrange,rect);
}
e.Graphics.DrawRectangle(Pens.DarkSlateBlue, rect);
}
為此,您需要:
Apperance = Appearance.Button
FlatStyle = FlatStyle.Flat
TextAlign = ContentAlignment.MiddleRight
FlatAppearance.BorderSize = 0
AutoSize = false
如果要重新使用它,最好將復選框子類化並覆蓋那里的OnPaint
事件。 這是一個例子:
public ColorCheckBox()
{
Appearance = System.Windows.Forms.Appearance.Button;
FlatStyle = System.Windows.Forms.FlatStyle.Flat;
TextAlign = ContentAlignment.MiddleRight;
FlatAppearance.BorderSize = 0;
AutoSize = false;
Height = 16;
}
protected override void OnPaint(PaintEventArgs pevent)
{
//base.OnPaint(pevent);
pevent.Graphics.Clear(BackColor);
using (SolidBrush brush = new SolidBrush(ForeColor))
pevent.Graphics.DrawString(Text, Font, brush, 27, 4);
Point pt = new Point( 4 , 4);
Rectangle rect = new Rectangle(pt, new Size(16, 16));
pevent.Graphics.FillRectangle(Brushes.Beige, rect);
if (Checked)
{
using (SolidBrush brush = new SolidBrush(ccol))
using (Font wing = new Font("Wingdings", 12f))
pevent.Graphics.DrawString("ü", wing, brush, 1,2);
}
pevent.Graphics.DrawRectangle(Pens.DarkSlateBlue, rect);
Rectangle fRect = ClientRectangle;
if (Focused)
{
fRect.Inflate(-1, -1);
using (Pen pen = new Pen(Brushes.Gray) { DashStyle = DashStyle.Dot })
pevent.Graphics.DrawRectangle(pen, fRect);
}
}
您可能需要調整控件和字體的大小。如果您想擴展代碼以支持TextAlign
和CheckAlign
屬性。
如果你需要一個三態控件,你可以調整代碼以顯示第三個狀態外觀,特別是如果你想到一個看起來比原來更好的。
你必須寫自己的復選框。 通過制作一個自定義控件,其中有一個藍色正方形(可能從Button繼承),通過OnClick事件在已檢查和未檢查的圖像之間切換,並在其旁邊放置一個標簽。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.