Link to home
Start Free TrialLog in
Avatar of Albert-Georg
Albert-Georg

asked on

CFileDialog::DoModal( ) causes window to be gray

Dear experts,

I am writing an MFC application that is dialog-based in Visual C++ (Visual Studio 2012). The dialog has a menu on the top containing an entry to open a file.

When the CFileDialog::DoModal( ) method is called the dialog window (including menu, red 'X' button on top-right) gets gray (which is normal behavior). However, it still is gray when the file dialog is closed.

This is still true when I do not select a file an click 'Cancel' in the file dialog.

Here the code being called when 'File open' is clicked in the menu:

void CAProsterioriDlg::OnFileOpen()
{
	CFileDialog fileDialog( TRUE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, _T( "AProsteriori-Files (*.apr)|*.apr|All Files (*.*)|*.*||" ), this );

	fileDialog.m_ofn.lpstrTitle = _T( "Open AProsteriori-file" );
	

	if (fileDialog.DoModal() == IDOK)
	{
		CString strPath = fileDialog.GetPathName();

		if ( strPath.Find( _T( ":\\\\" ) ) == 1 && strPath.GetLength( ) > 4 )
		{
			// this means we have an invalid path that looks like this:
			// C:\\cda.dgl
			// get rid of extra slash
			CString temp;
			temp = strPath.Left( 3 );
			temp += strPath.Mid( 4 );
			strPath = temp;
		}

		// Let's get it ...
		CFile theFile;
		theFile.Open( strPath, CFile::modeRead );
		CArchive archive( &theFile, CArchive::load );

		Serialize( archive );

		archive.Close();
		theFile.Close();
	}
}

Open in new window


Any help is welcome.

Thanks,

Albert
Avatar of sarabande
sarabande
Flag of Luxembourg image

actually the system  menu should update after the file fialog closed.

did you set the m_pMainWnd in the InitInstance function of your application class?

CAProstioriDlg mydlg;
m_pMainWnd = &mydlg;  // that might help
mydlg.DoModal();

Open in new window


next, you could try is to reset the system menu after the file dialog closed.

void CAProsterioriDlg::OnFileOpen()
{
     ...
     GetSystemMenu(TRUE); // the true should reset the system menu 
}

Open in new window


if nothing works you may explicitly enable the menu item:

CMenu* mnu = this->GetSystemMenu(FALSE);   
mnu->EnableMenuItem( SC_CLOSE, MF_BYCOMMAND);

Open in new window


Sara
Avatar of Albert-Georg
Albert-Georg

ASKER

Dear Sara,

thank you very much for helping. I tried the tips but none solved the problem. It is not only the menu that is grey but also the red field with the cross on the top-right of the main dialog (which closes it).

Update: The problem appears in both release build and debug build. However, when I use the debug build and stop at a breakpoint at an arbitrary position in 'OnFileOpen' and then continue, anything is O.K.

Sincerely,

Albert
I tried playing with this issue earlier this morning.  I didn't have any problems recreating the issue, but I'll be darned if I could find anything that would change the behavior.

I even thought it might be an issue with the Model Dialog popping open in the middle of a menu command, so I even tried changing the code that loads the Model Dialog to POST a user window message (then called the OnOpenFile in response to the user window message) and that didn't change anything.

I stepped through the OnModal logic, and I could see where the logic removes the parent from being enabled, but then saw the shutdown logic attempts to undo that and reenable the parent and set the focus back to the parent.

However, the window would 'fix' itself as soon as I clicked on another application.
Dear  HooKooDooKu,

yes, that is exactly the behavior I experience here.

Albert
It is not only the menu that is grey but also the red field with the cross
both items were bundled. if the one is enabled/disabled the other would be synchronized.

i also assumed that the issue easily could recreated cause i experienced similar errors with modal dialogs myself.

my guess is, that the cause for it is because mfc used a separate message loop for a modal dialog what works well as long as all other modal dialogs are mfc dialogs either. in your case the common file dialog is implemented by an active-x, what means com. i think that is why the "torch" wasn't passed properly from one dialog to the other.

as HooKooDooKu said, when switching between applications all works ok.

i even would think if we could make the dialog aware that the ui might need a refresh after the current message handler (where you called the file dialog) was fully processed, it also should work. so i would suggest you start a (message) timer from your function and then implement OnTimer override to reset the menu of your dialog.

void CAProsterioriDlg::OnFileOpen()
{
     ...
    SetTimer(123, 100, 0);
}

void CAProsterioriDlg::OnTimer(UINT_PTR nIDEvent)
{
     if (nIDEvent == 123)
     {
           GetSystemMenu(TRUE);
           CMenu * pmenu = GetSystemMenu(FALSE);
           if (pmenu != 0)
           {
                   pmenu->EnableMenuItem(SC_CLOSE, TRUE);
                   KillTimer(123);
           }      
     }  
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Albert-Georg
Albert-Georg

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if (fileDialog.DoModal() == IDOK)
{
      --------------
     -------
        -----
      theFile.Close();
}

 AfxGetMainWnd ()->EnableWindow (TRUE);
Dear puranik_p,

this does not work either. I believe it is a bug in CDialogEx.

Sincerely,

Albert
Changing from CDialogEx to CDialog solved the problem.