Hello,

I have a problem with my Win32 app.
I made a window, and than a Dialog boxes. For window I use one message loop, and for Dialog boxes another. But It seems this wont work:

while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
                if(!IsDialogMessage(g_hToolbar, &Msg))
        {
            TranslateMessage(&Msg);
           DispatchMessage(&Msg);
        }
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }

If I make two different loops, program will never close.

So I have this code:

#include <windows.h>

#include "resource.h"

const char g_szclassName[] = "myWindowClass";
HWND g_hToolbar = NULL;


//BOOL CALLBACK WelcomeDDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
//{
//switch(Message)
// {
//case WM_INITDIALOG:
//    return TRUE;
//    case WM_COMMAND:
//switch(LOWORD(wParam)) 
//    { 
//       case IDD_LOCK: 
//        {
//            int ret = DialogBox(GetModuleHandle(NULL), 
//                MAKEINTRESOURCE(IDD_LOCK), hwnd, WelcomeDDlgProc);
//        
//             break;           
//        
//        }
//
//        
//     }
//
// }
//
//return TRUE;
//}
BOOL CALLBACK FinishDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDC_OK:
                     ShellExecute( NULL, "open", "http://jan1024188.googlepages.com/", NULL, "", SW_SHOW );
                     DestroyWindow(g_hToolbar);
                     
                      DestroyWindow(hwnd);
                    
                break;
                
            }
        break;
        default:
            return FALSE;
    }
    return TRUE;
}
BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDC_OK:
                     MessageBox(NULL, "You will need pencil and some paper, or just some experience.", "What will you need", MB_OK | MB_ICONINFORMATION);
                     DestroyWindow(g_hToolbar);
                break;
                case IDC_EXIT1:
                     MessageBox(NULL, "I hope to see you later... \n Exit", "Quit", MB_OK | MB_ICONEXCLAMATION);
                      DestroyWindow(g_hToolbar);
                      DestroyWindow(hwnd);
                      PostQuitMessage( 0 );

                     //DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_LOCK), hwnd, WelcomeDDlgProc);
                     
                    

                break;
            }
        break;
        default:
            return FALSE;
    }
    return TRUE;
}
BOOL CALLBACK LevelDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDC_OK:
                     MessageBox(NULL, "Okay, lets fun begin", "Good Luck", MB_OK | MB_ICONINFORMATION);    
                     //DestroyWindow(g_hToolbar);
                     ShellExecute( NULL, NULL, "everybody_dance_now.exe", NULL, "", SW_SHOW );
                     g_hToolbar = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_FINISH),
          hwnd, FinishDlgProc);
        if(g_hToolbar != NULL)
        {
            ShowWindow(g_hToolbar, SW_SHOW);
       }
        else
        {
            MessageBox(hwnd, "CreateDialog returned NULL", "Warning!",  
                MB_OK | MB_ICONINFORMATION);
        }
    break;                 
                break;
                case IDC_CANCEL:
                     MessageBox(NULL, "Don't worry. Practice a bit, and you will be fine.\n\n See you next time.", "Quit", MB_OK | MB_ICONEXCLAMATION);
                     DestroyWindow(g_hToolbar);
                     DestroyWindow(hwnd);


                break;
            }
        break;
        default:
            return FALSE;
    }
    return TRUE;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{    
    
    
    switch(msg)
    {
    case WM_CREATE:
    Beep( 100, 0);

     g_hToolbar = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_TOOLBAR),
            hwnd, ToolDlgProc);
        if(g_hToolbar != NULL)
        {
            ShowWindow(g_hToolbar, SW_SHOW);
        }
        else
        {
            MessageBox(hwnd, "CreateDialog returned NULL", "Warning!",  
                MB_OK | MB_ICONINFORMATION);
        }
    //DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_WELCOMED), hwnd, WelcomeDDlgProc); 
    //MessageBox(NULL, "You will need pencil and some paper, or just some experience.", "What will you need", MB_OK | MB_ICONINFORMATION);
    break;

    

        
    case WM_LBUTTONDOWN:
        {    
            char fileName[MAX_PATH];
            HINSTANCE hInstance = GetModuleHandle(NULL);
            GetModuleFileName(hInstance, fileName, MAX_PATH);
             //DestroyWindow(hwnd);
            DestroyWindow(g_hToolbar);
             
            //DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_WELCOMED), hwnd, WelcomeDDlgProc);
            MessageBox(NULL, "Well done. You have just started with a test. \n\n Now starting with LEVEL ONE", "Congratulations", MB_OK | MB_ICONINFORMATION);
            MessageBox(hwnd, fileName, "Tip: The path of file is:", MB_OK | MB_ICONINFORMATION);
            //ShellExecuteA(0,"open","screen_locker.exe","","",0);
            ShellExecute( NULL, NULL, "screen_locker.exe", NULL, "", SW_SHOW );
            Beep(0, 10000);
            MessageBox(NULL, "Congratulations! Now, we are switching to the last and most difficult level. \n Level TWO", "Congratulations", MB_OK | MB_ICONINFORMATION);
             g_hToolbar = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_LAST),
            hwnd, LevelDlgProc);
        if(g_hToolbar != NULL)
        {
            ShowWindow(g_hToolbar, SW_SHOW);
        }
        else
        {
            MessageBox(hwnd, "CreateDialog returned NULL", "Warning!",  
                MB_OK | MB_ICONINFORMATION);
        }
    break;

    
        

            



        }
        break;
                case WM_COMMAND:
            switch(LOWORD(wParam)) 
            {
                case ID_FILE_EXIT: 
                    MessageBox(NULL, "Exit", "quit message", MB_OK | MB_ICONEXCLAMATION);
                    DestroyWindow(g_hToolbar);
        DestroyWindow(hwnd);
               break;
               case ID_HELP_NEEDHELP: 
                    //DestroyWindow(hwnd);
                   //DestroyWindow(g_hToolbar);
                    MessageBox(hwnd,  "Click on window with modified cursor to start and follow the program instructions. \n For more details please visit http://jan1024188.googlepages.com/", "Help", MB_OK | MB_ICONINFORMATION );
                    break;
                    //DestroyWindow(g_hToolbar);
                    //DestroyWindow(hwnd);
                    //break;
               case ID_HELP_ABOUTUS: 
                    //DestroyWindow(hwnd);
                   DestroyWindow(g_hToolbar);
                   MessageBox(hwnd,  "This application was developed by: \n -Jan Birsa  jan@fedoraproject.org \n For more details please visit http://jan1024188.googlepages.com/", "About", MB_OK | MB_ICONINFORMATION);
                   //DestroyWindow(g_hToolbar);
               break;
               case ID_HELP_LICENCE: 
                    //DestroyWindow(hwnd);
                   DestroyWindow(g_hToolbar);
                    MessageBox(hwnd,  "This program is under GPL licence", "License", MB_OK | MB_ICONINFORMATION);
                    //DestroyWindow(g_hToolbar);
               break;
                case ID_DIALOG_OK: 
                     //DestroyWindow(hwnd);
                    DestroyWindow(g_hToolbar);
                    MessageBox(NULL, "You will need pencil and some paper, or just some experience.", "What will you need", MB_OK | MB_ICONINFORMATION);
                     DestroyWindow(g_hToolbar);
               break;
               case ID_DIALOG_QUIT: 
                    //DestroyWindow(hwnd);
                   DestroyWindow(g_hToolbar);
                 MessageBox(NULL, "I hope to see you later... \n Exit", "Quit", MB_OK | MB_ICONEXCLAMATION);
                     DestroyWindow(g_hToolbar);
                     DestroyWindow(hwnd);
               break;
            }
        break;
    case WM_CLOSE:
        MessageBox(NULL, "Exit", "quit message", MB_OK | MB_ICONEXCLAMATION);
        DestroyWindow(hwnd);

        break;
    case WM_DESTROY:
        DestroyWindow(g_hToolbar);
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}



int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
    wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 16, 16, 0);
    wc.hInstance = hInstance;
    wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = g_szclassName;
    wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1); 
    wc.hCursor = LoadCursor(GetModuleHandle(NULL), MAKEINTRESOURCE(IDC_MYPOINTERC));
    

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Err: Error with registering class", "Error ",  MB_ICONERROR | MB_OK);
        return 0;
    }

    hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szclassName,
"TYCK: Beginner",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
600,
650,
NULL,NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL,"ERROr", "HWND ERR", MB_OK | MB_ICONERROR);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
                if(!IsDialogMessage(g_hToolbar, &Msg))
        {
            TranslateMessage(&Msg);
           DispatchMessage(&Msg);
        }
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }


    return Msg.wParam;



}

But, whenever I choose something for menu it executes TWO TIMES instead of ONCE.

Can you guys help me out,


Thanks in advance

BTW: Sorry for this ugly code...

Problem solved:

while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
                if(!IsDialogMessage(g_hToolbar, &Msg))
        {
            TranslateMessage(&Msg);
           DispatchMessage(&Msg);
        }
else {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg); }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.