Renaming Items a la Explorer ;)
Have you ever wondered how the Explorer.exe renaming the folder items when you perform it? Just single renaming so far. But it's powefull ;)
So be care of what you wish ;)
Never BEFORE shown in this forum a la Explorer.exe way to do it!!
Code:
Private Function pOnEndLabelEdit(lParam As LONG_PTR) As Long
Dim lpTVDI As tagTVDISPINFOEX
Dim hr As Long
Dim hWndEdit As Long
Dim sBuff As String
Dim pISI As IShellItem
Dim pISF As IShellFolder
Dim pIPAI As IParentAndItem
Dim pidl As LONG_PTR
Dim pidlOut As LONG_PTR
Dim lpPath As LONG_PTR
Dim lpTVIX As tagTVITEMEX
Dim lpTVHTI As tagTVHITTESTINFO
Dim lpCurPT As POINTAPI
MoveMemory lpTVDI, ByVal lParam, LenB(lpTVDI)
If lpTVDI.itemex.lParam > 0 Then
pidl = lpTVDI.itemex.lParam
Set pISI = m_cShell32.GetIShellItemFromPIDL(pidl)
If Not pISI Is Nothing Then
Set pIPAI = pISI
pIPAI.GetParentAndItem 0, pISF, 0
pISI.GetDisplayName SIGDN_DESKTOPABSOLUTEPARSING, lpPath
hWndEdit = pGetEditHandle
sBuff = String(MAX_PATH, Chr(0))
GetWindowText hWndEdit, StrPtr(sBuff), LenB(sBuff)
If MsgBox("The Item you now will rename will be renamed for all times! Do you wanna proceed?", vbExclamation Or vbYesNo, App.Title) = vbYes Then
hr = pISF.SetNameOf(m_hWndTV, pidl, StrPtr(sBuff), SHGDN_INFOLDER, pidlOut)
If hr = S_OK Then
SendMessage m_hWndTV, TVM_ENDEDITLABELNOW, ByVal 0, ByVal 0
GetCursorPos lpCurPT
lpTVIX.mask = TVIF_TEXT Or TVIF_PARAM Or TVIF_HANDLE
lpTVIX.lParam = pidl
lpTVIX.hTreeItem = lpTVDI.itemex.hTreeItem
If SendMessage(lpTVDI.hdr.hwndFrom, TVM_GETITEMW, 0, ByVal VarPtr(lpTVIX)) <> 0 Then
With lpTVIX
.mask = TVIF_TEXT
.pszText = StrPtr(sBuff)
End With
SendMessage lpTVDI.hdr.hwndFrom, TVM_SETITEMW, 0, ByVal VarPtr(lpTVIX)
UpdateWindow m_hWndTV
End If
Else
MsgBox "pISF.SetNameOf Failed! Error: #" & Hex(hr), vbExclamation
End If
End If
End If
End If
pOnEndLabelEdit = 0
End Function
* m_cShell32 is my cShell32 Class file.
Re: Renaming Items a la Explorer ;)
An little update - I had forgotten to set the new pidl reference for the renamed item.
Code:
Private Function pOnEndLabelEdit(lParam As LONG_PTR) As Long
Dim lpTVDI As tagTVDISPINFOEX
Dim hr As Long
Dim hWndEdit As Long
Dim sBuff As String
Dim pISI As IShellItem
Dim pISF As IShellFolder
Dim pIPAI As IParentAndItem
Dim pidl As LONG_PTR
Dim pidlOut As LONG_PTR
Dim lpPath As LONG_PTR
Dim lpTVIX As tagTVITEMEX
Dim lpTVHTI As tagTVHITTESTINFO
Dim lpCurPT As POINTAPI
MoveMemory lpTVDI, ByVal lParam, LenB(lpTVDI)
If lpTVDI.itemex.lParam > 0 Then
pidl = lpTVDI.itemex.lParam
Set pISI = m_cShell32.GetIShellItemFromPIDL(pidl)
If Not pISI Is Nothing Then
Set pIPAI = pISI
pIPAI.GetParentAndItem 0, pISF, 0
pISI.GetDisplayName SIGDN_DESKTOPABSOLUTEPARSING, lpPath
hWndEdit = pGetEditHandle
sBuff = String(MAX_PATH, Chr(0))
GetWindowText hWndEdit, StrPtr(sBuff), LenB(sBuff)
If MsgBox("The Item you now will rename will be renamed for all times! Do you wanna proceed?", vbExclamation Or vbYesNo, App.Title) = vbYes Then
hr = pISF.SetNameOf(m_hWndTV, pidl, StrPtr(sBuff), SHGDN_INFOLDER, pidlOut)
If hr = S_OK Then
SendMessage m_hWndTV, TVM_ENDEDITLABELNOW, ByVal 0, ByVal 0
GetCursorPos lpCurPT
lpTVIX.mask = TVIF_TEXT Or TVIF_PARAM Or TVIF_HANDLE
lpTVIX.lParam = pidl
lpTVIX.hTreeItem = lpTVDI.itemex.hTreeItem
If SendMessage(lpTVDI.hdr.hwndFrom, TVM_GETITEMW, 0, ByVal VarPtr(lpTVIX)) <> 0 Then
With lpTVIX
.mask = TVIF_TEXT
.pszText = StrPtr(sBuff)
.lParam = pidlOut '<--- Here is the little change I made...or else it keeps the invalid old pidl for the old name.
End With
SendMessage lpTVDI.hdr.hwndFrom, TVM_SETITEMW, 0, ByVal VarPtr(lpTVIX)
UpdateWindow m_hWndTV
End If
Else
MsgBox "pISF.SetNameOf Failed! Error: #" & Hex(hr), vbExclamation
End If
End If
End If
End If
pOnEndLabelEdit = 0
End Function
Re: Renaming Items a la Explorer ;)
The great thing about IShellFolder.SetNameOf, it's the easiest way to rename virtual objects and drives. Other methods either don't work or are much more complex and object-specific.
Re: Renaming Items a la Explorer ;)
Yes, I fully agree with you about that.