#include <windows.h>
#include <commctrl.h>
#define IDD_DIALOG1 101
#define IDB_TREE 134
#define IDB_FILE 135
#define IDI_ICON1 136
#define IDC_TREE1 1059
#define IDC_DELETE 1061
#define IDC_ADDROOT 1062
#define IDC_CHILD 1063
#define IDC_DELALL 1065
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
HINSTANCE hInst;
TV_ITEM tvi;
HTREEITEM Selected;
TV_INSERTSTRUCT tvinsert; // struct to config out tree control
HTREEITEM Parent; // Tree item handle
HTREEITEM Before; // .......
HTREEITEM Root; // .......
HIMAGELIST hImageList; // Image list array hadle
HBITMAP hBitMap; // bitmap handler
bool flagSelected=false;
// for drag and drop
HWND hTree;
TVHITTESTINFO tvht;
HTREEITEM hitTarget;
POINTS Pos;
bool Dragging;
// for lable editing
HWND hEdit;
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
hTree=CreateWindow(WC_TREEVIEW,0,WS_VISIBLE|WS_CHILD | TVS_HASLINES | TVS_LINESATROOT | TVS_EDITLABELS | WS_BORDER | WS_TABSTOP,2,8,134,110,hwnd,(HMENU)IDC_TREE1,hInst,0);
//GetDlgItem(hwnd,IDC_TREE1);
// creating image list and put it into the tree control
//====================================================//
hImageList=ImageList_Create(16,16,ILC_COLOR16,2,10); // Macro: 16x16:16bit with 2 pics [array]
hBitMap=LoadBitmap(hInst,"LIST.bmp"); // load the picture from the resource
ImageList_Add(hImageList,hBitMap,NULL); // Macro: Attach the image, to the image list
DeleteObject(hBitMap); // no need it after loading the bitmap
SendDlgItemMessage(hwnd,IDC_TREE1,TVM_SETIMAGELIST,0,(LPARAM)hImageList); // put it onto the tree control
tvinsert.hParent=NULL; // top most level no need handle
tvinsert.hInsertAfter=TVI_ROOT; // work as root level
tvinsert.item.mask=TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
tvinsert.item.pszText="Parent";
tvinsert.item.iImage=0;
tvinsert.item.iSelectedImage=1;
// [+]
// |
/* |--*/Parent=(HTREEITEM)SendDlgItemMessage(hwnd,IDC_TREE1,TVM_INSERTITEM,0,(LPARAM)&tvinsert);
/* | */Root=Parent;
/* | */Before=Parent; // handle of the before root
/* | */tvinsert.hParent=Parent; // handle of the above data
/* | */tvinsert.hInsertAfter=TVI_LAST; // below parent
/* | */tvinsert.item.pszText="Child 1";
/* | |--[+] */
/* | | | */
/* | | |*/ Parent=(HTREEITEM)SendDlgItemMessage(hwnd,IDC_TREE1,TVM_INSERTITEM,0,(LPARAM)&tvinsert);
/* | | |*/ tvinsert.hParent=Parent;
/* | | |*/ tvinsert.item.pszText="Child of Child1";
/* | | |*/ Parent=(HTREEITEM)SendDlgItemMessage(hwnd,IDC_TREE1,TVM_INSERTITEM,0,(LPARAM)&tvinsert);
/* | | |-[+]*/
/* | | | */
/* | | | */
/* | | */ tvinsert.hParent=Parent;
/* | | */ tvinsert.hInsertAfter=TVI_LAST;
/* | | */ tvinsert.item.pszText="Double Click Me!";
/* | | */ tvinsert.item.mask=TVIF_TEXT;
/* | | */ SendDlgItemMessage(hwnd,IDC_TREE1,TVM_INSERTITEM,0,(LPARAM)&tvinsert);
/* | */tvinsert.hParent=Before; // handle of the above data
/* | */tvinsert.hInsertAfter=TVI_LAST; // below parent
/* | */tvinsert.item.pszText="Child 2";
/* | */Parent=(HTREEITEM)SendDlgItemMessage(hwnd,IDC_TREE1,TVM_INSERTITEM,0,(LPARAM)&tvinsert);
/* | */
tvinsert.hParent=NULL; // top most level no need handle
tvinsert.hInsertAfter=TVI_LAST; // work as root level
tvinsert.item.pszText="Parent2";
Parent=(HTREEITEM)SendDlgItemMessage(hwnd,IDC_TREE1,TVM_INSERTITEM,0,(LPARAM)&tvinsert);
break;
case WM_NOTIFY:
{
case IDC_TREE1:
if(((LPNMHDR)lParam)->code == NM_DBLCLK) // if code == NM_CLICK - Single click on an item
{
char Text[255]="";
memset(&tvi,0,sizeof(tvi));
Selected=(HTREEITEM)SendDlgItemMessage(hwnd,IDC_TREE1,TVM_GETNEXTITEM,TVGN_CARET,(LPARAM)Selected);
if(Selected==NULL)
{
MessageBox(hwnd,"No Items in TreeView","Error",MB_OK|MB_ICONINFORMATION);
break;
}
TreeView_EnsureVisible(hwnd,Selected);
SendDlgItemMessage(hwnd,IDC_TREE1,TVM_SELECTITEM,TVGN_CARET,(LPARAM)Selected);
flagSelected=true;
tvi.mask=TVIF_TEXT;
tvi.pszText=Text;
tvi.cchTextMax=256;
tvi.hItem=Selected;
if(SendDlgItemMessage(hwnd,IDC_TREE1,TVM_GETITEM,TVGN_CARET,(LPARAM)&tvi))
{
if(tvi.cChildren==0 && strcmp(tvi.pszText,"Double Click Me!")==0)
{
MessageBox(hwnd,"Press OK to delete me!","Example",MB_OK|MB_ICONINFORMATION);
SendDlgItemMessage(hwnd,IDC_TREE1,TVM_DELETEITEM,TVGN_CARET,(LPARAM)tvi.hItem);
flagSelected=false;
break;
}
}
}
if(((LPNMHDR)lParam)->code == NM_RCLICK) // Right Click
{
Selected=(HTREEITEM)SendDlgItemMessage (hwnd,IDC_TREE1,TVM_GETNEXTITEM,TVGN_DROPHILITE,0);
if(Selected==NULL)
{
MessageBox(hwnd,"No Items in TreeView","Error",MB_OK|MB_ICONINFORMATION);
break;
}
SendDlgItemMessage(hwnd,IDC_TREE1,TVM_SELECTITEM,TVGN_CARET,(LPARAM)Selected);
SendDlgItemMessage(hwnd,IDC_TREE1,TVM_SELECTITEM,TVGN_DROPHILITE,(LPARAM)Selected);
TreeView_EnsureVisible(hTree,Selected);
}
if(((LPNMHDR)lParam)->code == TVN_BEGINDRAG)
{
HIMAGELIST hImg;
LPNMTREEVIEW lpnmtv = (LPNMTREEVIEW) lParam;
hImg=TreeView_CreateDragImage(hTree, lpnmtv->itemNew.hItem);
ImageList_BeginDrag(hImg, 0, 0, 0);
ImageList_DragEnter(hTree,lpnmtv->ptDrag.x,lpnmtv->ptDrag.y);
ShowCursor(FALSE);
SetCapture(hwnd);
Dragging = TRUE;
}
if(((LPNMHDR)lParam)->code == TVN_BEGINLABELEDIT)
{
hEdit=TreeView_GetEditControl(hTree);
}
if(((LPNMHDR)lParam)->code == TVN_ENDLABELEDIT)
{
char Text[256]="";
tvi.hItem=Selected;
SendDlgItemMessage(hwnd,IDC_TREE1,TVM_GETITEM,0,(WPARAM)&tvi);
GetWindowText(hEdit, Text, sizeof(Text));
tvi.pszText=Text;
SendDlgItemMessage(hwnd,IDC_TREE1,TVM_SETITEM,0,(WPARAM)&tvi);
}
}
break;
case WM_MOUSEMOVE:
{
if (Dragging)
{
Pos = MAKEPOINTS(lParam);
ImageList_DragMove(Pos.x-32, Pos.y-25); // where to draw the drag from
ImageList_DragShowNolock(FALSE);
tvht.pt.x = Pos.x-20; // the highlight items should be as the same points as the drag
tvht.pt.y = Pos.y-20; //
//if(hitTarget=(HTREEITEM)SendMessage(hTree,TVM_HITTEST,NULL,(LPARAM)&tvht)) // if there is a hit
SendMessage(hTree,TVM_SELECTITEM,TVGN_DROPHILITE,(LPARAM)hitTarget); // highlight it
ImageList_DragShowNolock(TRUE);
}
}
break;
case WM_LBUTTONUP:
{
if (Dragging)
{
ImageList_DragLeave(hTree);
ImageList_EndDrag();
Selected=(HTREEITEM)SendDlgItemMessage(hwnd,IDC_TREE1,TVM_GETNEXTITEM,TVGN_DROPHILITE,0);
SendDlgItemMessage(hwnd,IDC_TREE1,TVM_SELECTITEM,TVGN_CARET,(LPARAM)Selected);
SendDlgItemMessage(hwnd,IDC_TREE1,TVM_SELECTITEM,TVGN_DROPHILITE,0);
ReleaseCapture();
ShowCursor(TRUE);
Dragging = FALSE;
}
}
break;
case WM_COMMAND: // Controling the Buttons
{
switch (LOWORD(wParam)) // what we pressed on?
{
case IDC_DELETE: // Generage Button is pressed
{
if(flagSelected==true)
{
if(tvi.cChildren==0)
SendDlgItemMessage(hwnd,IDC_TREE1,TVM_DELETEITEM,TVGN_CARET,(LPARAM)tvi.hItem);
flagSelected=false;
}
else{
MessageBox(hwnd,"Double Click Item to Delete","Message",MB_OK|MB_ICONINFORMATION);
}
}
break;
case IDC_ADDROOT:
{
tvinsert.hParent=NULL; // top most level no need handle
tvinsert.hInsertAfter=TVI_ROOT; // work as root level
tvinsert.item.pszText="Parent Added";
Parent=(HTREEITEM)SendDlgItemMessage(hwnd,IDC_TREE1,TVM_INSERTITEM,0,(LPARAM)&tvinsert);
UpdateWindow(hwnd);
}
break;
case IDC_CHILD:
{
tvinsert.hParent=Selected; // top most level no need handle
tvinsert.hInsertAfter=TVI_LAST; // work as root level
tvinsert.item.pszText="Child Added";
Parent=(HTREEITEM)SendDlgItemMessage(hwnd,IDC_TREE1,TVM_INSERTITEM,0,(LPARAM)&tvinsert);
TreeView_SelectDropTarget(GetDlgItem(hwnd,IDC_TREE1),Parent);
}
break;
case IDC_DELALL:
{
int TreeCount=TreeView_GetCount(GetDlgItem(hwnd,IDC_TREE1));
for(int i=0;i<TreeCount;i++)
TreeView_DeleteAllItems(GetDlgItem(hwnd,IDC_TREE1));
}
break;
}
break;
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.