I want To know how To retrieve:
- the current selected item
- edit the current selected item
- Swap two items of a treeview
- drag item of a treeview
heare is an example
tvex.bi
Code: Select all
FUNCTION MainWnd_InitDialog(BYVAL hDlg AS HWND) AS LRESULT
'ghImgList=ImageList_Create(ghInst,16,16,ILC_COLOR8,10,10) ' create imagelist
' ImageList_Add(ghImgList,bmpAPI16) ' add icons
' ImageList_Add(ghImgList,bmpTvwOpen16)
' TreeView_SetImageList(GetDlgItem(hDlg,trvMain),ghImgList,TVSIL_NORMAL) ' attach image list to treeview control
Return 0
END FUNCTION
FUNCTION MainWnd_trvMain_NmClick(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
' TreeView_EditLabel(GetDlgItem(hDlg,trvMain),hitem)
messagebox hDlg,"","ok",0
Return 0
END FUNCTION
FUNCTION MainWnd_trvMain_NmDblclk(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
Return 0
END FUNCTION
FUNCTION MainWnd_trvMain_NmSetfocus(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
Return 0
END FUNCTION
FUNCTION MainWnd_trvMain_TvnSelchanged(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
Return 0
END FUNCTION
FUNCTION MainWnd_trvMain_TvnSelchanging(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
Return 0
END FUNCTION
FUNCTION MainWnd_spnIconID_UdnDeltapos(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
Return 0
END FUNCTION
FUNCTION MainWnd_txtIconID_EnChange(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
Return 0
END FUNCTION
FUNCTION MainWnd_txtIconID_EnUpdate(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
Return 0
END FUNCTION
FUNCTION MainWnd_cmdAddItem_BnClicked(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
Dim tInsert AS TV_INSERTSTRUCT ' structure needed to insert a new item
Dim tItem AS TV_ITEM ' treeview item
Dim As HWND hTrv=GetDlgItem(hDlg,trvMain)
Dim As HTREEITEM hti=TreeView_GetSelection(hTrv) ' get current selection = parent item
IF hti THEN ' make current item parent item
' the mask item specifies which properties / members in the UDT should be set
tItem.mask=TVIF_CHILDREN OR TVIF_HANDLE ' only set cchildren member
tItem.hItem=hti
tItem.cchildren=1 ' means that item will contain child items
TreeView_SetItem(hTrv,@tItem)
END IF
tInsert.hParent=hti ' fill "insert item" structure
tInsert.hInsertAfter=TVI_SORT
' the "item" member is the item which should be inserted (type = TV_ITEM)
tInsert.Item.mask=TVIF_CHILDREN OR TVIF_HANDLE OR TVIF_IMAGE OR _
TVIF_SELECTEDIMAGE OR TVIF_STATE OR TVIF_TEXT
Dim As ZString*255 a: getwindowtext(GetDlgItem(hDlg,txtItemText),StrPtr(a),255)
tInsert.Item.pszText=STRPTR(a) ' item text
tInsert.Item.cchTextMax=LEN(a)
Dim As ZString*255 b : getwindowtext(GetDlgItem(hDlg,txtIconID),StrPtr(b),255)
Dim As Integer im=VAL(b)
tInsert.Item.iImage=im ' image ID
tInsert.Item.iSelectedImage=im ' selected image = normal image
tInsert.Item.cChildren=0 ' new item currently does not have children
Dim As HTREEITEM h=TreeView_InsertItem(hTrv,@tInsert) ' insert item
Return TreeView_EnsureVisible(hTrv,h) ' open tree - make new item visible
END FUNCTION
FUNCTION MainWnd_cmdQuit_BnClicked(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
endFlag=-1
Return 0
END FUNCTION
'---------------------
' TreeView
'---------------------
' Ajoute un item à la TreeView
Function AddTreeViewItem(hTree As HWND,hParent As HTREEITEM ,Text As ZString Ptr,iImage As Integer)As HTREEITEM
Dim As TV_INSERTSTRUCT tvinsert
tvinsert.hParent=hParent
tvinsert.hInsertAfter=TVI_ROOT
tvinsert.item.mask=TVIF_TEXT Or TVIF_IMAGE Or TVIF_STATE Or TVIF_SELECTEDIMAGE
tvinsert.item.state = TVIS_SELECTED
tvinsert.item.pszText=Text
tvinsert.item.cchTextMax= Len(*Text)
tvinsert.item.iImage=tvinsert.item.iSelectedImage=iImage
return cast(HTREEITEM,SendMessage(hTree,TVM_INSERTITEM,0,cast(LPARAM,@tvinsert)))
End Function
' Récupère le type de l'item hTreeSel en retournant l'id de l'îcone
Function GetTreeViewItemType(hTree As HWND,hTreeSel As HTREEITEM )As Integer
Dim As TV_ITEM tvi
tvi.mask=TVIF_IMAGE
tvi.hItem=hTreeSel
SendMessage(hTree,TVM_GETITEM,0,cast(LPARAM,@tvi))
return tvi.iImage
End Function
'/ Récupère le texte de l'item hTreeSel
Sub GetTreeViewItemText(hTree As HWND,hTreeSel As HTREEITEM,lpResult As ZString Ptr,nMaxCount As integer)
Dim As TV_ITEM tvi
tvi.mask=TVIF_TEXT
tvi.pszText=lpResult
tvi.cchTextMax=nMaxCount
tvi.hItem=hTreeSel
SendMessage(hTree,TVM_GETITEM,0,cast(LPARAM,@tvi))
End Sub
' Modifie le texte de l'item hTreeSel
Sub SetTreeViewItemText(hTree As HWND,hTreeSel As HTREEITEM,Text As ZString Ptr)
Dim As TV_ITEM tvi
tvi.mask=TVIF_TEXT
tvi.pszText=Text
tvi.cchTextMax=sizeof(Text)
tvi.hItem=hTreeSel
SendMessage(hTree,TVM_SETITEM,0,cast(LPARAM,@tvi))
End Sub
Code: Select all
' Treeview Sample Code by Daniel Modler with powerbasic
' translatted to freebasic by Aloberr
#INCLUDE once "WINdows.bi"
#Include Once "win/COMMCTRL.bi"
#Define dlgMain 101
#Define bmpAPI16 159
#Define bmpTvwOpen16 162
#Define trvMain 1000
#Define txtIconID 1001
#Define spnIconID 1003
#Define txtItemText 1004
#Define cmdAddItem 1005
#Define cmdQuit 1006
'=========== [ Program GLOBAL declarations ] ========================
Dim Shared ghInst AS HINSTANCE ' Module instance handle.
Dim Shared ghCurrentDlg AS HWND ' Active modeless dlg handle.
Dim Shared ghImgList As HIMAGELIST
Dim Shared endFlag As Long
#Include Once "TVWEXAMP.bi" ' Event handling functions module.
Declare FUNCTION MainWndProc(BYVAL hDlg AS HWND,_
BYVAL wMsg AS UINT,_
BYVAL wParam AS WPARAM,_
BYVAL lParam AS LPARAM) AS LRESULT
FUNCTION WinMain(BYVAL hCurrInstance AS HINSTANCE,_
BYVAL hPrevInstance AS HINSTANCE,_
lpCmdLine AS ZString PTR,_
BYVAL nCmdShow AS LONG) AS LONG
Dim Msg AS MSG
Dim wClass AS WndClass
Dim hWnd AS HWND
Dim szClassName AS ZString * 11
szClassName = "TrvExample"
ghInst = hCurrInstance
' A REPLACEMENT for program PrevInstance.
IF 0=(FindWindow(szClassName, BYVAL NULL)) THEN
' Fill window structure.
wClass.style = CS_HREDRAW OR CS_VREDRAW
wClass.lpfnWndProc = Cast(WNDPROC,GetProcAddress(GetModuleHandle("USER32"), "DefDlgProcA"))
wClass.cbClsExtra = 0
wClass.cbWndExtra = DLGWINDOWEXTRA
wClass.hInstance = hCurrInstance
wClass.hIcon = LoadIcon(NULL, BYVAL IDI_APPLICATION)
wClass.hCursor = LoadCursor(NULL, BYVAL IDC_ARROW)
wClass.hbrBackground = GetStockObject(LTGRAY_BRUSH)
wClass.lpszMenuName = NULL
wClass.lpszClassName = StrPtr(szClassName)
' Register the window-class.
RegisterClass @wClass
ELSE
' Bring prev instance to front.
hWnd = FindWindow(szClassName, BYVAL NULL)
IF IsIconic(hWnd) THEN ShowWindow hWnd ,SW_RESTORE
SetForegroundWindow hWnd
' Exit.
FUNCTION = 0
EXIT FUNCTION
END IF
' Create the main window as a MODELESS dialog.
hWnd = CreateDialogParam(hCurrInstance, cast( LPCTSTR,dlgMain), 0, @MainWndProc, 0)
ShowWindow hWnd, nCmdShow
' Main message loop of program.
WHILE GetMessage(@Msg, NULL, 0, 0) AND (0=endFlag)
IF 0=(ghCurrentDlg) OR 0=(IsDialogMessage(ghCurrentDlg, @Msg)) THEN
TranslateMessage @Msg
DispatchMessage @Msg
END IF
WEND
' Clean up and assign return value.
FUNCTION = Msg.wParam
END FUNCTION
End WinMain (getmoduleHandle(0),0,Command,SW_show)
'=============== [ MainWnd dialog window procedure ] =================
FUNCTION MainWndProc(BYVAL hDlg AS HWND,_
BYVAL wMsg AS UINT,_
BYVAL wParam AS WPARAM,_
BYVAL lParam AS LPARAM) AS LRESULT
Dim Action As LONG
SELECT CASE wMsg
CASE WM_INITDIALOG
' Init and create common controls.
InitCommonControls
MainWnd_InitDialog(hDlg)
' Let dialog-box engine set the focus.
Action = TRUE
CASE WM_ACTIVATE
If LoWord(wParam) = 0 THEN ' Dialog becoming inactive.
ghCurrentDlg = 0
Else ' Dialog becoming active.
ghCurrentDlg = hDlg
End IF
CASE WM_DESTROY
' Send a message to terminate message loop.
PostQuitMessage 0
Action = TRUE
CASE WM_NOTIFY
Dim lpNmh AS NMHDR PTR
lpNmh = Cast(NMHDR Ptr,lParam)
' Examine .Code member.
Select CASE lpNmh->Code
Case NM_CLICK
Select CASE lpNmh->idFrom
Case trvMain
Action = MainWnd_trvMain_NmClick(hDlg, wParam, lParam)
End SELECT
Case NM_DBLCLK
Select CASE lpNmh->idFrom
Case trvMain
Action = MainWnd_trvMain_NmDblclk(hDlg, wParam, lParam)
End SELECT
Case NM_SETFOCUS
Select CASE lpNmh->idFrom
Case trvMain
Action = MainWnd_trvMain_NmSetfocus(hDlg, wParam, lParam)
End SELECT
' Tree view notifications.
Case TVN_LAST TO TVN_FIRST
Select CASE lpNmh->idFrom
Case trvMain
Select CASE lpNmh->Code
Case TVN_SELCHANGED
Action = MainWnd_trvMain_TvnSelchanged(hDlg, wParam, lParam)
Case TVN_SELCHANGING
Action = MainWnd_trvMain_TvnSelchanging(hDlg, wParam, lParam)
End SELECT
End SELECT
' Up-Down control notifications.
Case UDN_DELTAPOS
Select CASE lpNmh->idFrom
Case spnIconID
Select CASE lpNmh->Code
Case UDN_DELTAPOS
Action = MainWnd_spnIconID_UdnDeltapos(hDlg, wParam, lParam)
End SELECT
End SELECT
case TVN_BEGINLABELEDIT:
Dim As LPNMTVDISPINFO lptvdi = Cast(LPNMTVDISPINFO,lpNmh )
Case TVN_ENDLABELEDIT:
case TVM_DELETEITEM:
case TVN_BEGINDRAG:
Case TVN_BEGINRDRAG:
case TVN_GETDISPINFO:
Dim As LPNMTVDISPINFO lptvdi = Cast(LPNMTVDISPINFO,lpNmh )
case TVN_ITEMEXPANDING:
Dim As LPNMTREEVIEW lppnmtv = Cast(LPNMTREEVIEW,lpNmh )
case TVN_SELCHANGING:
Dim As LPNMTREEVIEW lppnmtv = Cast(LPNMTREEVIEW,lpNmh )
case TVN_ITEMEXPANDED:
Dim As LPNMTREEVIEW lppnmtv = Cast(LPNMTREEVIEW,lpNmh )
case TVN_SELCHANGED:
End SELECT
CASE WM_COMMAND
Select CASE LoWord(wParam)
Case txtIconID
Select CASE HiWord(wParam)
Case EN_CHANGE
Action = MainWnd_txtIconID_EnChange(hDlg, wParam, lParam)
Case EN_UPDATE
Action = MainWnd_txtIconID_EnUpdate(hDlg, wParam, lParam)
End SELECT
Case txtItemText
Action = TRUE
Case cmdAddItem
If HiWord(wParam) = BN_CLICKED THEN
Action = MainWnd_cmdAddItem_BnClicked(hDlg, wParam, lParam)
End IF
Case cmdQuit
If HiWord(wParam) = BN_CLICKED THEN
Action = MainWnd_cmdQuit_BnClicked(hDlg, wParam, lParam)
End IF
End SELECT
CASE WM_SYSCOMMAND
Select CASE wParam
Case SC_CLOSE
DestroyWindow hDlg
Action = TRUE
End SELECT
END SELECT
' Assign function return value.
FUNCTION = Action
END FUNCTION