貼個代碼,大家一起分享一下
protected override void OnEnabledChanged(EventArgs e)
{
if (Enabled==false){
SetStyle(ControlStyles.UserPaint,true);
}
else{
SetStyle(ControlStyles.UserPaint,false);
}
base.OnEnabledChanged(e);
}
//重寫OnPaint
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
if (Enabled==false){
pe.Graphics.FillRectangle(new SolidBrush(SystemColors.ControlLight),
pe.ClipRectangle);
//文字描畫
int x=0,y=0;
Size s = pe.Graphics.MeasureString(Text,Font).ToSize();
x=Width-s.Width;
y=(Height-s.Height)/2;
pe.Graphics.DrawString(this.Text, this.Font, Brushes.Black , x, y);
}
}