Thread: My first Dialog Box code...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    My first Dialog Box code...

    Code:
    #include <windows.h>
    #include "stdafx.h"
    #include "Dialog box.h"
    
    HWND hWnd;
    LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    				   LPSTR lpCmdLine, int nCmdShow)
    {
    	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGFIRST),
    	          hWnd, reinterpret_cast<DLGPROC>(DlgProc));
    
    	return FALSE;
    }
    
    LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(Msg)
    	{
    	case WM_INITDIALOG:
    		return TRUE;
    
    	case WM_COMMAND:
    		switch(wParam)
    		{
    		case IDOK:
    			EndDialog(hWndDlg, 0);
    			return TRUE;
    		}
    		break;
    	}
    
    	return FALSE;
    }
    Builds but doesn't run (no output!) This is the example i got from net...

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    58
    Help, docs
    please show MSDN documents.

  3. #3
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    It is definitely the WinMain function that needs fixing. To confirm this add:
    Code:
    if(hwnd == NULL)
    {
    	MessageBox(NULL, "Window Creation Failed!", "Error!",
    		MB_ICONEXCLAMATION | MB_OK);
    	return 0;
    }
    above return FALSE, if it fails that means DialogBox has failed. Also you should set hwnd = DialogBox(...);
    This page from msdn explains how to use dialog boxes, that page alone doesn't supply the WinMain function required though. Also look up the DialogBox function to see if there are any examples on how to use it.
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Unhappy

    Quote Originally Posted by sgh View Post
    Help, docs
    please show MSDN documents.
    ?????????????

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    58
    hhh,
    please try to like this again.
    Code:
    NT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    				   LPSTR lpCmdLine, int nCmdShow)
    {
     hWnd = CreateDialog(hInstance,  MAKEINTRESOURCE(IDD_DIALOG1), 
                             NULL, (DLGPROC) GoToProc);
    
       if (!hWnd)
       {
          return FALSE;
       }
    
       ShowWindow(hWnd, nCmdShow);
       UpdateWindow(hWnd);
    ...
    }
    
    BOOL CALLBACK GoToProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam) 
    { 
        BOOL fError; 
     
        switch (message) 
        { 
            case WM_INITDIALOG: 
                return TRUE; 
     
            case WM_COMMAND: 
                switch (LOWORD(wParam)) 
                { 
                    case IDOK: 
                        
                        // Notify the owner window to carry out the task. 
     
                        return TRUE; 
     
                    case IDCANCEL: 
                        DestroyWindow(hwndDlg); 
                        return TRUE; 
                } 
        } 
        return FALSE; 
    }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Never cast the address of a function unless you really, REALLY know what you're doing, and in 99&#37; of the cases, it's WRONG.
    It will mask compile errors due to passing an incompatible function address as argument.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by sgh View Post
    hhh,
    please try to like this again.
    Code:
    NT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    				   LPSTR lpCmdLine, int nCmdShow)
    {
     hWnd = CreateDialog(hInstance,  MAKEINTRESOURCE(IDD_DIALOG1), 
                             NULL, (DLGPROC) GoToProc);
    
       if (!hWnd)
       {
          return FALSE;
       }
    
       ShowWindow(hWnd, nCmdShow);
       UpdateWindow(hWnd);
    ...
    }
    
    BOOL CALLBACK GoToProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam) 
    { 
        BOOL fError; 
     
        switch (message) 
        { 
            case WM_INITDIALOG: 
                return TRUE; 
     
            case WM_COMMAND: 
                switch (LOWORD(wParam)) 
                { 
                    case IDOK: 
                        
                        // Notify the owner window to carry out the task. 
     
                        return TRUE; 
     
                    case IDCANCEL: 
                        DestroyWindow(hwndDlg); 
                        return TRUE; 
                } 
        } 
        return FALSE; 
    }
    hWnd returns NULL!!!! no dialog window was created.... What's the possible problem here?
    Last edited by csonx_p; 05-06-2008 at 05:48 AM.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    58
    ?
    in debug mode, verify hInstance value.
    and did you create dialog(=IDD_DIALOG1) box in resource?
    try it again!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dialog Box error
    By JJFMJR in forum Windows Programming
    Replies: 4
    Last Post: 09-04-2007, 07:51 AM
  2. Replies: 2
    Last Post: 05-31-2005, 03:02 PM
  3. How to program a "back" button with MFC
    By 99atlantic in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2005, 08:34 PM
  4. Dialog box with full screen app.
    By curlious in forum Windows Programming
    Replies: 2
    Last Post: 10-27-2003, 10:37 AM