MFC summary (a): MFC file operations, locate and select the summary (3)

    (C) Select the path to save the file

    A. Use SHBrowseForFolder create a dialog function to select a path. The prototype is

                    LPITEMIDLIST SHBrowseForFolder(LPBROWSEINFO lpbi)

Among them, the return value is a pointer to an item identifier list, if a user selects to cancel it, NULL is returned.

As input parameters lpbi BROWSEINFO structure pointer, used in some property dialog box, the structure is as follows.

typedef struct _browseinfo {
    HWND hwndOwner;         //路径选择对话框的父窗口句柄,可设为this->m_hWnd
    LPCITEMIDLIST pidlRoot; //浏览时的初始根目录,设为NULL时为桌面目录
    LPTSTR pszDisplayName;  //用于保存用户选中的路径
    LPCTSTR lpszTitle;      //对话框标题
    UINT ulFlags;           //指定对话框的一些特性,为一些值的组合
    BFFCALLBACK lpfn;       //处理事件的回调函数,一般设为NULL
    LPARAM lParam;          //应用程序传给回调函数的参数,一般设为NULL
    int iImage;             //保存被选取的文件夾的图片索引,一般设为NULL
} BROWSEINFO, *PBROWSEINFO, *LPBROWSEINFO;

 B. When a path is selected using the function SHGetPathFromIDList extracted path selection. The prototype is

        BOOL SHGetPathFromIDList(LPCITEMIDLIST pidl,LPTSTR pszPath)

Wherein the aforementioned input parameters pidl SHBrowseForFolder return value, the selected path is output parameter pszPath. Call successfully returns TRUE.

    C. Application examples.

//更改文件保存路径
void CTestDlg::OnPathselect() 
{
     //调用两个函数SHBrowseForFolder,SHGetPathFromIDList
     LPITEMIDLIST pID;   //定义第一个函数的返回值
     BROWSEINFO lpbi;    //定义其输入值
     char path[MAX_PATH];  //保存路径
     //为lpbi赋值
     memset(&lpbi,0,sizeof(BROWSEINFO));
     lpbi.hwndOwner=this->m_hWnd;	// 父窗口句柄
     lpbi.lpszTitle="请选择保存路径";	// 显示位于对话框左上部的标题
     lpbi.ulFlags=BIF_EDITBOX ;		// 指定对话框的外观和功能的标志
     pID=SHBrowseForFolder(&lpbi);      //选择路径,获取ID
     if(pID!=NULL)    
     {  //如果成功得到
        SHGetPathFromIDList(pID,path);  //获取路径
        GetDlgItem(IDC_PATH)->SetWindowText(path);   //显示路径 
     }
}

(D) select the file

    A. Use CFileDialog class, define an object. Its constructor as follows.

CFileDialog(
   BOOL bOpenFileDialog,          //为TRUE表示“打开”对话框,为FALSE表示“保存”对话框
   LPCTSTR lpszDefExt = NULL,     //指定默认的文件扩展名
   LPCTSTR lpszFileName = NULL,   //指定默认的文件名
   DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,   //指定一些风格
   LPCTSTR lpszFilter = NULL,     //指定可供选择的文件类型和相应的扩展名
   CWnd* pParentWnd = NULL,       //父窗口指针
   DWORD dwSize = 0
);

   B. The format of file types and extensions (parameter lpszFilter)

    And by extension between Type Description ① File | split;

    ② between extension with the same kind of file types; segmentation;

    ③ between each file type with | segmentation;

    ④ the end of the specified use ||

 For example, " the Data Files (* XLC; * XLS.) | * .Xlc ; * .xls | All Files (. * *) | . * * ||." Where blue represents the file type description, pink represents the extension.

   C. After CFileDialog defined class object can be generated dialog box, select the files DoModal function. In the dialog box "OK" to return, you can use the following function to get the path and file name.

    The main function: GetPathName, GetFileName, GetExtName, GetFileTile and so on.

   D. Application class member variables m_ofn can set the initial directory. The dlg.m_ofn.lpstrInitialDir = _T ( "C: \\").

   E. GetStartPosition and applied functions can be implemented GetNextPathName case of selecting a plurality of files.

   F. an example, to select a single file.

//选择发送的文件
void CTestDlg::OnSelectfile() 
{
    CString str;
    CFileDialog dlg(TRUE,NULL,NULL,NULL,
		"DATA Files(*.dat;*.txt)|*.dat;*.txt|All Files(*.*)|*.*||",this);//定义对象
    if(dlg.DoModal()==IDOK)
    {
	str=dlg.GetPathName();    //获取文件名
    }

    UpdateData(TRUE);
    m_nFilePath=str;  //显示路径
    UpdateData(FALSE);
}

(E) to find files in the specified directory

    A. Use CFileFind find class files in the specified directory, the function involved are FindFile and the FindNextFile lookup function, and a function of acquiring file attribute file attribute determination function, see MSDN.

    B. For the lookup function,

BOOL FindFile(
   LPCTSTR pstrName = NULL,  //查找的文件说明
   DWORD dwUnused = 0        //必须为0
);
virtual BOOL FindNextFile(); //返回非0表示还有符合条件的文件,返回0表示是最后一个符合条件的文件 

PstrName need to find the file name can be set to the following:

                               "E:\\VC++\\example.txt"   “E:\\VC++\\ex*.txt”   "E:\\VC++\\*.*"

     C. an example.

CFileFind finder;
BOOL bResult = finder.FindFile(_T("C:\\te*.dat"));
while(bResult)
{
    bResult = finder.FindNextFile();
    cout<<(LPCTSTR)finder.GetFileName()<<endl;
} 


(F) Gets the directory where the executable program

   A. For API function GetCurrentDirectory although the point of view from the literal meaning is to get the current directory, in fact, acquired not execute .exe files are located, but its parent directory. For example, the program is installed on the desktop, the path we have obtained through this function is C: \ Documents and Settings \ Administrator , instead of C: \ Documents and Settings \ Administrator \ Desktop.

   B. To obtain the directory in which the program is executed, you can use the other API functions: GetModuleFileName. With this function to obtain the currently executing program file name (including the full path), then the combined function decomposable _tsplitpath path, file name, extension, and then be combined as required.

   C. Here is a function I use often in your program:

//获取当前程序运行目录
CString GetCurrentDir()
{
	TCHAR	szFull[_MAX_PATH];		//完整路径
	TCHAR	szDrive[_MAX_DRIVE];	        //盘符
	TCHAR	szDir[_MAX_DIR];		//路径

	//获取程序当前执行文件名(包含完整路径)
	GetModuleFileName(NULL, szFull, _MAX_PATH);
	_tsplitpath(szFull, szDrive, szDir, NULL, NULL);

	_tcscpy(szFull, szDrive);		//盘符
	_tcscat(szFull, szDir);			//路径

	return CString(szFull);			//返回路径
}

(Vii) application function on access

      A. For access functions not many people know, this function can be used to determine the main access to the file or folder or exists. The prototype is as follows:

int _access( 
   const char *path, 
   int mode 
);
       B. parameter is used to specify a file path or folder path, mode specifies the mode in which there are the following four:
mode value To determine the mode
00 Determine whether there
02 Determine whether there is a write-only privileges
04 Determine whether there is read-only access
06 Determine whether there is read and write permissions
  When the specified file path, mode can be set to an arbitrary value of the four; when the path specified for the folder, it is determined whether the presence of only.

      C. When used io.h must include the header file.

      D. I have used this function to determine whether a file exists in a folder, to determine the corresponding treatment.


The End. . .


Published 37 original articles · won praise 204 · views 440 000 +

Guess you like

Origin blog.csdn.net/zwgdft/article/details/7165982