mfc treectrl 树节点添加 图标 和 tooltips(悬停提示信息)

 类似 工具栏的tooltips(可作为操作提示使用, 也可以是提示信息方便查看)

一、添加图标

1、添加ico图标 资源到资源管理库中

2、参见见代码如下:

HICON hIcon[3];

hIcon[0] = theApp.LoadIcon(IDI_ICON_GREEN_GREEN);

hIcon[1] = theApp.LoadIcon(IDI_ICON_GREEN_GREY);

hIcon[2] = theApp.LoadIcon(IDI_ICON_RED_GREEN);

m_imageList.Create(16, 16, ILC_COLOR32, 3, 3);

for(int i=0; i<3; i++)

m_imageList.Add(hIcon[i]);

m_cTree.SetImageList(&m_imageList, TVSIL_NORMAL);//添加图标到treectrl

HTREEITEM treeRoot = m_cTree.InsertItem(_T("消息"), 0,1 );//选中时显示图标1,未选中时显示图标0

m_cTree.SetItemData(treeRoot,0);

m_cmdNode1 = m_cTree.InsertItem( _T("子消息1"),1,1, treeRoot );

m_cTree.SetItemData(m_cmdNode1,1);

HTREEITEM cmdNode2 = m_cTree.InsertItem( _T("子消息2"),2,2, treeRoot );

m_cTree.SetItemData(cmdNode2,2);

HTREEITEM cmdNode3 = m_cTree.InsertItem( _T("子消息3"), treeRoot );//默认使用了第一个

m_cTree.Expand(treeRoot, TVE_EXPAND);

m_cTree.SetItemData(cmdNode3,3);

二、添加tooltips

1、treectrl 的 ToolTips 属性和 Info Tip 属性设置为 true

也可使用 m_cTree.ModifyStyle(0, TVS_INFOTIP); 进行修改

2、添加treectrl 的 事件处理程序, TVN_GETINFOTIP, 若 不同的 节点想提示不同的信息 可提前绑定不同的值参见代码如下:

void CtreeDlg::OnTvnGetInfoTipTree1(NMHDR *pNMHDR, LRESULT *pResult)

{

TOOLTIPTEXT* pT = (TOOLTIPTEXT*)pNMHDR;

CPoint pt;

GetCursorPos( &pt );

m_cTree.ScreenToClient( &pt );

HTREEITEM hItem = m_cTree.HitTest( pt );

if ( NULL == hItem )

return;

int data = m_cTree.GetItemData(hItem);

CString str("this is tooltips 测试");

switch( data)

{

case 0:

pT->lpszText = _T("0");

break;

case 1:

pT->lpszText = _T("1");

break;

case 2:

pT->lpszText = _T("2");

break;

case 3:

//wcscpy(pT->lpszText,T2W((LPTSTR)str.GetBuffer(NULL)));// CString 转换为 LPWSTR 

//str.ReleaseBuffer();

//或

USES_CONVERSION;

pT->lpszText = (LPWSTR)A2CW(W2A(str));//unicode集

str.ReleaseBuffer();

break;

}

*pResult = 0;

}

你可能感兴趣的:(mfc treectrl 树节点添加 图标 和 tooltips(悬停提示信息))