Treeview Control Demo

To create a TreeView control specify WC_TREEVIEW as the window class and register the class by specifying the ICC_TREEVIEW_CLASSES bit flag in the accompanying INITCOMMONCONTROLSEX structure.

#define _WIN32_WINNT 0x0501 #include <windows.h> #include <commctrl.h> #pragma comment(lib,"comctl32.lib") #define ID_TREEVIEW 1 #define ID_CREATEBUTTON 2 #define ID_DELETEBUTTON 3 #define ID_EDITBOX 4 HWND hwndEdit; HWND hwndTV; LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); void CreateControls(HWND); void addnode(); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { HWND hwnd; MSG msg; WNDCLASS wc; wc.style=CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc=WndProc; wc.cbClsExtra=0; wc.cbWndExtra=0; wc.hInstance=hInstance; wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); wc.hCursor=LoadCursor(NULL,IDC_ARROW); wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName=NULL; wc.lpszClassName=TEXT("myWindowClass"); RegisterClass(&wc); hwnd=CreateWindow( TEXT("myWindowClass"), TEXT("Treeview Control"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 10, 10, 550, 250, NULL, NULL, hInstance, NULL); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: CreateControls(hwnd); addnode(); break; case WM_COMMAND: if(HIWORD(wParam)==BN_CLICKED) { switch(LOWORD(wParam)) { case ID_CREATEBUTTON: addnode(); break; case ID_DELETEBUTTON: { HTREEITEM item; item=TreeView_GetSelection(hwndTV); if(item) { TreeView_DeleteItem(hwndTV,item); SetFocus(hwndTV); } } break; } } break; case WM_NOTIFY: { LPNMHDR hdr; hdr=(LPNMHDR)lParam; if(hdr->code==NM_DBLCLK) { MessageBox(hwnd, TEXT("Tree item double clicked"), TEXT("Tree View"), MB_OK); } if(hdr->code==TVN_SELCHANGED) { // selection changed } } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,msg,wParam,lParam); } return 0; } void CreateControls(HWND hwnd) { INITCOMMONCONTROLSEX icex; icex.dwSize=sizeof(INITCOMMONCONTROLSEX); icex.dwICC=ICC_TREEVIEW_CLASSES; InitCommonControlsEx(&icex); hwndTV=CreateWindowEx( 0, WC_TREEVIEW, TEXT(""), WS_CHILD | WS_VISIBLE | WS_BORDER | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_SHOWSELALWAYS, 10, 10, 250, 150, hwnd, (HMENU)ID_TREEVIEW, GetModuleHandle(NULL), NULL); hwndEdit=CreateWindow( TEXT("edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 290, 170, 120, 25, hwnd, (HMENU)ID_EDITBOX, GetModuleHandle(NULL), NULL); CreateWindow( TEXT("button"), TEXT("Add Child"), WS_CHILD | WS_VISIBLE, 20, 170, 120, 25, hwnd, (HMENU)ID_CREATEBUTTON, GetModuleHandle(NULL), NULL); CreateWindow( TEXT("button"), TEXT("Delete Selected"), WS_CHILD | WS_VISIBLE, 150, 170, 120, 25, hwnd, (HMENU)ID_DELETEBUTTON, GetModuleHandle(NULL), NULL); } void addnode() { TV_INSERTSTRUCT tvinsert; HTREEITEM selected; ZeroMemory(&tvinsert,sizeof(TV_INSERTSTRUCT)); tvinsert.hInsertAfter=TVI_LAST; tvinsert.item.mask=TVIF_TEXT; // first node if(TreeView_GetCount(hwndTV)==0) { tvinsert.hParent=TVI_ROOT; tvinsert.item.pszText=TEXT("Root"); } // child nodes else { TCHAR text[100]; GetWindowText(hwndEdit,text,100); if(lstrlen(text)==0) { return; } selected=TreeView_GetSelection(hwndTV); if(selected==NULL) { selected=TVI_ROOT; } tvinsert.hParent=selected; tvinsert.item.pszText=text; } TreeView_InsertItem(hwndTV,&tvinsert); SetFocus(hwndTV); }