BOOL CDialogModalless::OnInitDialog()
{
...................
m_image.Create(16,16,0,3,3);
//建立一个图象列表
hIcon[0]=AfxGetApp()->LoadIcon(IDB_DESKTOP);
hIcon[1]=AfxGetApp()->LoadIcon(IDB_HD);
hIcon[2]=AfxGetApp()->LoadIcon(IDB_FOLDER);
for(int i=0;i<3;i++)
m_image.Add(hIcon[i]);
m_Tree.SetImageList(&m_image,LVSIL_NORMAL);
GetDriveName();
}
void CDialogModalless::GetDriveName()
{
TreeItem.hParent=TVI_ROOT; //父级为根目录
TreeItem.item.mask=TVIF_TEXT | TVIF_IMAGE;
TreeItem.item.iImage=IDB_DESKTOP;
TreeItem.item.pszText="本地硬盘驱动器:";
HTREEITEM rootItem=m_Tree.InsertItem(&TreeItem);
}
为什么不对?我想在不同节点插入不同的图标,该怎么办?
5 个解决方案
#1
iImage参数应该设置图像在ImageList中的索引,而不是资源ID,另外,也应该同时设置选中图像
#2
经过修改后,又出现新的问题:只有C盘是对的,D,E,F盘的位图怎么还是IDB_DESKTOP?
void CDialogModalless::GetDriveName()
{
DWORD dwDrives=GetLogicalDrives();
char szDrive[20],DriveName[10];
TreeItem.hParent=TVI_ROOT; //父级为根目录
TreeItem.item.mask=TVIF_TEXT | TVIF_IMAGE |TVIF_SELECTEDIMAGE;
TreeItem.item.pszText="本地硬盘驱动器:";
TreeItem.item.iImage=IDB_DESKTOP-IDB_DESKTOP;
TreeItem.item.iSelectedImage=TreeItem.item.iImage;
HTREEITEM rootItem=m_Tree.InsertItem(&TreeItem); //插入Tree控件中
..............................
HTREEITEM cItem;
for(int i=0;i<4;i++)
{
//all drives have this as their parent
TreeItem.hParent=rootItem; //保证每个硬盘驱动器的父项均为“本地硬盘驱动器:”
szDrive[0]=DriveName[0]='C'+(char)i;
if(dwDrives & 1)
{
TreeItem.item.pszText=(LPSTR) DriveName;
ASSERT(strlen(szDrive)==3);
TreeItem.item.iImage=IDB_HD-IDB_DESKTOP; TreeItem.item.iSelectedImage=TreeItem.item.iImage;
nowItem=m_Tree.InsertItem(&TreeItem);
if(i==0) cItem=nowItem;
}
dwDrives=dwDrives>>1;
_chdir(szDrive);
GetFileList();
}
}
void CDialogModalless::GetDriveName()
{
DWORD dwDrives=GetLogicalDrives();
char szDrive[20],DriveName[10];
TreeItem.hParent=TVI_ROOT; //父级为根目录
TreeItem.item.mask=TVIF_TEXT | TVIF_IMAGE |TVIF_SELECTEDIMAGE;
TreeItem.item.pszText="本地硬盘驱动器:";
TreeItem.item.iImage=IDB_DESKTOP-IDB_DESKTOP;
TreeItem.item.iSelectedImage=TreeItem.item.iImage;
HTREEITEM rootItem=m_Tree.InsertItem(&TreeItem); //插入Tree控件中
..............................
HTREEITEM cItem;
for(int i=0;i<4;i++)
{
//all drives have this as their parent
TreeItem.hParent=rootItem; //保证每个硬盘驱动器的父项均为“本地硬盘驱动器:”
szDrive[0]=DriveName[0]='C'+(char)i;
if(dwDrives & 1)
{
TreeItem.item.pszText=(LPSTR) DriveName;
ASSERT(strlen(szDrive)==3);
TreeItem.item.iImage=IDB_HD-IDB_DESKTOP; TreeItem.item.iSelectedImage=TreeItem.item.iImage;
nowItem=m_Tree.InsertItem(&TreeItem);
if(i==0) cItem=nowItem;
}
dwDrives=dwDrives>>1;
_chdir(szDrive);
GetFileList();
}
}
#3
爲什麽不用System的ImageList???
#4
BOOL CDialogModalless::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
CWaitCursor cWait; //CWaitCursor总被定义为局部变量,当进行冗长的操作时,显示出一个沙漏表示等待光标
m_List.InsertColumn(0,"目录名",LVCFMT_CENTER,120,-1); //往List控件中插入一列
m_List.InsertColumn(1,"文件名",LVCFMT_CENTER,120,-1);
ListView_SetExtendedListViewStyle(m_List, LVS_EX_GRIDLINES); //在CListCtrl中显示表格线
m_image.Create(16,16,TRUE,3,3); //创建图象序列
// the 3 images have conseq resource numbers
// see resource.h
/*#define IDB_DESKTOP 133
#define IDB_HD 134
#define IDB_FOLDER 135*/
CBitmap bitmap;
for(int i=IDB_DESKTOP;i<=IDB_FOLDER;i++)
{
bitmap.LoadBitmap(i);
m_image.Add(&bitmap,(COLORREF)0xFFFFFF);
bitmap.DeleteObject();
}
m_Tree.SetImageList(&m_image,TVSIL_NORMAL);
GetDriveName();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
CWaitCursor cWait; //CWaitCursor总被定义为局部变量,当进行冗长的操作时,显示出一个沙漏表示等待光标
m_List.InsertColumn(0,"目录名",LVCFMT_CENTER,120,-1); //往List控件中插入一列
m_List.InsertColumn(1,"文件名",LVCFMT_CENTER,120,-1);
ListView_SetExtendedListViewStyle(m_List, LVS_EX_GRIDLINES); //在CListCtrl中显示表格线
m_image.Create(16,16,TRUE,3,3); //创建图象序列
// the 3 images have conseq resource numbers
// see resource.h
/*#define IDB_DESKTOP 133
#define IDB_HD 134
#define IDB_FOLDER 135*/
CBitmap bitmap;
for(int i=IDB_DESKTOP;i<=IDB_FOLDER;i++)
{
bitmap.LoadBitmap(i);
m_image.Add(&bitmap,(COLORREF)0xFFFFFF);
bitmap.DeleteObject();
}
m_Tree.SetImageList(&m_image,TVSIL_NORMAL);
GetDriveName();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
#5
IDB_HD-IDB_DESKTOP
这样写不能保证正确,还是直接写imaglist中得索引比较保险
这样写不能保证正确,还是直接写imaglist中得索引比较保险
#1
iImage参数应该设置图像在ImageList中的索引,而不是资源ID,另外,也应该同时设置选中图像
#2
经过修改后,又出现新的问题:只有C盘是对的,D,E,F盘的位图怎么还是IDB_DESKTOP?
void CDialogModalless::GetDriveName()
{
DWORD dwDrives=GetLogicalDrives();
char szDrive[20],DriveName[10];
TreeItem.hParent=TVI_ROOT; //父级为根目录
TreeItem.item.mask=TVIF_TEXT | TVIF_IMAGE |TVIF_SELECTEDIMAGE;
TreeItem.item.pszText="本地硬盘驱动器:";
TreeItem.item.iImage=IDB_DESKTOP-IDB_DESKTOP;
TreeItem.item.iSelectedImage=TreeItem.item.iImage;
HTREEITEM rootItem=m_Tree.InsertItem(&TreeItem); //插入Tree控件中
..............................
HTREEITEM cItem;
for(int i=0;i<4;i++)
{
//all drives have this as their parent
TreeItem.hParent=rootItem; //保证每个硬盘驱动器的父项均为“本地硬盘驱动器:”
szDrive[0]=DriveName[0]='C'+(char)i;
if(dwDrives & 1)
{
TreeItem.item.pszText=(LPSTR) DriveName;
ASSERT(strlen(szDrive)==3);
TreeItem.item.iImage=IDB_HD-IDB_DESKTOP; TreeItem.item.iSelectedImage=TreeItem.item.iImage;
nowItem=m_Tree.InsertItem(&TreeItem);
if(i==0) cItem=nowItem;
}
dwDrives=dwDrives>>1;
_chdir(szDrive);
GetFileList();
}
}
void CDialogModalless::GetDriveName()
{
DWORD dwDrives=GetLogicalDrives();
char szDrive[20],DriveName[10];
TreeItem.hParent=TVI_ROOT; //父级为根目录
TreeItem.item.mask=TVIF_TEXT | TVIF_IMAGE |TVIF_SELECTEDIMAGE;
TreeItem.item.pszText="本地硬盘驱动器:";
TreeItem.item.iImage=IDB_DESKTOP-IDB_DESKTOP;
TreeItem.item.iSelectedImage=TreeItem.item.iImage;
HTREEITEM rootItem=m_Tree.InsertItem(&TreeItem); //插入Tree控件中
..............................
HTREEITEM cItem;
for(int i=0;i<4;i++)
{
//all drives have this as their parent
TreeItem.hParent=rootItem; //保证每个硬盘驱动器的父项均为“本地硬盘驱动器:”
szDrive[0]=DriveName[0]='C'+(char)i;
if(dwDrives & 1)
{
TreeItem.item.pszText=(LPSTR) DriveName;
ASSERT(strlen(szDrive)==3);
TreeItem.item.iImage=IDB_HD-IDB_DESKTOP; TreeItem.item.iSelectedImage=TreeItem.item.iImage;
nowItem=m_Tree.InsertItem(&TreeItem);
if(i==0) cItem=nowItem;
}
dwDrives=dwDrives>>1;
_chdir(szDrive);
GetFileList();
}
}
#3
爲什麽不用System的ImageList???
#4
BOOL CDialogModalless::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
CWaitCursor cWait; //CWaitCursor总被定义为局部变量,当进行冗长的操作时,显示出一个沙漏表示等待光标
m_List.InsertColumn(0,"目录名",LVCFMT_CENTER,120,-1); //往List控件中插入一列
m_List.InsertColumn(1,"文件名",LVCFMT_CENTER,120,-1);
ListView_SetExtendedListViewStyle(m_List, LVS_EX_GRIDLINES); //在CListCtrl中显示表格线
m_image.Create(16,16,TRUE,3,3); //创建图象序列
// the 3 images have conseq resource numbers
// see resource.h
/*#define IDB_DESKTOP 133
#define IDB_HD 134
#define IDB_FOLDER 135*/
CBitmap bitmap;
for(int i=IDB_DESKTOP;i<=IDB_FOLDER;i++)
{
bitmap.LoadBitmap(i);
m_image.Add(&bitmap,(COLORREF)0xFFFFFF);
bitmap.DeleteObject();
}
m_Tree.SetImageList(&m_image,TVSIL_NORMAL);
GetDriveName();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
CWaitCursor cWait; //CWaitCursor总被定义为局部变量,当进行冗长的操作时,显示出一个沙漏表示等待光标
m_List.InsertColumn(0,"目录名",LVCFMT_CENTER,120,-1); //往List控件中插入一列
m_List.InsertColumn(1,"文件名",LVCFMT_CENTER,120,-1);
ListView_SetExtendedListViewStyle(m_List, LVS_EX_GRIDLINES); //在CListCtrl中显示表格线
m_image.Create(16,16,TRUE,3,3); //创建图象序列
// the 3 images have conseq resource numbers
// see resource.h
/*#define IDB_DESKTOP 133
#define IDB_HD 134
#define IDB_FOLDER 135*/
CBitmap bitmap;
for(int i=IDB_DESKTOP;i<=IDB_FOLDER;i++)
{
bitmap.LoadBitmap(i);
m_image.Add(&bitmap,(COLORREF)0xFFFFFF);
bitmap.DeleteObject();
}
m_Tree.SetImageList(&m_image,TVSIL_NORMAL);
GetDriveName();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
#5
IDB_HD-IDB_DESKTOP
这样写不能保证正确,还是直接写imaglist中得索引比较保险
这样写不能保证正确,还是直接写imaglist中得索引比较保险