VB의 TextBox 텍스트 상자를 중앙에 수직으로 표시하는 방법

본고의 실례 코드는 VB의 TextBox 텍스트 상자를 가운데에 수직으로 표시하는 효과를 실현할 수 있다.참고 사항: Form_Load () 창 코드의 여러 줄 속성 설정은 Text1이어야 합니다.MultiLine = True, 이 속성은 읽기 전용 속성입니다. 디자인할 때 수정하십시오. 줄을 바꾸면 다음 코드에 의해 차단됩니다. 차단하지 않으려면 스스로 수정할 수 있습니다. 이 함수를 호출하면 됩니다.
구체적인 기능 코드는 다음과 같다.

'================================================================================
'|       | 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
  ' , 
  ' , 
  '===========================================
  ' 
  VerMiddleText Me, Text1
  Caption = Len(Text1)
End Sub

좋은 웹페이지 즐겨찾기