在Vb中如text和label控制元件是沒有垂直居中功能的,但是可以變通就是給文字加空行。 "加一個 Text1 一個 Command1 : Dim aa As String, i As Integer, jj As Integer, maxline As Integer, LineCnt As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Const EM_GETLINECOUNT = &HBA Private Sub Form_Load() "窗體與Command1居中
Me.Move
(
Screen.Width
- Me.Width) \ 2, (
Screen.Height
- Me.Height) \ 2
Command1.Move
(
Me.Width
- Command1.Width) \ 2,
Text1.Height
+
Text1.Top
+ 200
Command1.Caption
= "居中顯示"
Text1.Alignment
= 2 "TextBox 水平居中顯示 aa = "Attn:樓主" aa = aa & Chr(13) & Chr(10) aa = aa & "呵呵你的問題都好奇怪,但也讓我學了不少!" aa = aa & Chr(13) & Chr(10) aa = aa & "我也想不出其它的好方法" aa = aa & Chr(13) & Chr(10) aa = aa & "你認為這樣行嗎?"
Text1.Text
= aa End Sub Private Sub Command1_Click() LineCnt = TextBoxLineCnt(Text1) "每增加一行加上180,當然是以預設的字型大小而言 maxline =
Text1.Height
\ 180 "計算textbox能裝下幾行 jj = (maxline - LineCnt) \ 2 "垂直居中,(textbox總行數-Text1的總行數)整除2 For i = 1 To jj "裝上幾行空白
Text1.Text
= Chr(13) & Chr(10) &
Text1.Text
Next i MsgBox "總共有" & CStr(LineCnt) & "行文字,看看垂直居中了嗎?" End Sub Public Function TextBoxLineCnt(ctl As TextBox) As Long "計算總行數的函式 TextBoxLineCnt = SendMessage(ctl.hwnd, EM_GETLINECOUNT, 0, 0) End Function
在Vb中如text和label控制元件是沒有垂直居中功能的,但是可以變通就是給文字加空行。 "加一個 Text1 一個 Command1 : Dim aa As String, i As Integer, jj As Integer, maxline As Integer, LineCnt As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Const EM_GETLINECOUNT = &HBA Private Sub Form_Load() "窗體與Command1居中
Me.Move
(Screen.Width
- Me.Width) \ 2, (Screen.Height
- Me.Height) \ 2Command1.Move
(Me.Width
- Command1.Width) \ 2,Text1.Height
+Text1.Top
+ 200Command1.Caption
= "居中顯示"Text1.Alignment
= 2 "TextBox 水平居中顯示 aa = "Attn:樓主" aa = aa & Chr(13) & Chr(10) aa = aa & "呵呵你的問題都好奇怪,但也讓我學了不少!" aa = aa & Chr(13) & Chr(10) aa = aa & "我也想不出其它的好方法" aa = aa & Chr(13) & Chr(10) aa = aa & "你認為這樣行嗎?"Text1.Text
= aa End Sub Private Sub Command1_Click() LineCnt = TextBoxLineCnt(Text1) "每增加一行加上180,當然是以預設的字型大小而言 maxline =Text1.Height
\ 180 "計算textbox能裝下幾行 jj = (maxline - LineCnt) \ 2 "垂直居中,(textbox總行數-Text1的總行數)整除2 For i = 1 To jj "裝上幾行空白Text1.Text
= Chr(13) & Chr(10) &Text1.Text
Next i MsgBox "總共有" & CStr(LineCnt) & "行文字,看看垂直居中了嗎?" End Sub Public Function TextBoxLineCnt(ctl As TextBox) As Long "計算總行數的函式 TextBoxLineCnt = SendMessage(ctl.hwnd, EM_GETLINECOUNT, 0, 0) End Function