在頁面控件TextBox禁用的情況下將文本字體顏色變爲黑色

 貼個代碼,大家一起分享一下

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);  
  }  
   
  }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章