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
LVL 1
Albert-GeorgAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sarabandeCommented:
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
0
Albert-GeorgAuthor Commented:
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
0
HooKooDooKuCommented:
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.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Albert-GeorgAuthor Commented:
Dear  HooKooDooKu,

yes, that is exactly the behavior I experience here.

Albert
0
sarabandeCommented:
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

0
Albert-GeorgAuthor Commented:
Dear Sara and HooKooDooKu,

I was wondering that I had a similar application that does not show the error. So I systematically compared the differences between this working application and the application with the error. I found the most obvious difference was that the main dialog of the working application was based on CDialog while the main dialog of the new application was based on CDialogEx (I think Microsoft changed the default for dialog-based applications from CDialog to CDialogEx with Visual Studio 2010).

Thus I changed the base class of the new application to CDialog since I have no need for the enhanced features of CDialogEx. This solved the problem.

Thanks again for thinking with me about a solution.

Albert
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
puranik_pCommented:
if (fileDialog.DoModal() == IDOK)
{
      --------------
     -------
        -----
      theFile.Close();
}

 AfxGetMainWnd ()->EnableWindow (TRUE);
0
Albert-GeorgAuthor Commented:
Dear puranik_p,

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

Sincerely,

Albert
0
Albert-GeorgAuthor Commented:
Changing from CDialogEx to CDialog solved the problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.