Esempio di un inserimento.
La treeview usa direttamente le funzioni di windows (in realtà macro) anche perché è strettamente legata al windows.
Le macro sono qui
http://msdn.microsoft.com/en-us/library/windows/desktop/ff486105(v=vs.85).aspxQuindi per una dettaglio sulle singole funzioni fare riferimento alla documentazione Microsoft.
I concetti principali sono:
- La treeview è fatta di item
- Ogni item è identificato da un handle e contiene un parametro numerico (lParam) e un testo (pszText): inoltre gli possono essere associate immagini.
- All'inserimento ad ogni item va dichiarato se qual'è l'handle parente, se non dichiarato è l'item di root, il primo.
- Ogni item ha una stato (state): può essere aperto (Expanded) o chiuso, selezionato in focus, ecc ...
Sintassi
static void _treeViewBuilder(HWND hwndTVDbase)
{
TVINSERTSTRUCT TVInsert;
HTREEITEM hMainTree,hParent;
HTREEITEM hClassTree;
HTREEITEM hTableTree;
FXD_ELEMENT sFxd;
INT a,Count;
EN_SQL_PLATFORM_INFO * psPi;
TreeView_SetImageList(sSetup.hwndTVDbase, himl, TVSIL_NORMAL); // <-- Associa delle immagini alla TV
TreeView_DeleteAllItems(sSetup.hwndTVDbase); // <-- Cancella tutti gli item
// -------------------------------------------
// Inserisco la radice dell'albero
_(TVInsert);
TVInsert.hParent=NULL;
TVInsert.hInsertAfter=TVI_FIRST; // <-- Modalità di inserimento
TVInsert.item.mask=TVIF_STATE|TVIF_TEXT|TVIF_HANDLE|TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_PARAM; // <-- Quali sono i parametri validi
TVInsert.item.pszText=(CHAR *) fxdServer(WS_REALGET,FHD_GET_NAME,NULL); // <-- Testo visualizzato
TVInsert.item.state=TVIS_BOLD|TVIS_EXPANDED; // <-- Stato
TVInsert.item.stateMask=TVIS_BOLD|TVIS_EXPANDED;
TVInsert.item.hItem=0;
TVInsert.item.iImage=imgDbase; // <-- handle della immagine associata; può non essere indicato
TVInsert.item.iSelectedImage=imgDbase;
TVInsert.item.lParam=0; // <-- Id numerico associato all'item
hClassTree=hMainTree=TreeView_InsertItem(sSetup.hwndTVDbase,&TVInsert);
Count=fxdServer(WS_COUNT,0,NULL);
for (a=0;a<Count;a++)
{
fxdServer(WS_REALGET,a,&sFxd);
_(TVInsert);
switch (sFxd.iType)
{
// -------------------------------------------
// Inserisco una classe
case FHD_FOLDER:
hParent=EhTreeViewSearch(sSetup.hwndTVDbase,sFxd.idParent); // <-- Cerco un handle con l'id (lParam)
if (!hParent) hParent=hMainTree;
TVInsert.hParent=hParent;
TVInsert.hInsertAfter=TVI_SORT;
TVInsert.item.mask=TVIF_STATE|TVIF_TEXT|TVIF_HANDLE|TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_PARAM;
TVInsert.item.pszText=sFxd.szName;
if (sFxd.psFolderInfo->bOpen)
{
TVInsert.item.iImage=imgFolderOpen;
TVInsert.item.iSelectedImage=imgFolderOpen;
TVInsert.item.state=TVIS_EXPANDED;
TVInsert.item.stateMask=TVIS_EXPANDED;
}
else
{
TVInsert.item.iImage=imgFolderClose;
TVInsert.item.iSelectedImage=imgFolderClose;
}
TVInsert.item.hItem=0;
TVInsert.item.lParam=sFxd.idAutocode;
hClassTree=TreeView_InsertItem(sSetup.hwndTVDbase,&TVInsert);
//win_infoarg("%s %d",sFxd.szName,sFxd.idAutocode);
//mouse_inp();
break;
case FHD_TABLE:
hParent=EhTreeViewSearch(sSetup.hwndTVDbase,sFxd.idParent); if (!hParent) hParent=hMainTree;
TVInsert.hParent=hParent;//hClassTree;
TVInsert.hInsertAfter=TVI_SORT;
TVInsert.item.mask=TVIF_TEXT|TVIF_HANDLE|TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_PARAM;
TVInsert.item.pszText=sFxd.szName;
TVInsert.item.hItem=0;
psPi=fxdPlatformInfo(sFxd.psTableInfo->enSqlPlatform);
if (sFxd.psTableInfo->enSqlPlatform==FHD_DEFAULT) {
printf("qui");
}
if (psPi->bSql)//sFxd.psTableInfo->enSqlPlatform)
{
TVInsert.item.iImage=imgTableSql;
TVInsert.item.iSelectedImage=imgTableSql;
}
else
{
TVInsert.item.iImage=imgTable;
TVInsert.item.iSelectedImage=imgTable;
}
TVInsert.item.lParam=sFxd.idAutocode;
hTableTree=TreeView_InsertItem(sSetup.hwndTVDbase,&TVInsert);
break;
case FHD_FIELD:
case FHD_INDEX:
break;
case FHD_VIEW:
case FHD_QUERY:
default:
hParent=EhTreeViewSearch(sSetup.hwndTVDbase,sFxd.idParent); if (!hParent) hParent=hMainTree;
TVInsert.hParent=hParent;//hClassTree;
TVInsert.hInsertAfter=TVI_SORT;
TVInsert.item.mask=TVIF_TEXT|TVIF_HANDLE|TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_PARAM;
TVInsert.item.pszText=sFxd.szName;
TVInsert.item.hItem=0;
TVInsert.item.iImage=imgView;
TVInsert.item.iSelectedImage=imgView;
TVInsert.item.lParam=sFxd.idAutocode;
hTableTree=TreeView_InsertItem(sSetup.hwndTVDbase,&TVInsert);
break;
break;
}
}
InvalidateRect(sSetup.hwndTVDbase,NULL,TRUE); // <-- Chiedo un refresh della treeview
}