MFC CTreeCtrl状态图像列表将不起作用 [英] MFC CTreeCtrl state image list won't work
问题描述
我有一个带有树视图的SDI应用程序,其中用户必须能够单击某些项目以禁用/启用它们.我希望在发生这种情况时更改图标.我有一个位图,其中包含多个16x16图像,可以针对不同的状态进行引用.
在
CMyView :: PreCreateWindow(CREATESTRUCT& cs)
{
m_bitmapState.LoadBitmap(IDB_STATE); // 这是CBitMap类型的成员
// 类型为CImageList的成员
m_stateImageList.Create(IDB_STATE, 16 , 2 ,RGB( 255 , 255 , 255 ));
......
}
CMyView::OnInitialUpdate() { CTreeCtrl& tree; tree.SetImageList(&m_stateImageList, TVSIL_STATE); ... }
我尝试了各种方法来显示不同的位图,但没有任何效果
例如
tree.SetItemImage(hti, 0, TVIS_STATEIMAGEMASK);
tree.SetItemState(hti, 1, TVIS_STATEIMAGEMASK);
但没有任何效果-请有人可以告诉我我做错了什么.
我确实通过设置TVS_CHECKBOXES标志使复选框与我自己的图像配合使用,但是我不希望每个树形视图项目都显示该状态-有办法做到这一点,但并不能让每个树形视图都显示该状态-查看项目?
非常感谢您所做的任何事情.
The normal way of doing this is: 0) Create a bit map with 2 bitmap images per each tree item (one for selected state, one for non selected) Define an enum that matches the order of images, for example: enum { TREE_IMAGE_SERVER, TREE_IMAGE_CLIENT, // and so on }; 1) In OnInitialUpdate do this: if (!m_imageList.Create(IDB_IMAGELIST_TREE_SML,16,1,RGB(255,255,255))) { // @todo - handle somehow } // // Now associate with image list with tree control. Not interested in return being // the handle to the previous image list // (void)tree.SetImageList(&m_imageList,TVSIL_NORMAL); 2) When you insert a tree item, do this: tree.InsertItem(ti.m_Name, 2*TREE_IMAGE_SERVER, 2*TREE_IMAGE_SERVER+1); It works.
尝试处理点击(NM_CLICK通知),并可能在树中进行击键(TVN_KEYDOWN通知),并使用 SetItemImage [ ^ ]根据您的需要更改图像.我对状态图像感到满意,您只能设置在选中树项或未选中树项时将其显示在树项旁边的图像,并且树形控件为单选.我认为您可能需要使用以下形式的函数调用将位图添加到图像列表中:
ImageList_Add(hImageList,hBitmap,NULL); // Win32宏
我不确定MFC的等效项是什么,但是应该很容易找到它.
I have an SDI app with a tree view in which the user must be able to click on some items to disable/enable them. I wish to change the icon when this happens. I have a bit map containing multiple 16x16 images which can be referenced for different states.
In
CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
m_bitmapState.LoadBitmap(IDB_STATE);//this is a member of type CBitMap
//a member of type CImageList
m_stateImageList.Create(IDB_STATE, 16, 2, RGB (255, 255, 255));
......
}
CMyView::OnInitialUpdate() { CTreeCtrl& tree; tree.SetImageList(&m_stateImageList, TVSIL_STATE); ... }
I have tried various things to get different bitmas to be shown but nothing works
e.g.
tree.SetItemImage(hti, 0, TVIS_STATEIMAGEMASK);
tree.SetItemState(hti, 1, TVIS_STATEIMAGEMASK);
but nothing works - please could somebody tell me what I am doing wrong.
I did manage to get the checkboxes working with my own images by setting the TVS_CHECKBOXES flag, but I don''t want the state shown by every tree view item - is there a way to do this but not have it shown by every tree-view item?
Many many thanks for anything that gets me a bit further with this.
The normal way of doing this is: 0) Create a bit map with 2 bitmap images per each tree item (one for selected state, one for non selected) Define an enum that matches the order of images, for example: enum { TREE_IMAGE_SERVER, TREE_IMAGE_CLIENT, // and so on }; 1) In OnInitialUpdate do this: if (!m_imageList.Create(IDB_IMAGELIST_TREE_SML,16,1,RGB(255,255,255))) { // @todo - handle somehow } // // Now associate with image list with tree control. Not interested in return being // the handle to the previous image list // (void)tree.SetImageList(&m_imageList,TVSIL_NORMAL); 2) When you insert a tree item, do this: tree.InsertItem(ti.m_Name, 2*TREE_IMAGE_SERVER, 2*TREE_IMAGE_SERVER+1); It works.
Try handling clicks (NM_CLICK notification) and possibly keyhits (TVN_KEYDOWN notification) in the tree and use SetItemImage[^] to change the image according to your needs. I belive with the state images you can only set what image to display next to the tree item when it is selected or when it is not selected, and a tree control is single-select.
I think you may need to add your bitmap into your image list with a function call of the form:
ImageList_Add(hImageList, hBitmap, NULL); // Win32 macro
I''m not sure what the MFC equivalent is but it should be easy to find.
这篇关于MFC CTreeCtrl状态图像列表将不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!