MFC学习记录 2.CTreeCtrl与CImageList
续1
在对话框头文件中添加私有成员
private:
CImageList m_iml;
1.初始化CImageList、设置图像列表
成员函数:
CImageList::Create
CTreeCtrl::SetImageList
示例:
BOOL CMfc1Dlg::OnInitDialog(){
//...
m_iml.Create(16,16,ILC_COLOR16 | ILC_MASK,20,20);
m_Tree.SetImageList(&m_iml,TVSIL_NORMAL);
}
2.向树视图中添加窗口列表
成员函数:
CImageList::Replace
CTreeCtrl::InsertItem
示例:
int i=0;
HWND hDesktop=::GetDesktopWindow();
HWND hWnd=::GetWindow(hDesktop,GW_CHILD);
TCHAR sTitle[MAX_PATH];
HICON hIcon;
BOOL bShow=TRUE;
LONG idx;
while ( i<1000 && hWnd!=NULL ) {
bShow=TRUE;
if (!::GetWindowTextLength(hWnd)) bShow=FALSE;
if (!::IsWindowEnabled(hWnd)) bShow=FALSE;
if (!::IsWindowVisible(hWnd)) bShow=FALSE;
if (!::GetWindow(hWnd,GW_CHILD)) bShow=FALSE;
if (bShow) {
::GetWindowText(hWnd,sTitle,MAX_PATH);
hIcon=(HICON)::SendMessage(hWnd,WM_GETICON,ICON_SMALL,NULL);
if ((LONG)hIcon==NULL)
hIcon=(HICON)::SendMessage(hWnd,WM_GETICON,ICON_BIG,NULL);
if ((LONG)hIcon==NULL)
hIcon=m_hIcon;
idx=m_iml.Replace(-1,hIcon);
m_Tree.InsertItem(sTitle,idx,idx);
}
hWnd=::GetWindow(hWnd,GW_HWNDNEXT);
++i;
}