调用系统通用对话框打开文件/文件夹

// 文件
void SelectFile(std::string& path)  
{  
    OPENFILENAME open_file_name; // 公共对话框结构。 
    char file[MAX_PATH]; // 保存获取文件名称的缓冲区。 
                                    // 初始化选择文件对话框。       
    ZeroMemory(&open_file_name, sizeof(OPENFILENAME)); 
    open_file_name.lStructSize = sizeof(OPENFILENAME); 
    open_file_name.hwndOwner = 0; 
    open_file_name.lpstrFile = file; 
    open_file_name.lpstrFile[0] = '\0'; 
    open_file_name.nMaxFile = sizeof(file); 
    open_file_name.lpstrFilter = "avi文件(*.*)\0*.avi\0mp4(*.mp4)\0*.mp4\0\0"; 
    open_file_name.nFilterIndex = 1; 
    open_file_name.lpstrFileTitle = 0; 
    open_file_name.nMaxFileTitle = 0; 
    open_file_name.lpstrInitialDir = 0; 
    open_file_name.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; 
    //ofn.lpTemplateName =  MAKEINTRESOURCE(ID_TEMP_DIALOG); 

    // 显示打开选择文件对话框。  
    if (GetOpenFileName(&open_file_name))  
    {  
        path = file; 
    }  
}  

// 文件夹  
void SelectDir(std::string& path)  
{  
    char path_buff[MAX_PATH]; 
    ZeroMemory(path_buff, MAX_PATH); 

    BROWSEINFO browse_info; 
    browse_info.hwndOwner = 0; 
    browse_info.pidlRoot = 0; 
    browse_info.pszDisplayName = 0; 
    browse_info.lpszTitle = "请选择文件夹"; 
    browse_info.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT; 
    browse_info.lpfn = 0; 
    browse_info.lParam = 0; 
    browse_info.iImage = 0; 

    LPITEMIDLIST item_id_list = SHBrowseForFolder(&browse_info); 
    if (item_id_list == 0)  
    {  
        return; 
    }  

    if (SHGetPathFromIDList(item_id_list, path_buff))  
    {  
        path = path_buff; 
    }  
}

你可能感兴趣的:(OPENFILENA,BROWSEINFO)