这里只提供代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
//打开文件 BOOL OpenFile(CString& strPath) { //定义相应变量 OPENFILENAME ofn; wchar_t file[MAX_PATH] = TEXT(""); //对变量进行初始化 - 设置相应属性 ZeroMemory(&ofn, sizeof(ofn)); ZeroMemory(file, MAX_PATH); ofn.lStructSize = sizeof(OPENFILENAME); ofn.lpstrFilter = TEXT("音乐文件 (wav/mp3/flac)\0*.wav;*.mp3;*.flac\0All files\0*.*\0\0"); ofn.lpstrFile = file; ofn.nMaxFile = MAX_PATH; ofn.Flags = OFN_FILEMUSTEXIST; //选择文件 if(!GetOpenFileName(&ofn)) { return FALSE; } strPath = ofn.lpstrFile; #ifdef DEBUG //弹框提示文件信息 MessageBox(NULL, strPath, TEXT("文件名称"), MB_OK); #endif return TRUE; } //选择文件夹 BOOL SelectDir(HWND hWnd, CString &strPath) { //定义相应变量 BROWSEINFO bifolder; LPITEMIDLIST idl; WCHAR FileName[MAX_PATH]; //初始化变量 ZeroMemory(&bifolder, sizeof(BROWSEINFO)); ZeroMemory(&FileName, MAX_PATH); bifolder.hwndOwner = hWnd; // 拥有者句柄 bifolder.pszDisplayName = FileName; // 存放目录路径缓冲区 bifolder.lpszTitle = TEXT("请选择文件夹"); // 标题 bifolder.ulFlags = BIF_NEWDIALOGSTYLE | BIF_EDITBOX; // 新的样式,带编辑框 if(!(idl = SHBrowseForFolder(&bifolder))) { return FALSE; } SHGetPathFromIDList(idl, FileName); strPath = FileName; #ifdef DEBUG //弹框提示文件信息 MessageBox(NULL, strPath, TEXT("文件夹名称"), MB_OK); #endif return TRUE; } |
使用头文件如下:
#include “shlwapi.h”
#include “shlobj.h”
#include <windows.h>
#include <atlstr.h>
#pragma comment(lib, “shlwapi.lib”)
【Win32】打开文件,选择文件夹 的实现