VB的TextBox文本框?qū)崿F(xiàn)垂直居中顯示的方法
本文實(shí)例代碼可以實(shí)現(xiàn)讓VB的TextBox文本框垂直居中顯示效果。此處需要注意:Form_Load()窗體代碼中的多行屬性設(shè)置必須為真,即Text1.MultiLine = True,該屬性為只讀屬性,請(qǐng)?jiān)谠O(shè)計(jì)時(shí)修改,換行會(huì)被之后的代碼屏蔽,不想屏蔽可自行修改,調(diào)用此函數(shù)就好了。
具體的功能代碼如下:
'================================================================================ '| 模 塊 名 | TextBoxMiddle '| 說 明 | 文本框居中顯示 '================================================================================= Option Explicit Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type 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 Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private Const EM_GETRECT = &HB2 Private Const EM_SETRECTNP = &HB4 Private Const GWL_WNDPROC = (-4) Private Const WM_CHAR = &H102 Private Const WM_PASTE As Long = &H302 Private prevWndProc As Long Public ClipText As String Public Sub DisableAbility(TargetTextBox As TextBox) prevWndProc = GetWindowLong(TargetTextBox.hwnd, GWL_WNDPROC) SetWindowLong TargetTextBox.hwnd, GWL_WNDPROC, AddressOf WndProc End Sub Private Function WndProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Dim Temp As String Select Case Msg Case WM_CHAR If wParam <> 13 Then WndProc = CallWindowProc(prevWndProc, hwnd, Msg, wParam, lParam) Case WM_PASTE ClipText = Clipboard.GetText Temp = Replace(ClipText, Chr(10), "") Temp = Replace(Temp, Chr(13), "") Clipboard.Clear Clipboard.SetText Temp WndProc = CallWindowProc(prevWndProc, hwnd, Msg, wParam, lParam) Clipboard.Clear Clipboard.SetText ClipText Case Else WndProc = CallWindowProc(prevWndProc, hwnd, Msg, wParam, lParam) End Select End Function Sub VerMiddleText(mForm As form, mText As TextBox) If mText.MultiLine = False Then Exit Sub Dim rc As RECT, tmpTop As Long, tmpBot As Long SendMessage mText.hwnd, EM_GETRECT, 0, rc With mForm.Font .Name = mText.Font.Name .Size = mText.Font.Size .Bold = mText.Font.Bold End With tmpTop = ((rc.Bottom - rc.Top) - _ (mText.Parent.TextHeight("H ") \ Screen.TwipsPerPixelY)) \ 2 + 2 tmpBot = ((rc.Bottom - rc.Top) + _ (mText.Parent.TextHeight("H ") \ Screen.TwipsPerPixelY)) \ 2 + 2 rc.Top = tmpTop rc.Bottom = tmpBot mText.Alignment = vbCenter SendMessage mText.hwnd, EM_SETRECTNP, 0&, rc mText.Refresh DisableAbility mText End Sub '/////////////////////////////////////////////////////// '以下為窗體代碼 '/////////////////////////////////////////////////////// Private Sub Form_Load() '================注意?。?!================= '多行屬性必須為真,暨Text1.MultiLine = True '該屬性為只讀屬性,請(qǐng)?jiān)谠O(shè)計(jì)時(shí)修改 '換行會(huì)被之后的代碼屏蔽,不想屏蔽可自行修改 '=========================================== '調(diào)用此函數(shù)就好了 VerMiddleText Me, Text1 Caption = Len(Text1) End Sub
- VB6實(shí)現(xiàn)連接Access數(shù)據(jù)庫的ADODB代碼實(shí)現(xiàn)方法
- VB的32位程序在64位系統(tǒng)中出現(xiàn)文件和注冊(cè)表自動(dòng)轉(zhuǎn)向的解決方法
- VB實(shí)現(xiàn)鼠標(biāo)繪圖實(shí)例代碼
- VB實(shí)現(xiàn)屏蔽文本框右鍵菜單的復(fù)制、粘貼等功能的方法
- VB使用XMLHTTP實(shí)現(xiàn)Post與Get的方法
- VB讀取線程、句柄及寫入內(nèi)存的API代碼實(shí)例
- VB實(shí)現(xiàn)的倒計(jì)時(shí)類代碼詳解
- VB調(diào)用Word拼寫檢查功能實(shí)例
- VB使用shell函數(shù)打開外部exe程序的實(shí)現(xiàn)方法
相關(guān)文章
VB使用shell函數(shù)打開外部exe程序的實(shí)現(xiàn)方法
這篇文章主要介紹了VB使用shell函數(shù)打開外部exe程序的實(shí)現(xiàn)方法,是非常實(shí)用的一個(gè)功能,需要的朋友可以參考下2014-07-07VB實(shí)現(xiàn)的倒計(jì)時(shí)類代碼詳解
這篇文章主要介紹了VB實(shí)現(xiàn)的倒計(jì)時(shí)類代碼,非常實(shí)用的功能,需要的朋友可以參考下2014-07-07VB使用XMLHTTP實(shí)現(xiàn)Post與Get的方法
這篇文章主要介紹了VB使用XMLHTTP實(shí)現(xiàn)Post與Get的方法,有一定的借鑒價(jià)值,需要的朋友可以參考下2014-07-07