文件打开(保存)对话框:GetOpenFileName和 GetSaveFileName

来源:互联网 发布:网络信息安全事件 编辑:程序博客网 时间:2024/05/17 08:18
void Test() {TCHAR strFilter[]= _T("Bitmap Files(*.bmp)\0*.bmp\0")_T("JPEG Files(*.jpeg;*jpg)\0*.jpeg;*.jpg\0")_T("PNG Files(*.png)\0*.png\0")_T("All Files(*.*)\0*.*\0");TCHAR strFile[MAX_PATH]= _T("未命名");TCHAR strFileTitle[256]= {0};TCHAR strInitiaDir[]= _T("D:\\DeskTop");TCHAR strTitle[]= _T("请选择");OPENFILENAME ofn;         ZeroMemory(&ofn, sizeof(ofn));// 指定结构的大小,以字节为单位ofn.lStructSize= sizeof(ofn);/*Handle to the window that owns the dialog box. This member can be any valid window handle, or it can be NULL if the dialog box has no owner.*/ofn.hwndOwner           = m_hWnd;// Not supported.//ofn.hInstance;// 过滤器字符串的长指针,需以两个空字符结尾ofn.lpstrFilter = strFilter;// Not supported.//ofn.lpstrCustomFilter;// Not supported.//ofn.nMaxCustFilter;// 过滤器序号(由1开始,如果该值为0表示第一个)ofn.nFilterIndex= 3;/*指向包含初始化文件名编辑控件使用的文件名的缓冲。如果不需要初始值,这个缓冲的第一个字符必须是NULL。当GetOpenFileName或GetSaveFileName函数返回成功时,这个缓冲包含驱动器,路径,文件名,及所选择的文件的扩展名。 If the buffer is too small, the function returns FALSE. In this case, the first two bytes of the lpstrFile buffer contain the required size, in bytes or characters.*/ofn.lpstrFile= strFile;/*Specifies the size, in bytes (ANSI version) or 16-bit characters (Unicode version), of the buffer pointed to by lpstrFile. The GetOpenFileName and GetSaveFileName functions return FALSE if the buffer is too small to contain the file information. The buffer should be at least 256 characters long.*/ofn.nMaxFile= sizeof(strFile);// 指向接收选择的文件的文件名和扩展名的缓冲(不带路径信息)。这个成员可以是NULL。ofn.lpstrFileTitle= strFileTitle;/*指定lpstrFileTitle缓冲的大小,以TCHARs为单位。对于ANSI版本,是字节的个数;对于Unicode版本,是字节的个数。如果lpstrFileTitle是NULL,这个成员被忽略。*/ofn.nMaxFileTitle= sizeof(strFileTitle);// 打开对话框的初始目录,可以为NULLofn.lpstrInitialDir= strInitiaDir;// 打开对话框标题栏的提示文字ofn.lpstrTitle= strTitle;/*A bitmask of flags used to initialize the dialog box. When the dialog box returns, it sets these flags to indicate the user's input. This member can be a combination of the following flags.*/ofn.Flags= 0;// 指定从路径开始到通过lpstrFile指定的文件名字符串基于0的偏移ofn.nFileOffset= 0;// 指定从路径开始到通过lpstrFile指定的文件名字符串中扩展名基于0的偏移ofn.nFileExtension= 0;/*指向包含默认扩展名的缓冲。如果用户忘记输入扩展名,GetOpenFileName和GetSaveFileName附加这个扩展名到文件名中。这个字符串可以是任一长度,但但只有头三个字符被附加。字符串不应该包含一个句点(.)。如果这个成员是NULL并且用户忘记了输入一个扩展名,那么将没有扩展名被附加。*/ofn.lpstrDefExt= _T("bmp");//ofn.lCustData;// Not supported.//ofn.lpfnHook;// Not supported.//ofn.lpTemplateName;// Not supported.CString str;if (GetOpenFileName(&ofn)){str = ofn.lpstrFile;AfxMessageBox(str);}if (GetSaveFileName(&ofn)){str = ofn.lpstrFile;AfxMessageBox(str);}}





0 0