vc++保存和另存为对话框,打开文件对话框

2023-10-16 15:45:04 浏览数 (2)

windows上保存时常用的两个弹窗,一个是选择路径去保存,适用于选择目录,弹窗如图:

 实现代码:

代码语言:javascript复制
//需要添加头文件
//#include <Shlobj.h>	
//#pragma comment(lib,"shell32.lib")
    BROWSEINFO sInfo;
	::ZeroMemory(&sInfo,sizeof(BROWSEINFO));
	sInfo.pidlRoot = 0;
	sInfo.lpszTitle = _T("选择路径");
	sInfo.ulFlags = BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE | BIF_EDITBOX;
	sInfo.lpfn = NULL;
	LPITEMIDLIST lpidlBrowse = SHBrowseForFolder(&sInfo);

	wchar_t pszPath[1024] = {0};
	::SHGetPathFromIDList(lpidlBrowse,pszPath);

另一个是保存到文件的弹窗,常用于另存为,弹窗如图

实现代码:

代码语言:javascript复制
//需要添加头文件
//#include <commdlg.h>	
//#pragma comment(lib,"comdlg32.lib")
		OPENFILENAME ofn;
		ZeroMemory(&ofn, sizeof(ofn));

		TCHAR szFilename[MAX_PATH] = {0};
		ofn.lpstrFile		  = szFilename;
		ofn.nMaxFile		  = MAX_PATH;
		ofn.lpstrFilter		  = _T("Text Files(.txt)*.txtImage Files(.png;jpg;.bmp)*.png;*.jpg;*.bmpAll Files(*.*)*.*");
		ofn.lpstrDefExt		  = _T("txt");
		ofn.lpstrTitle		  = _T("Save To");
		ofn.Flags			  = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
		ofn.lStructSize		  = sizeof(OPENFILENAME);  // No OFN_ENABLEHOOK
		ofn.hwndOwner		  = hWnd;
		if (GetSaveFileName(&ofn))
		{
			::MessageBoxW(hWnd,szFilename,_T("路径"),MB_OK);
		}

 接下来是选择打开文件时的弹窗:(截图就不放了,和另存为那个差别不大)

代码语言:javascript复制
//XP系统就可用
//需要添加头文件
//#include <commdlg.h>	
//#pragma comment(lib,"comdlg32.lib")	
	TCHAR szFilename[1024] = { 0 }; 
	OPENFILENAME ofn = { 0 };  
	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.lpstrFilter = _T("所有文件*.*可执行程序*.exe;*.bat");	//设置过滤  
	ofn.nFilterIndex = 1;
	ofn.lpstrFile = szFilename;
	ofn.nMaxFile = sizeof(szFilename);
	ofn.lpstrInitialDir = NULL; 
	ofn.lpstrTitle = TEXT("请选择可执行程序路径"); 
	ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_NODEREFERENCELINKS;
	ofn.hwndOwner = hWnd;
	if (GetOpenFileName(&ofn))  
	{   
		::MessageBoxW(hWnd,szFilename,_T("路径"),MB_OK);
	}

还有一种在win7及win7以后系统的系统中用的代码:

代码语言:javascript复制
//需要添加头文件
//#include <objbase.h>
//#include <Shlobj.h>	
//#pragma comment(lib,"shell32.lib")	
	wstring wstrFilePath;	
	IFileDialog *pfd = NULL;
	HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd));// 创建com对象
	if (SUCCEEDED(hr))
	{
		DWORD dwFlags;
		hr = pfd->GetOptions(&dwFlags);
		hr = pfd->SetOptions(dwFlags | FOS_FORCEFILESYSTEM | FOS_NODEREFERENCELINKS);
		COMDLG_FILTERSPEC fileType[] =
		{
			{ L"All files", L"*.*" },
		};
		hr = pfd->SetFileTypes(ARRAYSIZE(fileType), fileType);
		hr = pfd->Show(NULL);
		//hr = pfd->SetDefaultExtension(L"exe");
		if (SUCCEEDED(hr))
		{
			IShellItem *pSelItem;
			hr = pfd->GetResult(&pSelItem);
			if (SUCCEEDED(hr))
			{
				LPWSTR pszFilePath = NULL;
				hr = pSelItem->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &pszFilePath);
				wstrFilePath = pszFilePath;
				CoTaskMemFree(pszFilePath);
			}
			if (pSelItem)
				pSelItem->Release();
		}
	}

	if (pfd)
		pfd->Release();

	::MessageBoxW(hWnd,wstrFilePath.c_str(),_T("路径"),MB_OK);;

0 人点赞