MFC 文件夹对话框

浏览文件夹对话框

SHBrowseForFolder
Displays a dialog box that enables the user to select a Shell folder.
SHGetPathFromIDList
Converts an item identifier list to a file system path.

BOOL BrowseFolderControl(HWND hWnd, char *path, int size)  
{  
    BROWSEINFO bi;  
    ZeroMemory(&bi, sizeof(BROWSEINFO));   
    bi.hwndOwner = hWnd; 
    bi.pszDisplayName = path;    
    bi.lpszTitle = TEXT("请选择文件夹");  
    bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_EDITBOX;  
    LPITEMIDLIST pidl = SHBrowseForFolder(&bi); // 调用浏览文件夹对话框
    if (NULL != pidl) { // "确定"按钮
        SHGetPathFromIDList(pidl, path); // 获取选择的路径      
        MessageBox(hWnd, path, "Tips", MB_OK);
        return TRUE;  
    }  
    strcpy(path, "");
    return FALSE; // "取消"按钮
}  

浏览文件夹

打开文件对话框

GetOpenFileName
The GetOpenFileName function creates an Open dialog box that lets the user specify the drive, directory, and the name of a file or set of files to open.

BOOL OpenFileControl(HWND hWnd, char *path, int size) {
    OPENFILENAME ofn;
    ZeroMemory(path, size);
    ZeroMemory(&ofn, sizeof(OPENFILENAME));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = hWnd;
    ofn.lpstrFilter = "JPEG type(*.jpg;*.jpeg)\0*.jpg;*.jpeg\0Bitmap type(*.bmp)\0*.bmp\0所有文件(*.*)\0*.*\0\0"; // 默认打开*.jpg格式文件
    ofn.lpstrFile = path; // 存储打开文件路径
    ofn.nMaxFile = size; // 上述路径buf大小
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_DONTADDTORECENT | OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NONETWORKBUTTON | OFN_PATHMUSTEXIST;
    if (GetOpenFileName(&ofn)) { // "确定"按钮
        MessageBox(hWnd, path, "tips", MB_OK); 
        return TRUE;
    }
    strcpy(path, "");
    return FALSE; // "取消"按钮
}

OpenFileControl

保存文件对话框

GetSaveFileName
The GetSaveFileName function creates a Save dialog box that lets the user specify the drive, directory, and name of a file to save.

BOOL SaveFileControl(HWND hWnd, char *path, int size) {
    OPENFILENAME ofn;
    ZeroMemory(path, size);
    ZeroMemory(&ofn, sizeof(OPENFILENAME));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = hWnd;
    ofn.lpstrFilter = "Auth type(*.auth)\0*.auth\0所有文件(*.*)\0*.*\0\0";  
    ofn.lpstrDefExt = "auth";
    ofn.lpstrFile = path;
    ofn.nMaxFile = size;
    ofn.lpstrInitialDir = NULL; 
    ofn.Flags = OFN_DONTADDTORECENT | OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NONETWORKBUTTON | OFN_PATHMUSTEXIST;
    if (GetSaveFileName(&ofn)) {
        MessageBox(hWnd, path, "tips", MB_OK);
        return TRUE;
    }
    strcpy(path, "");
    return FALSE;
}

SaveFileControl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值