L'exemple de code de cet article peut avoir pour effet de centrer verticalement la zone de texte VB TextBox. Notez ici : le paramètre d'attribut multiligne dans le code du formulaire Form_Load() doit être vrai, c'est-à-dire Text1.MultiLine = True. Cet attribut est un attribut en lecture seule. Veuillez le modifier au moment de la conception. par les codes suivants. Je ne veux pas. Le bouclier peut être modifié par vous-même, appelez simplement cette fonction.
Les codes de fonction spécifiques sont les suivants :
'================================================== = ============================='| Nom du module| TextBoxMiddle'| '==== ============================================== ===== ============================Option ExplicitPrivate Type RECT à gauche aussi long en haut Aussi long à droite Aussi long en bas que LongEnd TypePrivate Déclarer la fonction SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPrivate Déclarer la fonction SetWindowText Lib "user32 " Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Fonction de déclaration LongPrivate 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 LongPrivate Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA " (ByVal hwnd As Long, ByVal nIndex As Long) As LongPrivate Déclarer la fonction 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) Const privée 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) Cas WM_PASTE ClipText = Clipboard.GetText Temp = Remplacer (ClipText, Chr (10), "") Temp = Remplacer (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) Si mText.MultiLine = False Alors quittez Sub Dim rc As RECT, tmpTop As Long, tmpBot As Long SendMessage mText.hwnd, EM_GETRECT, 0, rc Avec mForm.Font .Name = mText.Font.Name .Size = mText.Font.Size .Bold = mText.Font.Bold Fin avec 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 Sous'//////////////////////////////////////////////// /// ///////'Ce qui suit est le formulaire Code'//////////////////////////////////////////////// /// ///////Privé Sub Form_Load() '================Attention ! ! ! ================= 'L'attribut multiligne doit être vrai et Text1.MultiLine = True 'Cet attribut est un attribut en lecture seule, veuillez le modifier au moment de la conception' Blocage du code, si vous ne souhaitez pas le bloquer, vous pouvez le modifier vous-même'================================ ============= == 'Appelez simplement cette fonction VerMiddleText Me, Text1 Caption = Len(Text1)End Sub