Click here to Skip to main content
15,920,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to create Save As dialog box in vc++(MFC)?
Posted

You could use something like this;

int GetFileName(HWND hwndOwner,char *filename)
{	OPENFILENAME ofn = {0};

	ofn.lStructSize  = sizeof(ofn);
	ofn.Flags        = OFN_EXPLORER | OFN_PATHMUSTEXIST;
	ofn.hInstance    = GetModuleHandle(0);
	ofn.hwndOwner    = hwndOwner;
	ofn.nMaxFile     = MAX_PATH;
	ofn.lpstrFile    = filename;
	ofn.nFilterIndex = 1;
	ofn.lpstrFilter	 = "Text file\0*.txt\0Any file\0*.*\0";
	ofn.lpstrDefExt	 = "txt";
	return GetSaveFileName(&ofn);
}


hwndOwner is a handle to the window that owns the SaveAs dialog and filename is a character array with length MAX_PATH.
Change the lpstrFilter and lpstrDefExt variables to an appropriate file type for your application.
 
Share this answer
 
Comments
Olivier Levrey 29-Mar-11 6:06am    
Good answer. My 5. But I prefer CFileDialog class.
Thaddeus Jones answer is correct. You can also use the CFileDialog class:

For saving:
CFileDialog dlg(FALSE, defaultExtension, NULL, OFN_OVERWRITEPROMPT, "All files|*.*||");
if (dlg.DoModal() != IDOK)
{
    //the user didn't click OK
    return;
}
CString theFileName = dlg.GetPathName();
//save the file...


For loading:
CFileDialog dlg(TRUE, defaultExtension, NULL, OFN_HIDEREADONLY, "All files|*.*||");
if (dlg.DoModal() != IDOK)
{
    //the user didn't click OK
    return;
}
CString theFileName = dlg.GetPathName();
//load the file...
 
Share this answer
 
Comments
virus131 29-Mar-11 6:16am    
thanx...........
[no name] 29-Mar-11 6:29am    
Good answer, this works equally well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900