2

I'm attempting to call CFileDialog to allow the user to select a file.

My program is a MFC application and I am using VS2005.

Here is my problem:

I have main View that creates a modeless dialog box. In this dialog box I have a menu item that makes a simple CFileDialog call:

CFileDialog dlgFile(true);
dlgFile.DoModal();

But the program always hangs on the DoModal. The program does not respond to commands and Task manager says it has stopped responding.

Other interesting pieces of information:

  • This has only been observed on Windows 8, Windows 7 machines seem to be unaffected.
  • I created a new project with the same basic View->modeless dialog->CFileDialog scheme and it worked just fine.
  • When I make a call to MessageBox, it appears behind the dialog box (I have to hit alt to get it up front), but the program is still responsive.
  • If I make the same CFileDialog call in the main View, it pops up without any problem.
  • Another annoying issue that may or may not be related to this: When the CFileDialog call is working (on Win 7), selecting "computer" in the browse window shows a blank white screen (Everything else works fine).

The closest questions I could find is this: Why does CFileDialog::DoModal() Hang? However, I don't use threads (At least I'm 95% sure I don't, this isn't just my project). Is my project automatically being threaded? If so, how can I make sure that isn't causing me problems?

Community
  • 1
  • 1
  • I tried this in Windows 8 and couldn't see a problem. You have to show more code to reproduce the problem. `dlgFile` must have more arguments etc. – Barmak Shemirani Apr 15 '15 at 13:59
  • @BarmakShemirani Yeah, I wish I could, but that is all there is to it, there is no more code. I literally have a function that just has those two lines of code in it. I can't seem to replicate it either. Like I mentioned, I made a new project on Windows 8 with the same basic setup and it worked just fine. It only happens with this project it seems. – Tyler Jackson Apr 15 '15 at 19:54
  • Create your CFileDialog passing the parent window of your modeless window. Have a look at the [constructor](https://msdn.microsoft.com/en-us/library/wh5hz49d%28v=vs.100%29.aspx) There is a `CWnd* pParentWnd = NULL` parameter that you can set – cha Apr 15 '15 at 22:51
  • @cha I tried that, I have set the parent to the Desktop, the Dialog Box, and main view, still freezes. – Tyler Jackson Apr 16 '15 at 19:13

3 Answers3

1

dlgFile.DoModal() calls run modal routine, it enables/disables and refocus windows, maybe that's the problem. You can try GetOpenFileName instead:

void CModeless::foo()
{
    //EnableWindow(0);
    OPENFILENAME ofn = { 0 };
    char buf[300];
    memset(buf, 0, 300);
    ofn.lpstrFile = buf;
    ofn.nMaxFile = 300;
    ofn.lStructSize = sizeof(OPENFILENAME);
    //ofn.hwndOwner = AfxGetApp()->m_pMainWnd->m_hWnd;
    GetOpenFileName(&ofn);
    //EnableWindow(1);
}

If there is no bug, then uncomment EnableWindow and ofn.hwndOwner, try again. You can also try this method:

void CModeless::foo()
{
    CWnd *wnd = GetParent(); //or AfxGetApp()->m_pMainWnd
    EnableWindow(0);
    CFileDialog dlg(TRUE, 0, 0, 0, 0, wnd, 0, 1);
    dlg.DoModal();
    EnableWindow(1);

    MSG msg;
    while (::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
        if (!AfxGetApp()->PumpMessage())
            break;

    SetFocus();
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • 1
    Thanks for the reply, but I tried both functions and they both give the same result. Still freezing, even when I uncommenting the two EnableWindow lines and the hwndOwner line. – Tyler Jackson Apr 16 '15 at 19:16
1

I've the same problem in VS2008. Try to set to false the last parameter in CFileDialog constructor (bVistaStyle)

V_G
  • 26
  • 2
  • Setting `bVistaStyle=FALSE` in the last parameter worked for me too, but it has reduced functionality. Also the hanging only occurs in DEBUG mode not RELEASE mode. (VS2008) – MasterHD Feb 20 '16 at 21:53
  • Coming back to this after a looooong time. There is not a bVistaStyle parameter in VS2005, however, I'm going to go ahead and mark this as the answer. – Tyler Jackson Aug 26 '16 at 18:44
0

My CDialogEx based dialog was unable to show the CFileDialog. From within my CDocument, everything worked fine. I found, that my dialog had a custom control, that was derived from CStatic and overrode the virtual WindowProc( ). After commenting the WindowProc, the CFileDialog showed as usual. The issue is only for the "m_bVistaStyle" CFileDialog, and the freeze occours in IFileDialog->Show().

KungPhoo
  • 516
  • 4
  • 18