DataGridView重绘单元格中某些字体颜色,大小(转)

原文:http://blog.csdn.net/ly210501076/article/details/6580171

主要通过对单元格进行重绘,改变元单元格字体大小和颜色

 1 protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
 2         {
 3             base.OnCellPainting(e);
 4             if (e.Value != null)
 5             {
 6                 string cellWord = e.Value.ToString();//单元格原本内容
 7                 string keyWord = e.Value.ToString();//要改变的单元格关键字内容
 8 
 9                 Rectangle cellRect = e.CellBounds;//默认单元格
10                 Rectangle keyRect = e.CellBounds;//单元格内容区域,默认定义为单元格大小
11                 float fontSizeWeight = 96 / (72 / e.CellStyle.Font.Size); // 字体实际像素宽度
12                 float fontSizeHeight = 96 / (72 / e.CellStyle.Font.Size); // 字体实际像素高度
13                 //关键字的坐标
14                 keyRect.X += cellWord.Substring(0, cellWord.IndexOf(keyWord)).Length * (int)(fontSizeWeight / 2);
15                 keyRect.Y += (e.CellBounds.Height - (int)fontSizeHeight) / 2;
16                 //原文本的Y坐标
17                 cellRect.Y = keyRect.Y;
18 
19                 using (Brush foreColor = new SolidBrush(e.CellStyle.ForeColor), fontColor = new SolidBrush(this.FontColor))
20                 {
21                     //绘制背景色
22                     e.PaintBackground(e.ClipBounds, false);
23                     //绘制背景色(被选中状态下)
24                     if (e.State == (DataGridViewElementStates.Displayed | DataGridViewElementStates.Selected | DataGridViewElementStates.Visible))
25                         e.PaintBackground(e.ClipBounds, true);
26                     //分别绘制原文本和现在改变颜色的文本
27                     e.Graphics.DrawString(cellWord, this.Font, foreColor, cellRect, StringFormat.GenericDefault);
28                     e.Graphics.DrawString(keyWord, this.Font, fontColor, keyRect, StringFormat.GenericDefault);
29                     //提交事务
30                     e.Handled = true;
31                 }
32             }
33         }

 

定义该重绘控件的字体,前景色和背景图片

 1  #region properties
 2         /// <summary>
 3         /// 获取或设置该控件下显示字体的大小.
 4         /// </summary>
 5         [Browsable(true)]
 6         [DefaultValue(typeof(Font), "宋体,9"), Description("获取或设置该控件下显示字体的大小.")]
 7         public override Font Font
 8         {
 9             get { return this._font; }
10             set { this._font = value; }
11         }
12         /// <summary>
13         /// 获取或设置当前字体颜色.
14         /// </summary>
15         [Description("获取或设置当前字体颜色.")]
16         public Color FontColor
17         {
18             get { return this._fontColor; }
19             set { this._fontColor = value; }
20         }
21         /// <summary>
22         /// 获取或设置当前控件的背景图片.
23         /// </summary>
24         [DefaultValue(typeof(Image), ""), Description("获取或设置当前控件的背景图片.")]
25         public Image BackImage
26         {
27             get { return this._backImage; }
28             set { this._backImage = value; }
29         }
30         #endregion

 

以上代码仅供参考

posted @ 2013-01-06 11:14  KT  阅读(888)  评论(0编辑  收藏  举报