学习VC中组合框、树控件、列表框及列表控件的一些用法

Eddy 发布于2011-4-29 14:44:22 分类: 程序设计 已浏览loading 网友评论2条 我要评论

具体用法可以看MSDN,或者Google之,这里主要贴点相关的代码:

//对话框初始化

 

BOOL CListDlg::OnInitDialog()

{

CDialog::OnInitDialog();

 

// Add "About..." menu item to system menu.

 

// IDM_ABOUTBOX must be in the system command range.

ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);

ASSERT(IDM_ABOUTBOX < 0xF000);

 

CMenu* pSysMenu = GetSystemMenu(FALSE);

if (pSysMenu != NULL)

{

//省略一些代码

// TODO: Add extra initialization here

PopulateCombo();

m_DirDetails.InsertColumn(0,"Directory",LVCFMT_LEFT,150);

m_DirDetails.InsertColumn(1,"Files",LVCFMT_RIGHT,100);

m_DirDetails.InsertColumn(2,"Size KB",LVCFMT_RIGHT,100);

return TRUE;  // return TRUE  unless you set the focus to a control

}

 

//添加项目至组合框

void CListDlg::PopulateCombo()

{

TCHAR szBuffer[MAX_PATH];

GetWindowsDirectory(szBuffer,MAX_PATH);

m_cbMainDir.AddString(szBuffer);

 

szBuffer[2]=0;

m_cbMainDir.AddString(szBuffer);

 

GetSystemDirectory(szBuffer,MAX_PATH);

m_cbMainDir.AddString(szBuffer);

 

GetCurrentDirectory(MAX_PATH,szBuffer);

m_cbMainDir.AddString(szBuffer);

 

m_cbMainDir.SetCurSel(0);

}

 

//响应组合框

void CListDlg::OnSelchangeMainDir() 

{

// TODO: Add your control notification handler code here

int nIndex=m_cbMainDir.GetCurSel();

 

if (nIndex!=CB_ERR)

{

m_cbMainDir.GetLBText(nIndex,m_strMainDIr);

PopulateTree();

PopulateListBox();

}

}

 

//添加项目至树控件

void CListDlg::PopulateTree()

{

m_treeFiles.DeleteAllItems();

 

HTREEITEM hLetter[27];

for (int nChar='A';nChar<='Z';nChar++)

{

hLetter[nChar-'A']=m_treeFiles.InsertItem((TCHAR*)&nChar);

}

 

hLetter[26]=m_treeFiles.InsertItem("Other");

 

HANDLE hFind;

WIN32_FIND_DATA dataFind;

BOOL bMoreFiles=TRUE;

CString strFile;

 

hFind=FindFirstFile(m_strMainDIr+"\\*.*",&dataFind);

while (hFind!=INVALID_HANDLE_VALUE && bMoreFiles ==TRUE)

{

if (dataFind.dwFileAttributes==FILE_ATTRIBUTE_ARCHIVE)

{

int nChar=dataFind.cFileName[0];

if (islower(nChar))

{

nChar-=32;

}

if (isalpha(nChar))

{

nChar-='A';

}

else

{

nChar=26;

}

 

m_treeFiles.InsertItem(dataFind.cFileName,hLetter[nChar]);

}

bMoreFiles=FindNextFile(hFind,&dataFind);

}

FindClose(hFind);

 

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

{

m_treeFiles.Expand(hLetter[i],TVE_EXPAND);

}

}

 

//添加项目至列表框

void CListDlg::PopulateListBox()

{

m_lbSubDirs.ResetContent();

 

HANDLE hFind;

WIN32_FIND_DATA dataFind;

BOOL bMoreFiles=TRUE;

 

hFind=FindFirstFile(m_strMainDIr + "\\*.*",&dataFind);

 

while (hFind!=INVALID_HANDLE_VALUE && bMoreFiles==TRUE)

{

if (dataFind.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY)

{

if (strcmp(dataFind.cFileName,"."))

{

if (strcmp(dataFind.cFileName,".."))

{

m_lbSubDirs.AddString(dataFind.cFileName);

}

}

}

 

bMoreFiles=FindNextFile(hFind,&dataFind);

}

FindClose(hFind);

}

 

//响应列表框

void CListDlg::OnSelchangeSubDirs() 

{

// TODO: Add your control notification handler code here

int nSelCount=m_lbSubDirs.GetSelCount();

 

m_strList.RemoveAll();

if (nSelCount)

{

CString str;

 

LPINT pItems= new int[nSelCount];

m_lbSubDirs.GetSelItems(nSelCount,pItems);

 

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

{

m_lbSubDirs.GetText(pItems[i],str);

m_strList.AddTail(str);

}

 

delete [] pItems;

}

PopulateListContol();

}

 

//添加项目至列表控件

void CListDlg::PopulateListContol()

{

m_DirDetails.DeleteAllItems();

POSITION i;

for (i=m_strList.GetHeadPosition();i!=NULL;)

{

int nItem;

HANDLE hFind;

WIN32_FIND_DATA dataFind;

BOOL bMoreFiles=TRUE;

CString str;

CString strFind;

 

str=m_strList.GetAt(i);

nItem=m_DirDetails.InsertItem(0,str);

strFind=m_strMainDIr+"\\"+str+"\\*.*";

 

int nFileCount=0;

double nFileSize=0;

hFind=FindFirstFile(strFind,&dataFind);

while (hFind!=INVALID_HANDLE_VALUE && bMoreFiles==TRUE)

{

if (dataFind.dwFileAttributes==FILE_ATTRIBUTE_ARCHIVE)

{

nFileCount++;

nFileSize+=dataFind.nFileSizeHigh*MAXDWORD+dataFind.nFileSizeLow;

}

bMoreFiles=FindNextFile(hFind,&dataFind);

}

FindClose(hFind);

 

str.Format("%ld",nFileCount);

m_DirDetails.SetItemText(nItem,1,str);

 

str.Format("%1.2f",nFileSize/1024.0);

m_DirDetails.SetItemText(nItem,2,str);

 

m_strList.GetNext(i);

}

 

代码下载:

http://u.115.com/file/f16af44073#

List.zip

 

已经有(2)位网友发表了评论,你也评一评吧!
原创文章如转载,请注明:转载自Eddy Blog
原文地址:http://www.rrgod.com/program/769.html     欢迎订阅Eddy Blog

关于 组合框  树控件  列表框  列表控件  的相关文章

  1. 发表于2011-5-17 17:54:24

    哈哈,又学到了哦。。。

  2. 发表于2013-8-5 14:55:55

    本人手中有各种VC自制美化控件源代码,并有详细的制作方法介绍,简单,简捷,易学
    按钮:1元
    菜单:2元
    组合框:3元
    树控件:4元
    如有需要还可以定制,可以先看效果
    联系人:刘先生 电话:13545974168

记住我的信息,下次不用再输入 欢迎给Eddy Blog留言