Date: 02-16-2022
Return to Index
created by gbSnippets
'Rather than using drag/drop of a node to a new location, this code uses
'the familiar cut/copy/paste, including paste as child or paste as sibling.
'The built-in PowerBASIC TreeView statements are the primary code used to
'create the Cut/Copy/Paste features.
'Compiler Comments:
'This code was written to compilete in PBWin10. To compile with PBWin9, replace VatPtr(tvht.pt)
'with just tvht.pt where it is used in the MapWindowPoints() API (one location).
'Primary Code:
'Because of length, the Cut/Copy/PasteAsChild/PasteAsSibling/Delete code
'is only shown once at the bottom of the Compilable Example listing.
'Compilable Example: (Jose Includes)
'This example allows cut/copy/paste of child nodes only - implemented as a context menu
'It does not allows cut/copy/paste of parent nodes
'Although a normal menu bar could be used to accomplish these features,
'a context menu (right mouse click) is used in this example, using the
'%WM_ContextMenu and TrackPopupMenu API to display a TreeView popup menu.
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "commctrl.inc"
Global hDlg As Dword, hContext As Dword, OrigTextBoxProc&
Global NodeCopy$, NodeUser&
%IDC_TreeView = 400
%ID_Cut = 500 : %ID_Copy = 502
%ID_PasteC = 503 : %ID_PasteS = 504 : %ID_Delete = 505
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Local hItem As Dword, hTemp As Dword, hTemp2 As Dword, hTemp3 As Dword
Dialog New Pixels, 0, "TreeView",200,200,155,250, %WS_SysMenu, 0 To hDlg
Control Add TreeView, hDlg, %IDC_TreeView, "", 10,10,130,200
TreeView Insert Item hDlg, %IDC_TreeView, 0, %TVI_Last, 0,0,"Root" To hItem
TreeView Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 0,0,"Mother" To hTemp
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Dan" To hTemp2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Bob" To hTemp3
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp, %True
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_Last, 0,0,"Foot" To hTemp2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_Last, 0,0,"Arm" To hTemp2
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp3, %True
TreeView Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 0,0,"Father" To hTemp
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Helen" To hTemp2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Any" To hTemp3
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp, %True
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_Last, 0,0,"Leg" To hTemp2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_Last, 0,0,"Finger" To hTemp2
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp3, %True
TreeView Set Expanded hDlg, %IDC_TreeView, hItem, %True
AddContextMenu
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local x As Long, y As Long, iReturn As Long
Select Case CB.Msg
Case %WM_ContextMenu
x = Lo(Integer,CB.lParam) : y = Hi(Integer, CB.lParam)
iReturn = GetDlgCtrlID (CB.wParam) 'get control ID
If iReturn = %IDC_TreeView Then
'select the item under the mouse when a right-click is made
Local TVht As TV_HitTestInfo
TVht.pt.x = x
TVht.pt.y = y
MapWindowPoints(%HWND_Desktop, CB.wParam, ByVal VarPtr(Tvht.pt), 1)
SendMessage(CB.wParam, %TVM_HITTEST, 0, ByVal VarPTR(TVht))
TreeView Select hDlg, %IDC_TreeView, TVht.hItem
TrackPopupMenu hContext, %TPM_LEFTALIGN, x, y, 0, hDlg, ByVal 0
End If
Case %WM_Command
Select Case CB.Ctl
Case %ID_Cut : TreeCut
Case %ID_Copy : TreeCopy
Case %ID_PasteC : TreePasteAsChild
Case %ID_PasteS : TreePasteAsSibling
Case %ID_Delete : TreeDelete
End Select
End Select
End Function
Sub AddContextMenu
Menu New Popup To hContext
Menu Add String, hContext, "Cut", %ID_Cut, %MF_Enabled
Menu Add String, hContext, "Copy", %ID_Copy, %MF_Enabled
Menu Add String, hContext, "Paste As Child", %ID_PasteC, %MF_Enabled
Menu Add String, hContext, "Paste As Sibling", %ID_PasteS, %MF_Enabled
Menu Add String, hContext, "Delete", %ID_Delete, %MF_Enabled
End Sub
Sub TreeDelete
Local hNode as Dword, iReturn&
TreeView Get Select hDlg, %IDC_TreeView to hNode
TreeView Get Child hDlg, %IDC_TreeView, hNode to iReturn&
If iReturn& Then
'is parent
Beep
Else
'is child
TreeView Delete hDlg, %IDC_TreeView, hNode
End If
End Sub
Sub TreeCut
Local hNode as Dword, iReturn&
TreeView Get Select hDlg, %IDC_TreeView to hNode
TreeView Get Child hDlg, %IDC_TreeView, hNode to iReturn&
If iReturn& Then
'is parent
Beep
Else
'is child
TreeView Get Text hDlg, %IDC_TreeView, hNode To NodeCopy$
TreeView Get User hDlg, %IDC_TreeView, hNode To NodeUser&
TreeView Delete hDlg, %IDC_TreeView, hNode
End If
End Sub
Sub TreeCopy
Local hNode as Dword, iReturn&
TreeView Get Select hDlg, %IDC_TreeView to hNode
TreeView Get Child hDlg, %IDC_TreeView, hNode to iReturn&
If iReturn& Then
'is parent
Beep
Else
'is child
TreeView Get Text hDlg, %IDC_TreeView, hNode To NodeCopy$
TreeView Get User hDlg, %IDC_TreeView, hNode To NodeUser&
End If
End Sub
Sub TreePasteAsChild
Local hNode as Dword, iReturn&
TreeView Get Select hDlg, %IDC_TreeView to hNode
TreeView Insert Item hDlg, %IDC_TreeView, hNode, %TVI_First, 0, 0, NodeCopy$ to hNode
TreeView Set User hDlg, %IDC_TreeView, hNode, NodeUser&
End Sub
Sub TreePasteAsSibling
Local hNode as Dword, iReturn&, hParent as Dword
TreeView Get Select hDlg, %IDC_TreeView to hNode
TreeView Get Parent hDlg, %IDC_TreeView, hNode To hParent
TreeView Insert Item hDlg, %IDC_TreeView, hParent, hNode, 0, 0, NodeCopy$ to hNode
TreeView Set User hDlg, %IDC_TreeView, hNode, NodeUser&
End Sub
'gbs_00263
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm