簡(jiǎn)單js樹形目錄菜單代碼(js實(shí)現(xiàn)樹結(jié)構(gòu)菜單)
樹形控件
MFC中使用CTreeCtrl類來封裝樹形控件各種操作,樹形控件可以用于樹形結(jié)構(gòu),比如目錄樹等。其中有一個(gè)根節(jié)點(diǎn)root,下面有很多子節(jié)點(diǎn)。
常用屬性配置:
初始化
設(shè)置圖標(biāo)并添加根節(jié)點(diǎn)和子節(jié)點(diǎn):
m_icon_list.Create(32, 32, ILC_COLOR32, 3, 3);
UINT icons[] = { IDI_ICON_BTC32, IDI_ICON_LTC32, IDI_ICON_XRP32 };
for (int i = 0; i 3; i++)
展開全文
m_icon_list.Add(AfxGetApp()-LoadIconW(icons[i]));
m_tree.SetImageList(m_icon_list, L對(duì)陣IL_NORMAL);
HTREEITEM root1 = m_tree.InsertItem(_T("root1"), 0, 0);
HTREEITEM root2 = m_tree.InsertItem(_T("root2"), 1, 1);
HTREEITEM sub1 = m_tree.InsertItem(_T("sub1"), 2, 2, root1);
m_tree.Expand(root1, TVE_EXPAND);
添加節(jié)點(diǎn)
void CMyDlg::OnBnClickedBtnAdd()
{
UpdateData(TRUE);
if (!m_string.GetLength()) {
AfxMessageBox(_T("請(qǐng)輸入文本"));
return;
}
HTREEITEM item = m_tree.GetSelectedItem();
if (!item) item = TVI_ROOT;
TVINSERTSTRUCT ts = { 0 };
ts.hParent = item;
ts.hInsertAfter = TVI_LAST;
ts.item.pszText = m_string.GetBuffer();
ts.item.mask = TVIF_TEXT | TVIF_IMAGE;
HTREEITEM new_item = m_tree.InsertItem(ts);
m_tree.SelectItem(new_item);
m_tree.EnsureVisible(new_item);
m_string = _T("");
UpdateData(FALSE);
}
刪除節(jié)點(diǎn)
void CMyDlg::OnBnClickedBtnDel()
{
HTREEITEM item = m_tree.GetSelectedItem();
if (!item) {
AfxMessageBox(_T("請(qǐng)選擇一個(gè)節(jié)點(diǎn)"));
return;
}
HTREEITEM parent = m_tree.GetParentItem(item);
m_tree.DeleteItem(item);
m_tree.SelectItem(parent);
}
修改節(jié)點(diǎn)
void CMyDlg::OnBnClickedBtnModify()
{
UpdateData(TRUE);
HTREEITEM item = m_tree.GetSelectedItem();
if (!item) {
AfxMessageBox(_T("請(qǐng)選擇一個(gè)節(jié)點(diǎn)"));
return;
}
if (!m_string.GetLength()) {
AfxMessageBox(_T("請(qǐng)輸入文本"));
return;
}
m_tree.SetItemText(item, m_string);
}
添加事情
選擇節(jié)點(diǎn),編輯框顯示節(jié)點(diǎn)中的內(nèi)容:
void CMyDlg::OnTvnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMTREEVIEW pNMTreeView = reinterpret_cast(pNMHDR);
UpdateData(TRUE);
HTREEITEM item = m_tree.GetSelectedItem();
if (!item) {
AfxMessageBox(_T("請(qǐng)選擇一個(gè)節(jié)點(diǎn)"));
return;
}
m_string = m_tree.GetItemText(item);
UpdateData(FALSE);
*pResult = 0;
}
掃描二維碼推送至手機(jī)訪問。
版權(quán)聲明:本文由飛速云SEO網(wǎng)絡(luò)優(yōu)化推廣發(fā)布,如需轉(zhuǎn)載請(qǐng)注明出處。