首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

mfc中读取资料显示在list控件中

2012-12-23 
mfc中读取文件显示在list控件中我建了个list控件,还有个button按钮,想实现点击按钮后当前目录下名为“共享

mfc中读取文件显示在list控件中
我建了个list控件,还有个button按钮,想实现点击按钮后当前目录下名为“共享文件夹”的信息显示在list中。
程序如下:
// findfileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "findfile.h"
#include "findfileDlg.h"

#define MAX_COUNT 1024
//#define MAX_PATH       256
struct   OLDPATH
{
   char m_oldpath[MAX_PATH];
};

OLDPATH   oldpath[10];   //记录上一层文件读取的路径
char          m_flpath[MAX_PATH];   //记录当前文件读取的路径

CButton    m_FileBack;//声明返回上一层控件对象
CListCtrl    m_filelist;    //声明listctrl控件对象

int           m_count[MAX_COUNT]; //用来记录共享文件夹下子文件夹在listctrl控件中显示的行号
int          layernum; //用来记录查询的是第几层
char* des;


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFindfileDlg dialog

CFindfileDlg::CFindfileDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFindfileDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFindfileDlg)
m_Input = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFindfileDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFindfileDlg)
DDX_Control(pDX, IDC_LIST, m_filelist);
DDX_Text(pDX, IDC_INPUT, m_Input);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFindfileDlg, CDialog)
//{{AFX_MSG_MAP(CFindfileDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_FIND, OnFind)
ON_NOTIFY(NM_DBLCLK, IDC_LIST, OnDblclkList)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFindfileDlg message handlers

BOOL CFindfileDlg::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)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);// Set big icon
SetIcon(m_hIcon, FALSE);// Set small icon

// TODO: Add extra initialization here
for (int i=0;i<MAX_COUNT;i++) //如果listctrl中第i行为文件夹则m_count[i]=i,否则仍为-1
{
   m_count[i]=-1;
}
layernum=0;


//初始化listctrl控件

m_filelist.InsertColumn(0,"名称");
m_filelist.InsertColumn(1,"大小");
CRect   rect;
m_filelist.GetClientRect(&rect);
m_filelist.SetColumnWidth(0,rect.Width()/3);
m_filelist.SetColumnWidth(1,rect.Width()/3);

//获取当前目录的路径

des=new char[256];
memset(des,0,256);
GetCurrentDirectory(256,des);
strcat(des,"\\共享文件夹");     //des存储的是你需要读取的文件夹的路径,根据自己的要求进行修改
//MessageBox(des);

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

void CFindfileDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CFindfileDlg::OnPaint() 
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFindfileDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CFindfileDlg::OnFind(char *lpPath) 


{
// TODO: Add your control notification handler code here
/*

MessageBox(s);
 */
CString s;
UpdateData(true);
s=m_Input;
  int i=0;
        m_filelist.DeleteAllItems(); //每次查询前清除上次查询显示在listctrl中的结果

       char szFind[MAX_PATH];
      WIN32_FIND_DATA FindFileData;

      strcpy(szFind,lpPath);
       strcpy(oldpath[layernum].m_oldpath,szFind);// 用oldpath记录第几(layernum)层的文件夹的路径

      strcpy(m_flpath,szFind); //用m_flpath记录当前文件夹的路径
      strcat(szFind,"\\*.*");


    HANDLE hFind=::FindFirstFile(szFind,&FindFileData);
    if(INVALID_HANDLE_VALUE == hFind)    return;
    
    while(TRUE)
    {
        if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            if(FindFileData.cFileName[0]!='.')//查询出的子文件夹信息
            {
                    CString fileName;

                  fileName=FindFileData.cFileName;
                  m_filelist.InsertItem(i,fileName) ;
                   m_count[i]=i;    //记录是listctrl中的第几行
                    i++;
            }
        }
        else //查询出的子文件信息
        {
              CString fileName,fileSizeh,fileSizelow,str,fileSize;

             DWORD HfileSize,LfileSize;
              HfileSize=FindFileData.nFileSizeHigh; //记录文件大小的高32位
               LfileSize=FindFileData.nFileSizeLow;   //记录文件大小的低32位
             fileSizeh.Format("%d",HfileSize);
              fileSizelow.Format("%d",LfileSize);
             str=fileSizeh;
              str+=fileSizelow;
              fileSize.Format("%db",atoi(str));    //文件大小****b
                // AfxMessageBox(fileSize);

           //将文件名和大小显示在listctrl控件中

            fileName=FindFileData.cFileName;


            m_filelist.InsertItem(i,fileName);
             m_filelist.SetItemText(i,1,fileSize);
               i++;
        }
        if(!FindNextFile(hFind,&FindFileData))    break;
    }
           FindClose(hFind);
            layernum++;   //记录查询的是第几层

}


void CFindfileDlg::OnDblclkList(NMHDR* pNMHDR, LRESULT* pResult) 
{
// TODO: Add your control notification handler code here

POSITION pos=m_filelist.GetFirstSelectedItemPosition();
int nItem=m_filelist.GetNextSelectedItem(pos);    //获取双击的行号,从0开始计算

CString listname;
listname=m_filelist.GetItemText(nItem,0);   //得到选中行的文件名

char szFile[MAX_PATH];
strcpy(szFile,m_flpath);

strcat(szFile,"\");
strcat(szFile,listname);               //得到新的查询路径


if(m_count[nItem]==nItem)           //判断选中行是否为文件夹
{
 for(int i=0;i<MAX_COUNT;i++) //将用来标志子文件夹所在行号信息重新置为-1
 m_count[i]= -1;
OnFind(szFile);                             //查询当前文件夹的子目录

}
else
{
// CFile file;
  
}

*pResult = 0;

}

没有错误,但就是不能实现。哪位能帮我改一下,谢谢!
[解决办法]

// findfileDlg.cpp : implementation file 
// 
#include "stdafx.h" 
#include "findfile.h" 
#include "findfileDlg.h" 

#define MAX_COUNT 1024 
//#define MAX_PATH      256 
struct  OLDPATH 

  char m_oldpath[MAX_PATH]; 
};
OLDPATH  oldpath[10];  //记录上一层文件读取的路径 
char          m_flpath[MAX_PATH];  //记录当前文件读取的路径 

CButton    m_FileBack;//声明返回上一层控件对象 
CListCtrl    m_filelist;    //声明listctrl控件对象 

int          m_count[MAX_COUNT]; //用来记录共享文件夹下子文件夹在listctrl控件中显示的行号 
int          layernum; //用来记录查询的是第几层 
char* des; 


#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 

///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 

class CAboutDlg : public CDialog 

public: 
CAboutDlg(); 

// Dialog Data 
//{{AFX_DATA(CAboutDlg) 
enum { IDD = IDD_ABOUTBOX }; 
//}}AFX_DATA 

// ClassWizard generated virtual function overrides 


//{{AFX_VIRTUAL(CAboutDlg) 
protected: 
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
//}}AFX_VIRTUAL 

// Implementation 
protected: 
//{{AFX_MSG(CAboutDlg) 
//}}AFX_MSG 
DECLARE_MESSAGE_MAP() 
}; 

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 

//{{AFX_DATA_INIT(CAboutDlg) 
//}}AFX_DATA_INIT 


void CAboutDlg::DoDataExchange(CDataExchange* pDX) 

CDialog::DoDataExchange(pDX); 
//{{AFX_DATA_MAP(CAboutDlg) 
//}}AFX_DATA_MAP 


BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
//{{AFX_MSG_MAP(CAboutDlg) 
// No message handlers 
//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 

///////////////////////////////////////////////////////////////////////////// 
// CFindfileDlg dialog 

CFindfileDlg::CFindfileDlg(CWnd* pParent /*=NULL*/) 
: CDialog(CFindfileDlg::IDD, pParent) 

//{{AFX_DATA_INIT(CFindfileDlg) 
m_Input = _T(""); 
//}}AFX_DATA_INIT 
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 


void CFindfileDlg::DoDataExchange(CDataExchange* pDX) 

CDialog::DoDataExchange(pDX); 
//{{AFX_DATA_MAP(CFindfileDlg) 
DDX_Control(pDX, IDC_LIST, m_filelist); 
DDX_Text(pDX, IDC_INPUT, m_Input); 
//}}AFX_DATA_MAP 


BEGIN_MESSAGE_MAP(CFindfileDlg, CDialog) 
//{{AFX_MSG_MAP(CFindfileDlg) 
ON_WM_SYSCOMMAND() 
ON_WM_PAINT() 
ON_WM_QUERYDRAGICON() 
ON_BN_CLICKED(IDC_FIND, OnFind) 
ON_NOTIFY(NM_DBLCLK, IDC_LIST, OnDblclkList) 
//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 

///////////////////////////////////////////////////////////////////////////// 
// CFindfileDlg message handlers 

BOOL CFindfileDlg::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) 

CString strAboutMenu; 
strAboutMenu.LoadString(IDS_ABOUTBOX); 
if (!strAboutMenu.IsEmpty()) 

pSysMenu->AppendMenu(MF_SEPARATOR); 
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 



// Set the icon for this dialog.  The framework does this automatically 
//  when the application's main window is not a dialog 
SetIcon(m_hIcon, TRUE); // Set big icon 
SetIcon(m_hIcon, FALSE); // Set small icon 

// TODO: Add extra initialization here 
for (int i=0;i <MAX_COUNT;i++) //如果listctrl中第i行为文件夹则m_count[i]=i,否则仍为-1 

  m_count[i]=-1; 


layernum=0; 


//初始化listctrl控件 

m_filelist.InsertColumn(0,"名称"); 
m_filelist.InsertColumn(1,"大小"); 
CRect  rect; 
m_filelist.GetClientRect(&rect); 
m_filelist.SetColumnWidth(0,rect.Width()/3); 
m_filelist.SetColumnWidth(1,rect.Width()/3); 

//获取当前目录的路径 
des=new char[256]; 
memset(des,0,256); 
GetCurrentDirectory(256,des); 
strcat(des,"\\共享文件夹");    //des存储的是你需要读取的文件夹的路径,根据自己的要求进行修改 
// MessageBox(des); 

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


void CFindfileDlg::OnSysCommand(UINT nID, LPARAM lParam) 

if ((nID & 0xFFF0) == IDM_ABOUTBOX) 

CAboutDlg dlgAbout; 
dlgAbout.DoModal(); 

else 

CDialog::OnSysCommand(nID, lParam); 



// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 

void CFindfileDlg::OnPaint() 

if (IsIconic()) 

CPaintDC dc(this); // device context for painting 

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 

// Center icon in client rectangle 
int cxIcon = GetSystemMetrics(SM_CXICON); 
int cyIcon = GetSystemMetrics(SM_CYICON); 
CRect rect; 
GetClientRect(&rect); 
int x = (rect.Width() - cxIcon + 1) / 2; 
int y = (rect.Height() - cyIcon + 1) / 2; 

// Draw the icon 
dc.DrawIcon(x, y, m_hIcon); 

else 

CDialog::OnPaint(); 



// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CFindfileDlg::OnQueryDragIcon() 

return (HCURSOR) m_hIcon; 


void CFindfileDlg::OnFind(char *lpPath) 

// TODO: Add your control notification handler code here 
/* 

MessageBox(s); 
*/ 
CString s; 
UpdateData(true); 
s=m_Input; 
  int i=0; 
        m_filelist.DeleteAllItems(); //每次查询前清除上次查询显示在listctrl中的结果 

      char szFind[MAX_PATH]; 
      WIN32_FIND_DATA FindFileData; 

      strcpy(szFind,lpPath); 
      strcpy(oldpath[layernum].m_oldpath,szFind);// 用oldpath记录第几(layernum)层的文件夹的路径 

      strcpy(m_flpath,szFind); //用m_flpath记录当前文件夹的路径 
      strcat(szFind,"\\*.*"); 


    HANDLE hFind=::FindFirstFile(szFind,&FindFileData); 


    if(INVALID_HANDLE_VALUE == hFind)    return; 
    
    while(TRUE) 
    { 
        if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
        { 
            if(FindFileData.cFileName[0]!='.')//查询出的子文件夹信息 
            { 
                    CString fileName; 

                  fileName=FindFileData.cFileName; 
                  m_filelist.InsertItem(i,fileName) ; 
                  m_count[i]=i;    //记录是listctrl中的第几行 
                    i++; 
            } 
        } 
        else //查询出的子文件信息 
        { 
              CString fileName,fileSizeh,fileSizelow,str,fileSize; 

            DWORD HfileSize,LfileSize; 
              HfileSize=FindFileData.nFileSizeHigh; //记录文件大小的高32位 
              LfileSize=FindFileData.nFileSizeLow;  //记录文件大小的低32位 
            fileSizeh.Format("%d",HfileSize); 
              fileSizelow.Format("%d",LfileSize); 
            str=fileSizeh; 
              str+=fileSizelow; 
              fileSize.Format("%db",atoi(str));    //文件大小****b 
                // AfxMessageBox(fileSize); 

          //将文件名和大小显示在listctrl控件中 

            fileName=FindFileData.cFileName; 
            m_filelist.InsertItem(i,fileName); 
            m_filelist.SetItemText(i,1,fileSize); 
              i++; 
        } 
        if(!FindNextFile(hFind,&FindFileData))    break; 
    } 
          FindClose(hFind); 
            layernum++;  //记录查询的是第几层 





void CFindfileDlg::OnDblclkList(NMHDR* pNMHDR, LRESULT* pResult) 

// TODO: Add your control notification handler code here 

POSITION pos=m_filelist.GetFirstSelectedItemPosition(); 
int nItem=m_filelist.GetNextSelectedItem(pos);    //获取双击的行号,从0开始计算 

CString listname; 
listname=m_filelist.GetItemText(nItem,0);  //得到选中行的文件名 

char szFile[MAX_PATH]; 
strcpy(szFile,m_flpath); 

strcat(szFile,"\"); 
strcat(szFile,listname);              //得到新的查询路径 


if(m_count[nItem]==nItem)          //判断选中行是否为文件夹 

for(int i=0;i <MAX_COUNT;i++) //将用来标志子文件夹所在行号信息重新置为-1 
m_count[i]= -1; 
OnFind(szFile);                            //查询当前文件夹的子目录 


else 

// CFile file; 
  


*pResult = 0; 



这样比较好看点!
[解决办法]
内容太多  自己先定位一下
[解决办法]
这么长,看不下去了。。。。好像有个Serialize
[解决办法]
求工程。好好研究研究。350146607@qq.com

热点排行