Bonjour,
J'ai développer un petit code pour ajuster le "Width" d'un treeview quand on clique sur un nœud.
Pour pouvoir voir en entier le texte du nœud si le treeview n'est pas assez large et inversement si on ferme des nœuds le "Width" diminue.

Je détecte si il y a une barre de défilement horizontale si oui on entre dans une boucle et on augmente ou on diminue la largeur.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer) As Integer
    Public Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Integer) As Integer
    Public Const GWL_STYLE As Integer = (-16)
    Public Const WS_VSCROLL As Integer = &H200000
    Public Const WS_HSCROLL As Integer = &H100000
 
..........
 
    Private Sub TreeView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.Click
        Dim bVScrollBar As Boolean
        Dim x As Integer
        x = 0
 
        For i = 1 To 200
            bVScrollBar = ((GetWindowLong(Me.TreeView1.Handle, GWL_STYLE) And WS_HSCROLL) = WS_HSCROLL)
            If bVScrollBar = True Then
                If x < 0 Then Exit Sub
                x = x + 1
                TreeView1.Width = TreeView1.Width + 10
            End If
            If bVScrollBar = False Then
                If x > 0 Then Exit Sub
                x = x - 1
                TreeView1.Width = TreeView1.Width - 10
            End If
        Next
    End Sub
Je ne trouve pas ça très "Clean"
Je ne peu pas croire qu'il n'existe pas un fonction plus "clean" qui ferait la même chose.

Qu'est-ce ce que vous en pensé.