浏览文件夹对话框
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; // "取消"按钮
}
保存文件对话框
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;
}