转自:http://www.microsoft.com/china/community/Column/18.mspx

Henry手记—从Datagrid的标题居中说起
韩睿
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

图1 第一种方案:加空格
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

图2 第二种方案:嵌入文本框
posted on 2010-08-31 13:58  TsingCai  阅读(432)  评论(0编辑  收藏  举报