Link to home
Start Free TrialLog in
Avatar of m_sachin
m_sachin

asked on

Creating modeless dialog box (VC++)

Hi,
I intend to create a simple dialog box using VC++. The code snippet is given below. The call of CreateDialog always returns a NULL handle. Why is it so?

--Sachin


// dialog.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"

BOOL
InitMainDialog ( HINSTANCE hInstance,
                         int nCmdShow);

BOOL CALLBACK
MainDlgProc ( HWND hwDlg,
                    UINT uiMsg,
                    WPARAM wParam,
                    LPARAM lParam );

HINSTANCE      ghInstance;
HWND            ghWnd;

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
      MSG msg;
      if (!hPrevInstance ) {
             if (!InitMainDialog ( hInstance, nCmdShow ) ) {
                  return FALSE;
            }
      }
      while (GetMessage (&msg, NULL, 0, 0)) {
            TranslateMessage ( &msg );
            DispatchMessage ( &msg );
      }
      return 0;
}

BOOL
InitMainDialog (HINSTANCE hInstance,
                        int nCmdShow)
{
      // Save instance handle in a global variable
      ghInstance      = hInstance;
      
      char *strInResource      = MAKEINTRESOURCE ( IDD_MAIN_DLG );
      // Try to create a dialog box
      ghWnd            = CreateDialog ( hInstance,
                                                 MAKEINTRESOURCE (IDD_MAIN_DLG),
                                                 NULL,
                                                 (DLGPROC) MainDlgProc );
      if ( ghWnd == NULL ) {
            return FALSE;
      }

      ShowWindow ( ghWnd, SW_SHOW );
      return TRUE;
}


BOOL CALLBACK
MainDlgProc ( HWND hwDlg,
                    UINT uiMsg,
                    WPARAM wParam,
                    LPARAM lParam )
{
      switch ( uiMsg ) {
      
      }
      return TRUE;
}




Avatar of Member_2_1001466
Member_2_1001466

You should call GetLastError in the case that the call to CreateDialog fails. The reason for failing is stored in a "global" error value.

Can you post that value.
The CreateDialog returned not NULL after i added resource file to the project.

However, as there was no WM_PAINT handler there was no output ;-)

Regards, Alex
Avatar of m_sachin

ASKER

Error value returned is 1406
--Sachin
ASKER CERTIFIED SOLUTION
Avatar of Member_2_1001466
Member_2_1001466

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
1406 == //  Cannot create a top-level child window

Your dialog resource has Child Window property. You have to switch to Popup or Overlapped Style.

Regards, Alex

P. S. You'll find all errors in "winerror.h"