转自:http://www.microsoft.com/china/community/Column/18.mspx
韩睿
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim perWidth As Integer ‘一个英文字符占据的宽度
Dim count As Integer ‘标题栏所能容纳的字符数
Dim len As Integer‘已有标题的字符数
Dim i As Integer
With Graphics.FromHwnd(DataGrid1.Handle).MeasureString
(Text:="ABCDEFGHIJKLMNOPQRSTUVWXYZ", Font:=DataGrid1.Font)perWidth = CInt(.Width / 26.0!)
End With
count = DataGrid1.Width / perWidth
len = System.Text.Encoding.Default.GetByteCount(DataGrid1.CaptionText)
For i = 1 To (count - len) / 2
DataGrid1.CaptionText = " " + DataGrid1.CaptionText
Next
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
LabCaption.BackColor = Color.Transparent ‘设为透明色
LabCaption.AutoSize = True ‘设为可根据文本内容自动调整文本框大小
LabCaption.BorderStyle = BorderStyle.None ‘设为无边框
LabCaption.ForeColor = Color.White ‘前景色设为白色,就是文字是白色字
DataGrid1.Controls.Add(LabCaption) ‘将文本框嵌入Datagrid中去
LabCaption.Top = 5‘设置高度,0指的是datagrid的头部位置
LabCaption.Left = (DataGrid1.Width - LabCaption.Width) / 2 ‘设置居中
End Sub