DevExpress开发项目中,GridView和bandedGridView的列标题经常需要显示一些带有下标或上标的文字,如下图所示:
如果上标是平方或是立方,可以通过输入法完成,平方(二次方)如下所示:
同样,立方(三次方)也是一样的方法,不在赘述。但对于下标和其他的一些上标,则可用如下方法进行:
1 // 表头设置 2 private void bandedGridView1_CustomDrawBandHeader(object sender, DevExpress.XtraGrid.Views.BandedGrid.BandHeaderCustomDrawEventArgs e) 3 { 4 DrawBandHeader("Test", null, "1", "gridBand1", sender, e); 5 DrawBandHeader("Test", "1", null, "gridBand2", sender, e); 6 DrawBandHeader("Test", "1", "1", "gridBand3", sender, e); 7 } 8 9 /// <summary> 10 /// 重绘Band名称 11 /// </summary> 12 /// <param name="strBase">底数</param> 13 /// <param name="subStr">下标</param> 14 /// <param name="supStr">上标</param> 15 /// <param name="bandName">GridBand名称</param> 16 /// <param name="sender"></param> 17 /// <param name="e"></param> 18 private void DrawBandHeader(string strBase, string subStr, string supStr, string bandName, object sender, DevExpress.XtraGrid.Views.BandedGrid.BandHeaderCustomDrawEventArgs e) 19 { 20 if (!e.Band.Name.Equals(bandName)) 21 return; 22 23 float startX = (e.Bounds.Left + e.Bounds.Right) / 2; 24 float startY = e.Bounds.Top; 25 e.Info.Caption = ""; 26 e.Painter.DrawObject(e.Info); 27 28 // 绘制底数 29 Font baseFont = new Font(e.Appearance.Font.OriginalFontName, e.Appearance.Font.Size); 30 e.Graphics.DrawString(strBase, baseFont, Brushes.Black, startX, startY); 31 32 // 求上标或下标起点的参考位置 33 startX += e.Graphics.MeasureString(strBase, baseFont).Width - 2; 34 35 // 上下标大小字体 36 Font scriptFont = new Font(e.Appearance.Font.OriginalFontName, 37 e.Appearance.Font.Size - 3); 38 // 绘制下标 39 if (subStr != null) 40 { 41 e.Graphics.DrawString(subStr, scriptFont, Brushes.Black, startX, startY + 9); 42 } 43 44 // 绘制上标 45 if (supStr != null) 46 { 47 e.Graphics.DrawString(supStr, scriptFont, Brushes.Black, startX, startY + 2); 48 } 49 50 e.Handled = true; 51 } 52 53 // 列标题设置 54 private void bandedGridView1_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e) 55 { 56 DrawColHeader("Game", null, "1", "bandedGridColumn1", sender, e); 57 DrawColHeader("Game", "1", null, "bandedGridColumn2", sender, e); 58 DrawColHeader("Game", "1", "1", "bandedGridColumn3", sender, e); 59 } 60 61 /// <summary> 62 /// 重绘列标题 63 /// </summary> 64 /// <param name="strBase">底数</param> 65 /// <param name="subStr">下标</param> 66 /// <param name="supStr">上标</param> 67 /// <param name="colName">列名</param> 68 /// <param name="sender"></param> 69 /// <param name="e"></param> 70 private void DrawColHeader(string strBase, string subStr, string supStr, string colName, object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e) 71 { 72 if (e.Column != null && !e.Column.Name.Equals(colName)) 73 return; 74 75 float startX = (e.Bounds.Left + e.Bounds.Right) / 2; 76 float startY = e.Bounds.Top; 77 e.Info.Caption = ""; 78 e.Painter.DrawObject(e.Info); 79 80 // 绘制底数 81 Font baseFont = new Font(e.Appearance.Font.OriginalFontName, e.Appearance.Font.Size); 82 e.Graphics.DrawString(strBase, baseFont, Brushes.Black, startX, startY); 83 84 // 求上标或下标起点的参考位置 85 startX += e.Graphics.MeasureString(strBase, baseFont).Width - 2; 86 87 // 上下标大小字体 88 Font scriptFont = new Font(e.Appearance.Font.OriginalFontName, 89 e.Appearance.Font.Size - 3); 90 // 绘制下标 91 if (subStr != null) 92 { 93 e.Graphics.DrawString(subStr, scriptFont, Brushes.Black, startX, startY + 9); 94 } 95 96 // 绘制上标 97 if (supStr != null) 98 { 99 e.Graphics.DrawString(supStr, scriptFont, Brushes.Black, startX, startY + 2); 100 } 101 102 e.Handled = true; 103 }
实现效果如下如所示,可根据需要替换上下标内容: