0
votes

I need to create a console application that has a main() function and pop a modeless dialog, so the console can still work in parallel to the modeless dialog (do other work, like communicating with the modeless dialog).

Whatever i tried, i could only pop a modal dialog. (where the console is in hold till the modal dialog close itself).

When switching to modeless dialog using Create() and ShowWindow() the dialog is displayed without its controls and it freeze / block (you can see the hourglass cursor).

1) I tried to pop the modeless dialog from the main() function:

void main()
{
    AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), SW_SHOW);

    TestGUI * gui;
    gui = new TestGUI();
    gui->Create(TestGUI::IDD);
    gui->ShowWindow(SW_SHOW);

    // just to see if the modeless dialog responses
    Sleep(10000);
}

2) I tried to pop the modeless dialog from the InitInstance() of a CWinApp derived class:

extern int AFXAPI AfxWinMain(HINSTANCE hInstance,
                             HINSTANCE hPrevInstance,
                             LPTSTR lpCmdLine, int nCmdShow);

class MyApp : public CWinApp
{
public:
    virtual BOOL InitInstance()
    {
        gui = new TestGUI();
        gui->Create(TestGUI::IDD);
        gui->ShowWindow(SW_SHOW);

        return TRUE;
    }

private:
    TestGUI * gui;
};

MyApp my_app;

void main()
{
    AfxWinMain(GetModuleHandle(NULL), NULL, GetCommandLine(), SW_SHOW);

    // just to see if the modeless dialog responses
    Sleep(10000);
}

In all cases the modeless dialog freeze.

I believe this is a one line solution.
Please help.

TNX,
Vertilka

1

1 Answers

2
votes

Following code snippet solves the problem:

#include "stdafx.h"
#include "TestGUI.h"

DWORD WINAPI ModelessThreadFunc(LPVOID)
{
  TestGUI gui;
  gui.Create(TestGUI::IDD);
  gui.ShowWindow(SW_SHOW);

  HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, L"CloseModelessDialog");

  MSG msg;
  while(WaitForSingleObject(hEvent, 0) != WAIT_OBJECT_0)
  {
    while(::GetMessage(&msg, NULL, 0, 0))
    {
      ::TranslateMessage(&msg);
      ::DispatchMessage(&msg);
    }
  }

  // event cleanup
  CloseHandle(hEvent);

  return 0;
}

void main()
{
  // initialize MFC
  AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), SW_SHOW);

  // create thread for the modeless dialog
  CreateThread(NULL, 0, ModelessThreadFunc, NULL, 0, NULL);

  // wait for the modeless dialog to close itself
  HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, L"CloseModelessDialog");
  while(WaitForSingleObject(hEvent, 0) != WAIT_OBJECT_0)
  {
    // do other job
  }

  // event cleanup
  CloseHandle(hEvent);
}

Also look at the following link: microsoft newsgroups