The Architect n00b Posts: 14
| The following is the code I have managed to write so far for my GUI:
Code: | #include "QSDCHub.h" #include "stdafx.h" LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_NOTIFY: switch(LOWORD(wParam)) { case IDC_TVMAIN: { switch(((LPNMHDR) lParam)->code) { case NM_CLICK: //This is where I am stuck... I would like a conditional statement so if a particular item is selected, it performs a particular function (like showing a dialog that is a child of the main form) break; } } break; } break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } HTREEITEM AddItemToTree(HWND hwndTrV, LPSTR lpszItem, int nLevel) { TVITEM tvi; TVINSERTSTRUCT tvins; static HTREEITEM hPrev = (HTREEITEM)TVI_FIRST; static HTREEITEM hPrevRootItem = NULL; static HTREEITEM hPrevLev2Item = NULL; HTREEITEM hti; tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM; tvi.pszText = LPWSTR(lpszItem); tvi.cchTextMax = sizeof(tvi.pszText)/sizeof(tvi.pszText[0]); tvi.lParam = (LPARAM)nLevel; tvins.item = tvi; tvins.hInsertAfter = hPrev; if (nLevel == 1) tvins.hParent = TVI_ROOT; else if (nLevel == 2) tvins.hParent = hPrevRootItem; else tvins.hParent = hPrevLev2Item; hPrev = (HTREEITEM)SendMessage(hwndTrV, TVM_INSERTITEM, 0, (LPARAM)(LPTVINSERTSTRUCT)&tvins); if (nLevel == 1) hPrevRootItem = hPrev; else if (nLevel == 2) hPrevLev2Item = hPrev; if (nLevel > 1) { hti = TreeView_GetParent(hwndTrV, hPrev); tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE; tvi.hItem = hti; TreeView_SetItem(hwndTrV, &tvi); } return hPrev; } BOOL InitTreeViewImageLists(HWND hwndTV) { return TRUE; } BOOL InitTreeViewItems(HWND hwndTrV) { int nLevel; AddItemToTree(hwndTrV, LPSTR(TEXT("Main")), 2); AddItemToTree(hwndTrV, LPSTR(TEXT("Hub Statistics")), -1); AddItemToTree(hwndTrV, LPSTR(TEXT("Appearance")), -1); AddItemToTree(hwndTrV, LPSTR(TEXT("Network")), -1); AddItemToTree(hwndTrV, LPSTR(TEXT("Hublists")), -1); AddItemToTree(hwndTrV, LPSTR(TEXT("Expert Settings")), -1); AddItemToTree(hwndTrV, LPSTR(TEXT("User Settings")), 2); AddItemToTree(hwndTrV, LPSTR(TEXT("Bots")), -1); return TRUE; } HWND CreateTreeView(HWND parent) { HWND hwndTrV; RECT rcClient; GetClientRect(parent, &rcClient); hwndTrV = CreateWindowEx(0, WC_TREEVIEW, TEXT("TV_Menu"), WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES, 0, 0, 150, rcClient.bottom, parent, (HMENU)IDC_TVMAIN, GetModuleHandle(NULL), NULL); if (!InitTreeViewItems(hwndTrV)) { DestroyWindow(hwndTrV); return FALSE; } return hwndTrV; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; HWND hwnd; MSG Msg; wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_BLKSRV)); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName = NULL; wc.lpszClassName = TEXT("QSDCHubMain"); wc.hIconSm = LoadIcon(wc.hInstance, MAKEINTRESOURCE(IDI_BLKSRV)); if(!RegisterClassEx(&wc)) { MessageBox(NULL, TEXT("Window Registration Failed!"), TEXT("Error!"), MB_ICONEXCLAMATION | MB_OK); return 0; } hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, TEXT("QSDCHubMain"), TEXT("QSDCHub"), WS_BORDER | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 440, NULL, NULL, hInstance, NULL); CreateTreeView(hwnd); if(hwnd == NULL) { MessageBox(NULL, TEXT("Window Creation Failed!"), TEXT("Error!"), MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } | In WndProc, i have commented exactly where I am stuck. I do not know what to do from here and have tried looking through other places for a solution to this dilemma. This was posted in a few other dev forums and no one was able to answer lol
|
The Architect n00b Posts: 14
| I have solved this problem now. And in case anyone has the same problem, here is how I did it. I am sure there are more elegant ways of coding this, but this is what I came up with: First, I initialized values on the top of my source that will store addresses to the treeview items:
Code: | static HTREEITEM TVI_MAIN = NULL; static HTREEITEM TVI_HSTATS = NULL; static HTREEITEM TVI_APP = NULL; static HTREEITEM TVI_NTWRK = NULL; static HTREEITEM TVI_HLST = NULL; static HTREEITEM TVI_SETEX = NULL; static HTREEITEM TVI_USET = NULL; static HTREEITEM TVI_BOTS = NULL; | Then, in AddItemToTree, I specified a pointer to the static value of the item being added's handle:
Code: | HTREEITEM *TrI = NULL; | In the same function, I specified which values to place where:
Code: | if(lpszItem == "Main") { TrI = &TVI_MAIN; *TrI = hPrev; } else if(lpszItem == "Hub Statistics") { TrI = &TVI_HSTATS; *TrI = hPrev; } else if(lpszItem == "Appearance") { TrI = &TVI_APP; *TrI = hPrev; } else if(lpszItem == "Network") { TrI = &TVI_NTWRK; *TrI = hPrev; } else if(lpszItem == "Hublists") { TrI = &TVI_HLST; *TrI = hPrev; } else if(lpszItem == "Expert Settings") { TrI = &TVI_SETEX; *TrI = hPrev; } else if(lpszItem == "User Settings") { TrI = &TVI_USET; *TrI = hPrev; } else if(lpszItem == "Bots") { TrI = &TVI_BOTS; *TrI = hPrev; } | So my entire code ends up like this (The following code will send a message box indicating the selected Item in the treeview when selection is changed):
Code: | #include "QSDCHub.h" #include "stdafx.h" using namespace std; static HTREEITEM TVI_MAIN = NULL; static HTREEITEM TVI_HSTATS = NULL; static HTREEITEM TVI_APP = NULL; static HTREEITEM TVI_NTWRK = NULL; static HTREEITEM TVI_HLST = NULL; static HTREEITEM TVI_SETEX = NULL; static HTREEITEM TVI_USET = NULL; static HTREEITEM TVI_BOTS = NULL; LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { NMHDR *nmptr; switch(msg) { case WM_NOTIFY: switch(LOWORD(wParam)) { case IDC_TVMAIN: { nmptr = (LPNMHDR) lParam; switch(nmptr->code) { case TVN_SELCHANGED: { std::stringstream ss; HTREEITEM tvi = ((LPNM_TREEVIEW)nmptr)->itemNew.hItem; if(tvi == TVI_MAIN) { ss << "Selected Item : Main"; MessageBox(NULL, ss.str().c_str(), "Item Selection", MB_OK);} else if(tvi == TVI_HSTATS) { ss << "Selected Item : Hub Statistics"; MessageBox(NULL, ss.str().c_str(), "Item Selection", MB_OK); } else if(tvi == TVI_APP) { ss << "Selected Item : Appearance"; MessageBox(NULL, ss.str().c_str(), "Item Selection", MB_OK); } else if(tvi == TVI_NTWRK) { ss << "Selected Item : Network"; MessageBox(NULL, ss.str().c_str(), "Item Selection", MB_OK); } else if(tvi == TVI_HLST) { ss << "Selected Item : Hublists"; MessageBox(NULL, ss.str().c_str(), "Item Selection", MB_OK); } else if(tvi == TVI_SETEX) { ss << "Selected Item : Expert Settings"; MessageBox(NULL, ss.str().c_str(), "Item Selection", MB_OK); } else if(tvi == TVI_USET) { ss << "Selected Item : User Settings"; MessageBox(NULL, ss.str().c_str(), "Item Selection", MB_OK); } else if(tvi == TVI_BOTS) { ss << "Selected Item : Bots"; MessageBox(NULL, ss.str().c_str(), "Item Selection", MB_OK); } } break; } } break; } break; case WM_COMMAND: switch(wParam) { } break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } HTREEITEM AddItemToTree(HWND hwndTrV, LPSTR lpszItem, int nLevel) { TVITEM tvi; TVINSERTSTRUCT tvins; static HTREEITEM hPrev = (HTREEITEM)TVI_FIRST; static HTREEITEM hPrevRootItem = NULL; static HTREEITEM hPrevLev2Item = NULL; HTREEITEM hti; HTREEITEM *TrI = NULL; tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM; tvi.pszText = lpszItem; tvi.cchTextMax = sizeof(tvi.pszText)/sizeof(tvi.pszText[0]); tvi.lParam = (LPARAM)nLevel; tvins.item = tvi; tvins.hInsertAfter = hPrev; if (nLevel == 1) tvins.hParent = TVI_ROOT; else if (nLevel == 2) tvins.hParent = hPrevRootItem; else tvins.hParent = hPrevLev2Item; hPrev = (HTREEITEM)SendMessage(hwndTrV, TVM_INSERTITEM, 0, (LPARAM)(LPTVINSERTSTRUCT)&tvins); if (nLevel == 1) hPrevRootItem = hPrev; else if (nLevel == 2) hPrevLev2Item = hPrev; if(lpszItem == "Main") { TrI = &TVI_MAIN; *TrI = hPrev; } else if(lpszItem == "Hub Statistics") { TrI = &TVI_HSTATS; *TrI = hPrev; } else if(lpszItem == "Appearance") { TrI = &TVI_APP; *TrI = hPrev; } else if(lpszItem == "Network") { TrI = &TVI_NTWRK; *TrI = hPrev; } else if(lpszItem == "Hublists") { TrI = &TVI_HLST; *TrI = hPrev; } else if(lpszItem == "Expert Settings") { TrI = &TVI_SETEX; *TrI = hPrev; } else if(lpszItem == "User Settings") { TrI = &TVI_USET; *TrI = hPrev; } else if(lpszItem == "Bots") { TrI = &TVI_BOTS; *TrI = hPrev; } if (nLevel > 1) { hti = TreeView_GetParent(hwndTrV, hPrev); tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE; tvi.hItem = hti; TreeView_SetItem(hwndTrV, &tvi); } return hPrev; } BOOL InitTreeViewImageLists(HWND hwndTV) { return TRUE; } BOOL InitTreeViewItems(HWND hwndTrV) { int nLevel; AddItemToTree(hwndTrV, LPSTR("Main"), 2); AddItemToTree(hwndTrV, LPSTR("Hub Statistics"), -1); AddItemToTree(hwndTrV, LPSTR("Appearance"), -1); AddItemToTree(hwndTrV, LPSTR("Network"), -1); AddItemToTree(hwndTrV, LPSTR("Hublists"), -1); AddItemToTree(hwndTrV, LPSTR("Expert Settings"), -1); AddItemToTree(hwndTrV, LPSTR("User Settings"), 2); AddItemToTree(hwndTrV, LPSTR("Bots"), -1); return TRUE; } HWND CreateTreeView(HWND parent) { HWND hwndTrV; RECT rcClient; GetClientRect(parent, &rcClient); hwndTrV = CreateWindowEx(0, WC_TREEVIEW, "TV_Menu", WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES, 0, 0, 150, rcClient.bottom, parent, (HMENU)IDC_TVMAIN, GetModuleHandle(NULL), NULL); if (!InitTreeViewItems(hwndTrV)) { DestroyWindow(hwndTrV); return FALSE; } return hwndTrV; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; HWND hwnd; MSG Msg; wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_BLKSRV)); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName = NULL; wc.lpszClassName = TEXT("QSDCHubMain"); wc.hIconSm = LoadIcon(wc.hInstance, MAKEINTRESOURCE(IDI_BLKSRV)); if(!RegisterClassEx(&wc)) { MessageBox(NULL, TEXT("Window Registration Failed!"), TEXT("Error!"), MB_ICONEXCLAMATION | MB_OK); return 0; } hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, TEXT("QSDCHubMain"), TEXT("QSDCHub"), WS_BORDER | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 440, NULL, NULL, hInstance, NULL); CreateTreeView(hwnd); if(hwnd == NULL) { MessageBox(NULL, TEXT("Window Creation Failed!"), TEXT("Error!"), MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } |
|