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 GetClientRect Lib "user32" (ByVal hWnd As Long, _ lpRect As RECT) As Long Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type
Private Const TV_FIRST = &H1100 Private Const TVM_GETITEMRECT = (TV_FIRST + 4) Private Const TVM_GETNEXTITEM = (TV_FIRST + 10) Private Const TVGN_CARET = 9
Dim WithEvents TreeView As TreeView
Dim WithEvents TextBox As TextBox
Dim Parent As Object
Dim saveText As String
Dim defaultCtrl As Object
Dim cancelCtrl As Object
Sub Init(TView As TreeView, TBox As TextBox) Set TreeView = TView Set TextBox = TBox Set Parent = TextBox.Parent TextBox.Visible = False End Sub
Private Sub TreeView_BeforeLabelEdit(Cancel As Integer) Cancel = True StartLabelEdit End Sub
Private Sub TextBox_Change() Dim saveFont As StdFont Dim wi As Single Dim borderWidth As Single Set saveFont = Parent.Font Set Parent.Font = TextBox.Font wi = Parent.TextWidth(TextBox.Text) + Parent.ScaleX(20, vbPixels, _ Parent.ScaleMode) Set Parent.Font = saveFont borderWidth = Parent.ScaleX(2, vbPixels, Parent.ScaleMode) If TextBox.Left + wi > TreeView.Left + TreeView.Width - borderWidth Then wi = TreeView.Left + TreeView.Width - TextBox.Left - borderWidth End If TextBox.Width = wi End Sub
Private Sub TextBox_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case 13 EndLabelEdit True KeyAscii = 0 Case 27 EndLabelEdit False KeyAscii = 0 End Select End Sub
Private Sub TextBox_MouseDown(Button As Integer, Shift As Integer, X As Single, _ Y As Single) If X < 0 Or Y < 0 Or X > TextBox.Width Or Y > TextBox.Height Then EndLabelEdit True End If End Sub
Private Sub StartLabelEdit() Dim lpRect As RECT, lpClientRect As RECT Dim hNode As Long hNode = SendMessage(TreeView.hWnd, TVM_GETNEXTITEM, TVGN_CARET, ByVal 0&) lpRect.Left = hNode If SendMessage(TreeView.hWnd, TVM_GETITEMRECT, True, lpRect) = 0 Then Exit Sub End If
With lpRect .Left = TreeView.Left + Parent.ScaleX(.Left, vbPixels, Parent.ScaleMode) .Top = TreeView.Top + Parent.ScaleY(.Top, vbPixels, Parent.ScaleMode) .Right = TreeView.Left + Parent.ScaleX(.Right, vbPixels, _ Parent.ScaleMode) .Bottom = TreeView.Top + Parent.ScaleY(.Bottom, vbPixels, _ Parent.ScaleMode) End With With TextBox .Move lpRect.Left, lpRect.Top, lpRect.Right - lpRect.Left + 200, _ lpRect.Bottom - lpRect.Top .ZOrder .Text = TreeView.SelectedItem.Text .SelStart = 0 .SelLength = Len(.Text) Set .Font = TreeView.Font .Visible = True .SetFocus SetCapture .hWnd Set defaultCtrl = Nothing Set cancelCtrl = Nothing Dim ctrl As Control On Error Resume Next For Each ctrl In Parent.Controls If ctrl.Default = False Then Else Set defaultCtrl = ctrl ctrl.Default = False End If If ctrl.Cancel = False Then Else Set cancelCtrl = ctrl ctrl.Cancel = False End If Next saveText = TreeView.SelectedItem.Text TreeView.SelectedItem.Text = "" End With
End Sub
Sub EndLabelEdit(AcceptIt As Boolean) If |