0

With the code below, the File Save dialog is displayed as expected, with two buttons: Save and Cancel. Clicking Cancel returns with result=IDCANCEL, but clicking Save or typing Enter does not return from DoModal, just repaints the Filename window. Is there any reason the Save button should not work?

// Code below is in a message handler of a modeless dialog
CString defaultExt, filter;
defaultExt = "fits";
filter = "FITS image Files (*.fits)|*.fits|All image files (*.img; *.fits)|*.img; *.fits|All Files (*.*)|*.*||";
CFileDialog dlg(FALSE/*save as*/, defaultExt, "GeneratedImage", OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST, filter, this);
int result = (int)dlg.DoModal(); // does not return if Save is clicked

The behavior is the same if the optional argument of CFileDialog is bVistaStyle=FALSE.

Visual Studio 2019 v16.7.7, 32-bit debug build, built on and running on 64-bit Windows 7 (same result running on 64-bit Windows 10).

Woody20
  • 791
  • 11
  • 30
  • 1
    `OFN_PATHMUSTEXIST` in a Save dialog looks strange. Did you actually specify a file name before you click Save? – j6t Apr 19 '21 at 20:42
  • The combination, `OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST` would imply that: (a) You can only use an existing file to save into; (b) when you specify such a file, you'll get a popup asking, "Are you sure you want to overwrite this file?" (or something like that). If you don't see the latter, then you haven't given an acceptable filename. – Adrian Mole Apr 19 '21 at 20:51
  • @j6t: The default filename ("GeneratedImage") appears in the filename box. – Woody20 Apr 19 '21 at 20:59
  • @Adrian Mole:The intent was to (1) prompt if user was about to overwrite an existing file; and (2) the directory must exist. But I see that since the directory as already given by the File Save dialog, the flag doesn't make sense. However, removing `OFN_PATHMUSTEXIST` doesn't change the behavior. – Woody20 Apr 19 '21 at 21:04

2 Answers2

0

There's also a problem with your filter string. You shouldn't include spaces inside the pattern string.

oldFilter = "FITS image Files (*.fits)|*.fits|All image files (*.img; *.fits)|*.img; *.fits|All Files (*.*)|*.*||";
--- REMOVE THIS SPACE --------------------------------------------------------------^

newFilter = "FITS image Files (*.fits)|*.fits|All image files (*.img; *.fits)|*.img;*.fits|All Files (*.*)|*.*||";

See the lpstrFilter member documentation here: https://learn.microsoft.com/en-us/windows/win32/api/commdlg/ns-commdlg-openfilenamew#members

  • What the doc says is that you shouldn't include spaces in the pattern, which I haven't. – Woody20 Apr 19 '21 at 23:01
  • Sorry, I missed the space you indicated. I removed it, but this didn't change the behavior. – Woody20 Apr 21 '21 at 16:38
  • NP... We've run into issues in past versions of Windows with spaces in the filter string (typically wouldn't filter the files properly). Could be MFC "fixes" it when passing it to the save dialog. – Chris Burnette Apr 22 '21 at 18:13
0

Found the problem, sort of. Apparently you cannot save a file to a library.

In Windows 7, navigating to Libraries / Documents in the left pane shows "Documents library, includes 2 locations" in the right pane. Then, clicking Save does nothing. If a different Save as type is selected, I can save.

If I navigate to a simple folder, there is no problem; files can always be saved.

CFileDialog is behaving as if libraries are read-only, but dependent on the file type selected from the filter AND the extension chosen in the File name box.

Perhaps someone knows where the functionality of CFileDialog is described in detail.

Woody20
  • 791
  • 11
  • 30