Der Beispielcode in diesem Artikel kann den Effekt einer vertikalen Zentrierung des VB TextBox-Textfelds erzielen. Beachten Sie hier: Die Einstellung des mehrzeiligen Attributs im Form_Load()-Formularcode muss wahr sein, d. h. Text1.MultiLine = True. Bitte ändern Sie es zur Entwurfszeit Durch nachfolgende Codes kann das Schild nicht selbst geändert werden. Rufen Sie einfach diese Funktion auf.
Die spezifischen Funktionscodes lauten wie folgt:
'============================================== ========================='| Das Textfeld wird in der Mitte angezeigt '==== ========================================== ===== ==========================Option ExplicitPrivate Typ RECT Left As Long Top As Long Right As Long Bottom As LongEnd TypePrivate Declare Function SendMessage Lib „user32“ Alias „SendMessageA“ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPrivate Declare Function SetWindowText Lib „user32 " Alias „SetWindowTextA“ (ByVal hwnd As Long, ByVal lpString As String) As LongPrivate Deklarieren Sie die Funktion 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) Als LongPrivate Deklarieren Sie die Funktion GetWindowLong Lib „user32“ Alias "GetWindowLongA " (ByVal hwnd As Long, ByVal nIndex As Long) As LongPrivate Declare Function SetWindowLong Lib „user32“ Alias „SetWindowLongA“ (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongPrivate Const EM_GETRECT = &HB2Private Const EM_SETRECTNP = &HB4Private Const GWL_WNDPROC = (-4) Privatkonst WM_CHAR = &H102Private Const WM_PASTE As Long = &H302Private prevWndProc As LongPublic ClipText As StringPublic Sub DisableAbility(TargetTextBox As TextBox) prevWndProc = GetWindowLong(TargetTextBox.hwnd, GWL_WNDPROC) SetWindowLong TargetTextBox.hwnd, GWL_WNDPROC, AddressOf WndProcEnd SubPrivate 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) Fall WM_PASTE ClipText = Clipboard.GetText Temp = Ersetzen(ClipText, Chr(10), "") Temp = Ersetzen(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 SelectEnd FunctionSub VerMiddleText(mForm As form, mText As TextBox) If mText.MultiLine = False Then Exit Sub Dim rc As RECT, tmpTop As Long, tmpBot As Lange SendMessage mText.hwnd, EM_GETRECT, 0, rc Mit mForm.Font .Name = mText.Font.Name .Size = mText.Font.Size .Bold = mText.Font.Bold End Mit 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 mTextEnd Sub'///////////////////////////////////////////// /// ///////'Das Folgende ist das Formular Code'//////////////////////////////////////////////// /// ///////Privat Sub Form_Load() '================= Achtung! ! ! ================= 'Das mehrzeilige Attribut muss wahr sein und Text1.MultiLine = True 'Dieses Attribut ist ein schreibgeschütztes Attribut, bitte ändern Sie es zur Entwurfszeit.' Codeblockierung. Wenn Sie ihn nicht blockieren möchten, können Sie ihn selbst ändern ============= == 'Rufen Sie einfach diese Funktion auf VerMiddleText Me, Text1 Caption = Len(Text1)End Sub