views:

229

answers:

3

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

A: 

Have you tried pumping messages?

jeffamaphone
A: 

show me the magic how to pump the messages !

btw,
isn't it something that you get automatically ? for example, if i would pop a modal dialog (from the console) and than pop the modeless dialog from the model dialog, the modeless dialog will work great !!

Vertilka
You should edit your question in stead of adding this as an answer. You probably will also have to write an email to [email protected] and ask to merge your 2 unregistered accounts into one (supply both user id's ==> 193395 and 301642).
fretje
A: 

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

Vertilka