菜单
菜单的讲解以上节所创建的列表框为基础进行讲解菜单
下图便是我们上节课创建的列表
现在我们针对窗口的顶层菜单(即上图中文件,帮助)进行讲解
Menu是菜单的目录,我们可以进行菜单的新建复制等等操作
顶层菜单的修改地址如下
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WIN32); //此处是顶层菜单的定义,我们可以在()中替换菜单类型
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
现我们开始对菜单功能的实现
实现地址如下
case WM_COMMAND: //此处对菜单功能进行实现
{
int wmId = LOWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
接下来我们开始具体实现相关功能
case WM_COMMAND:
{
switch (LOWORD(wParam)) //判断鼠标点击处
{
case ID_TEST://该选项是点击菜单中某项的ID,我们可以自己在菜单项属性中进行项ID的修改。注意包含菜单的头文件,由于ID延迟,可能报错但没有问题
{
MessageBox(NULL, L"ID_TEST", L"Msg", MB_OK);//此时当我们点击该菜单项时,会弹窗
break;
}
case ID_GETTEXT://此处是点击该ID的菜单项时的操作
{
HWND hSWnd = GetDlgItem(hWnd, IDC_LIST_COMM); //获取当前点击的位置,行数索引
DWORD dwItemIndex = ListView_GetSelectionMark(hSWnd); //获取文本
WCHAR * szBuffer = new WCHAR[50];
ListView_GetItemText(hSWnd, dwItemIndex, 2, szBuffer, 50);
MessageBox(hWnd, szBuffer, L"Msg", MB_OK);
break;
}
case ID_DELETELINE: //此处是点击该ID的菜单项时的操作
{
HWND hSWnd = GetDlgItem(hWnd, IDC_LIST_COMM);
//获取当前点击的位置,行数索引
DWORD dwItemIndex = ListView_GetSelectionMark(hSWnd);
//删除选择行
SendMessage(hSWnd, LVM_DELETEITEM, dwItemIndex, NULL);
break;
}
default:
break;
}
}
break;
case WM_NOTIFY:
{
switch (((LPNMHDR)lParam)->code)
{
case NM_CLICK:
{
break;
}
case NM_RCLICK://右键点击处,弹出菜单
{
POINT pt = { 0 };
GetCursorPos(&pt); //获取当前右键点击的坐标
HMENU hMenu = LoadMenu((HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), MAKEINTRESOURCEW(IDR_MENU2))//获取主菜单; IDR_MENU2是我们自行创建的一个菜单项
HMENU hSubMenu = GetSubMenu(hMenu, NULL);//获取菜单的子菜单
TrackPopupMenu(hSubMenu, TPM_CENTERALIGN, pt.x, pt.y, 0, hWnd, NULL);//显示菜单
break;
}
default:
break;
}
break;
}
资源:打开资源头文件,发现里面保存的是一些ID,并没有相应的资源,因此他并不是真正的资源。真正的资源在该cpp文件目录下的.rc文件
如下便是部分真正的资源
其中IDM_EXIT等等便是菜单有效项ID,即真正实现功能的项ID,存储在资源头文件中
树
现我们以一个全新的Windows窗口进行树的讲解
树在WM_CREATE消息中进行创建
如下我们便创建一个最基础的树:
HWND hTreeWnd = CreateWindow(L"SysTreeView32", L"", WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_CHECKBOXES| TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS, 10, 10, 450, 600, hWnd, (HMENU)IDC_TREE_COMM, (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL);//创建最基础的树框架
其中TVS_HASLINES样式可使父节点与子节点之间用线连接
TVS_LINESATROOT节点前有加减号
TVS_HASBUTTONS节点前有复选框可以勾选
如图便是一个有填充任何东西的树的框架
接下来我们开始逐步填充东西,使最后达到如下图的效果
如下我们开始创建树插入节点的函数
HTREEITEM CreateTreeNode(HWND hWnd, HTREEITEM hParentNode, const WCHAR * szBuffer)
{
TVINSERTSTRUCT Stvi;//树节点结构
Stvi.item.mask = TVIF_TEXT;//填充数据类型
Stvi.hParent = hParentNode;//父节点
Stvi.hInsertAfter = TVI_LAST;//插入方式
Stvi.item.pszText = new WCHAR[50];//申请文本缓冲区
swprintf(Stvi.item.pszText, szBuffer);//填充缓冲区
HTREEITEM hRoot = (HTREEITEM)SendMessage(hWnd, TVM_INSERTITEM, 0, (LPARAM)&Stvi);//设置根节点
return hRoot;
}
接下来我们在WM_CRETE消息中创建树
HTREEITEM hRoot1 = CreateTreeNode(hTreeWnd, TVI_ROOT, L"RootNode1");//创建根节点,根节点填TVI_ROOT
HTREEITEM h1SubNode1 = CreateTreeNode(hTreeWnd, hRoot1, L"hSubNode1");//创建子节点。此时点击根节点会出现子节点
CreateTreeNode(hTreeWnd, h1SubNode1, L"hSubNode1Sub1");
CreateTreeNode(hTreeWnd, h1SubNode1, L"hSubNode1Sub2");
HTREEITEM h1SubNode2 = CreateTreeNode(hTreeWnd, hRoot1, L"hSubNode2");
HTREEITEM hRoot2 = CreateTreeNode(hTreeWnd, TVI_ROOT, L"RootNode2");
HTREEITEM h2SubNode1 = CreateTreeNode(hTreeWnd, hRoot2, L"hSubNode1");
HTREEITEM h2SubNode2 = CreateTreeNode(hTreeWnd, hRoot2, L"hSubNode2");
HTREEITEM hRoot3 = CreateTreeNode(hTreeWnd, TVI_ROOT, L"RootNode3");
HTREEITEM h3SubNode1 = CreateTreeNode(hTreeWnd, hRoot3, L"hSubNode1");
HTREEITEM h3SubNode2 = CreateTreeNode(hTreeWnd, hRoot3, L"hSubNode2");
如上,我们便创建了一个相对完整的树
创建完一个树以后,我们开始对树进行应用。
树的应用在WM_NOTIFY消息中
case WM_NOTIFY:
{
switch (((LPNMHDR)lParam)->code)
{
case NM_CLICK:
{
HWND hWndTree = GetDlgItem(hWnd, IDC_TREE_COMM);//获取树句柄
TVHITTESTINFO ti;//用于检测是否点击树
POINT pi = { 0 };
GetCursorPos(&pi);//获取点击位置
ScreenToClient(hWndTree, &pi);//查询点击树的位置
ti.flags = TVHT_ONITEMLABEL;
ti.pt = pi;
HTREEITEM hItem = TreeView_HitTest(hWndTree, &ti);//获取树的项的句柄
TVITEM Item;
Item.pszText = new WCHAR[50];
Item.mask = TVIF_TEXT;
Item.cchTextMax = 50;
Item.hItem = hItem;
SendMessage(hWndTree, TVM_GETITEM, NULL, (LPARAM)&Item);
MessageBox(NULL, Item.pszText, L"Msg", MB_OK);//点击树的项会获取项的文本值
TVITEM Item2;
Item2.pszText = new WCHAR[50];
Item2.mask = TVIF_TEXT;
Item2.cchTextMax = 50;
Item2.hItem = hItem;
wsprintf(Item2.pszText, L"rkvir");//点击树的项修改的文本值
SendMessage(hWndTree, TVM_SETITEM, NULL, (LPARAM)&Item2);//点击树的项以后修改项文本为指定内容
SendMessage(hWndTree,TVM_DELETEITEM, NULL, (LPARAM)hItem); //点击树的项删除,同时删除父节点和子节点
UINT uFlag = TreeView_GetCheckState(hWndTree, hItem);//判断是否点中树的项前的复选框
if (uFlag)
{
MessageBox(NULL, L"选中" , L"Msg", MB_OK);
}
else
{
MessageBox(NULL, L"未选中", L"Msg", MB_OK);
}
break;
}
case NM_RCLICK:
{
MessageBox(NULL, L"右键", L"Msg", MB_OK);//右键点击树后弹窗
break;
}
default: