-2
 **FileEnumTreeControl.cpp file**

// FileEnumTreeControl.cpp : Defines the class behaviors for the 
   application.
//

#include "stdafx.h"
#include "FileEnumTreeControl.h"
#include "FileEnumTreeControlDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CFileEnumTreeControlApp

BEGIN_MESSAGE_MAP(CFileEnumTreeControlApp, CWinApp)
    ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()


// CFileEnumTreeControlApp construction

CFileEnumTreeControlApp::CFileEnumTreeControlApp()
{
    // TODO: add construction code here,
    // Place all significant initialization in InitInstance
}


// The one and only CFileEnumTreeControlApp object

CFileEnumTreeControlApp theApp;


// CFileEnumTreeControlApp initialization

BOOL CFileEnumTreeControlApp::InitInstance()
{
    // InitCommonControlsEx() is required on Windows XP if an application
    // manifest specifies use of ComCtl32.dll version 6 or later to enable
    // visual styles.  Otherwise, any window creation will fail.
    INITCOMMONCONTROLSEX InitCtrls;
    InitCtrls.dwSize = sizeof(InitCtrls);
    // Set this to include all the common control classes you want to use
    // in your application.
    InitCtrls.dwICC = ICC_WIN95_CLASSES;
    InitCommonControlsEx(&InitCtrls);

    CWinApp::InitInstance();

    AfxEnableControlContainer();

    // Standard initialization
    // If you are not using these features and wish to reduce the size
    // of your final executable, you should remove from the following
    // the specific initialization routines you do not need
    // Change the registry key under which our settings are stored
    // TODO: You should modify this string to be something appropriate
    // such as the name of your company or organization
    SetRegistryKey(_T("Local AppWizard-Generated Applications"));

    CFileEnumTreeControlDlg dlg;
    m_pMainWnd = &dlg;
    INT_PTR nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
        // TODO: Place code here to handle when the dialog is
        //  dismissed with OK
    }
    else if (nResponse == IDCANCEL)
    {
        // TODO: Place code here to handle when the dialog is
        //  dismissed with Cancel
    }

    // Since the dialog has been closed, return FALSE so that we exit the
    //  application, rather than start the application's message pump.
    return FALSE;
}
 **FileEnumTreeControlDlg.cpp file**
// FileEnumTreeControlDlg.cpp : implementation file
//

#include "stdafx.h"
#include <string>
#include <iostream>
#include "FileEnumTreeControl.h"
#include "FileEnumTreeControlDlg.h"
#include<windows.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CFileEnumTreeControlDlg dialog




CFileEnumTreeControlDlg::CFileEnumTreeControlDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CFileEnumTreeControlDlg::IDD, pParent)
    , m_strTree(_T(""))
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFileEnumTreeControlDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_STATIC_TXT, m_strTree);
    DDX_Control(pDX, IDC_TREE1, m_treeCtrl);
}

BEGIN_MESSAGE_MAP(CFileEnumTreeControlDlg, CDialog)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()


// CFileEnumTreeControlDlg message handlers

BOOL CFileEnumTreeControlDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // 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
    WIN32_FIND_DATA data;
    HTREEITEM hItem, hCar;

    HANDLE find=FindFirstFile(L"D:\\*",&data);
    hItem = m_treeCtrl.InsertItem(L"D", TVI_ROOT);
    if(find!=INVALID_HANDLE_VALUE)
    {
        do
        {         
            hCar = m_treeCtrl.InsertItem(data.cFileName, hItem);
             if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
             {
                 WIN32_FIND_DATA data2;
                 wchar_t* dir=L"D:\\";
                 wcsncat(dir,data.cFileName,wcslen(data.cFileName)-4);  
                 wcsncat(dir,L"\\*",3); 
                 HANDLE find2=FindFirstFile(dir,&data2);    
                 if(find2!=INVALID_HANDLE_VALUE)
                 {
                     do{
                         m_treeCtrl.InsertItem(data2.cFileName,hCar);

                     }while(FindNextFile(find2,&data2));
                 }
              }

        }while(FindNextFile(find,&data));
        FindClose(find);
    }
    // TODO: Add extra initialization here

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

// 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 CFileEnumTreeControlDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the 
 user drags
//  the minimized window.
HCURSOR CFileEnumTreeControlDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}

**FileEnumTreeControl.h : main header file for the PROJECT_NAME 
 application**
//

#pragma once

#ifndef __AFXWIN_H__
    #error "include 'stdafx.h' before including this file for PCH"
#endif

#include "resource.h"       // main symbols


// CFileEnumTreeControlApp:
// See FileEnumTreeControl.cpp for the implementation of this class
//

class CFileEnumTreeControlApp : public CWinApp
{
public:
    CFileEnumTreeControlApp();

// Overrides
    public:
    virtual BOOL InitInstance();

// Implementation

    DECLARE_MESSAGE_MAP()
};

extern CFileEnumTreeControlApp theApp;

FileEnumTreeControlDlg.h : 头文件 //

#pragma once
#include "afxcmn.h"


// CFileEnumTreeControlDlg dialog
class CFileEnumTreeControlDlg : public CDialog
{
// Construction
public:
    CFileEnumTreeControlDlg(CWnd* pParent = NULL);  // standard constructor

// Dialog Data
    enum { IDD = IDD_FILEENUMTREECONTROL_DIALOG };

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support


// Implementation
protected:
    HICON m_hIcon;

    // Generated message map functions
    virtual BOOL OnInitDialog();
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    DECLARE_MESSAGE_MAP()
public:
    CString m_strTree;
    CTreeCtrl m_treeCtrl;
};

FileEnumTreeControl.h:PROJECT_NAME 应用程序的主头文件

#pragma once

#ifndef __AFXWIN_H__
    #error "include 'stdafx.h' before including this file for PCH"
#endif

#include "resource.h"       // main symbols


// CFileEnumTreeControlApp:
// See FileEnumTreeControl.cpp for the implementation of this class
//

class CFileEnumTreeControlApp : public CWinApp
{
public:
    CFileEnumTreeControlApp();

// Overrides
    public:
    virtual BOOL InitInstance();

// Implementation

    DECLARE_MESSAGE_MAP()
};

extern CFileEnumTreeControlApp theApp;

该程序是在 mfc 中创建的,一切正常,但我无法扩展内部目录,因为无法提供 FindFirstFile() 的完整路径,请告诉我如何将动态路径传递给 FindFirstFile()

4

1 回答 1

1

让我猜猜,这行出错了:

wchar_t* dir=L"D:\\";
wcsncat(dir,data.cFileName,wcslen(data.cFileName)-4);  

那是因为您已将 dir 定义为 wchar*,它指向包含“D:\”的静态内存块,并且在将其余字符串连接到其中之后没有足够的可用空间。

修复它的最简单方法是将 dir 的定义替换为堆栈分配的数组,例如

wchar_t dir[MAX_PATH] 

这会分配一长块空白空间,然后您可以将目录名称复制到其中。它还有一个优点是当程序退出代码块时它会自动释放。您还应该使用 wcsncat 的第三个参数作为缓冲区的大小,而不是要复制的字符串的大小。仔细阅读函数文档。

如果您是 C/C++ 新手,则需要先阅读内存分配、字符串、数组和缓冲区,然后再继续学习,否则您会犯大错。

于 2017-08-19T15:41:30.193 回答