前言
- 各位师傅大家好,我是qmx_07,今天给大家讲解菜单和树控件的相关知识
菜单
认识菜单 及 创建自定义菜单栏
- 资源文件 -> 项目名.rc ->Menu
- 这是系统提供的默认菜单,也可以往里面添加修改内容
以下是 自定义菜单栏:
- 创建视图,修改菜单选项,需要绑定到注册窗口
- 通过创建自定义视图,绑定注册窗口,视觉效果就完成了,还需要对点击菜单选项,做出对应的效果
创建右键菜单栏
- 创建菜单视图
- 设置菜单对应的ID值
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_MENU1));
HMENU hSubMenu = GetSubMenu(hMenu, NULL);
TrackPopupMenu(hSubMenu, TPM_CENTERALIGN, pt.x, pt.y, 0, hWnd, NULL);
break;
}
default:
break;
}
break;
- 获取光标位置,加载菜单资源,子菜单资源,在鼠标居中的位置弹出菜单
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_GET_TEXT:
{
HWND hSWnd = GetDlgItem(hWnd, IDC_LIST);//获取点击的窗口句柄
DWORD dwltemIndex = ListView_GetSelectionMark(hSWnd);//读取句柄索引
WCHAR* szBuffer = new WCHAR[50];
ListView_GetItemText(hSWnd, dwltemIndex, 0, szBuffer, 50);//获取索引0位置的文本信息
MessageBox(hWnd, szBuffer, L"Msg", MB_OK);
break;
}
case IDC_Delete:
{
HWND hSWnd = GetDlgItem(hWnd, IDC_LIST);//获取点击的窗口句柄
DWORD dwltemIndex = ListView_GetSelectionMark(hSWnd);//读取句柄索引
SendMessage(hSWnd, LVM_DELETEITEM, dwltemIndex, NULL);
}
}
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;
- 获取文本 和 删除的功能实现
画面演示:
树控件
创建树控件
HWND hTreeWnd = CreateWindow(L"SysTreeView32", L"", WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS, 10, 10, 450, 600, hWnd, (HMENU)1, (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL);
- WS_BORDER:为窗口创建一个边框。
- 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;
}
HTREEITEM hRoot1 = CreateTreeNode(hTreeWnd, TVI_ROOT, L"根节点");
HTREEITEM h1SubNode1 = CreateTreeNode(hTreeWnd, hRoot1, L"选项1");
HTREEITEM h1SubNode2 = CreateTreeNode(hTreeWnd, hRoot1, L"选项2");
画面演示:
对节点 进行弹出文本、修改、删除操作
弹出文本:
case WM_NOTIFY:
{
switch (((LPNMHDR)lParam)->code)
{
case NM_CLICK:
{
//获取文本
HWND hWndTree = GetDlgItem(hWnd, 1); //获取 树控件ID
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);/获取树控件文本内容存储在Item中
MessageBox(NULL, Item.pszText, L"Msg", MB_OK);//显示内容
画面演示:
修改文本:
HWND hWndTree = GetDlgItem(hWnd, 1);
TVHITTESTINFO ti;
POINT pi = { 0 };
GetCursorPos(&pi);
ScreenToClient(hWndTree, &pi);
ti.flags = TVHT_ONITEMLABEL;
ti.pt = pi;
HTREEITEM hItem = TreeView_HitTest(hWndTree, &ti);
TVITEM Item2;
Item2.pszText = new WCHAR[50];
Item2.mask = TVIF_TEXT;
Item2.cchTextMax = 50;
Item2.hItem = hItem;
wsprintf(Item2.pszText, L"qmx_07");
SendMessage(hWndTree, TVM_SETITEM, NULL, (LPARAM)&Item2);
画面演示:
删除:
HWND hWndTree = GetDlgItem(hWnd, 1);
TVHITTESTINFO ti;
POINT pi = { 0 };
GetCursorPos(&pi);
ScreenToClient(hWndTree, &pi);
ti.flags = TVHT_ONITEMLABEL;
ti.pt = pi;
HTREEITEM hItem = TreeView_HitTest(hWndTree, &ti);
SendMessage(hWndTree,TVM_DELETEITEM, NULL, (LPARAM)hItem);
画面演示:
总结
- 介绍了菜单和控件的创建,获取文本和删除的相关知识