Dialog erscheint nicht wie gewünscht



  • Hallo Community,

    ich habe mich seit kurzen an die WinAPI-Programmierung gewagt.
    Nun habe ich beim lesen des forger-win32-Tutorial ein Problem
    mit den Dialogen. Das im Tutorial gezeigte Dialog erscheint bei
    mir NICHT gleich. 😮 http://mat.tac-design.net/pic/gfx/dialog.png
    Ich bin ratlos, denn selbst bei absolut exaktem Code erscheint das
    Fenster anders. Ich hoffe ihr könnt mir helfen. 🙂

    Ich arbeite mit:
    -Win2K
    -DevCPP 4.9.8.10 mit MingW-Compiler

    Ich poste hier den gesamten Quellcode, da ich absolut keine Ahnung
    habe, wo der Fehler sich eingeschlichen haben könnte. Es betrifft den
    Aufruf des About-Fensters.

    WinMain.cpp

    #include <windows.h>
    #include "resource.h"
    
    const char g_szClassName[] = "MyWindowClass";
    
    //Procedure for the About-Dialog
    BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
        switch(Message)
        {
            case WM_INITDIALOG:
    
            return TRUE;
            case WM_COMMAND:
                switch(LOWORD(wParam))[url]
                {
                    case IDOK:
                        EndDialog(hwnd, IDOK);
                    break;
                    case IDCANCEL:
                        EndDialog(hwnd, IDCANCEL);
                    break;
                }
            break;
            default:
                return TRUE;
        }
        return TRUE;
    }
    
    //The message procedure has to be before WINMAIN!!
    //These lines cannot be include!!
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case ID_\1:
                    {
                     DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
                    }
            }
            break;        
        case WM_LBUTTONDOWN:
            {
                    char szFileName[MAX_PATH];
                    HINSTANCE hInstance = GetModuleHandle(NULL);
                    GetModuleFileName(hInstance, szFileName, MAX_PATH);
                    MessageBox(hwnd, szFileName, "This program is:", MB_OK | MB_ICONINFORMATION);
            }            
            break;    
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
        }
    
        return 0;
    }
    
    //*****************************************************************************
    //********************************WINMAIN!!************************************
    //*****************************************************************************
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
    
        //Step 1: Registering the Window Class
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.style = 0;    //Can usually be set on 0
        wc.lpfnWndProc = WndProc;    //Pointer to the procedur for this window class
        wc.cbClsExtra = 0;    //Usually 0
        wc.cbWndExtra = 0;    //Usually 0
        wc.hInstance = hInstance;    //Handle to the application instance
        wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));    //Large 32x32 Icon
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);    //Cursor displayed over the Window
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);    //Background set to the Window
        wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);    //Name of a menu resource to use for the windows with this class.
        wc.lpszClassName = g_szClassName;    //Name to identify the class with.
        wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);    //Small (usually 16x16) icon to show in the taskbar and in the top left corner of the window.
    
        RegisterClassEx(&wc);
    
        // Step 2: Creating the Window
        hwnd = CreateWindowEx(
        WS_EX_APPWINDOW,
        g_szClassName,
        "WinAPI",
        WS_OVERLAPPEDWINDOW,
        200,    //X-coordinates for the left upper corner of the Window
        80,    //Y-coordinates for the left upper corner of the Window
        500,    //Window size in Pixels, X-coordinate
        380,    //Window size in Pixels, Y-coordinate
        NULL,
        NULL,
        hInstance,
        NULL);
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        //**************************************************************************
        //************************Here comes the individual code********************
        //**************************************************************************
        //This code will be executed on the startup of the program
        //********************************************************
    
        //MessageBox (NULL, "Hallo", "Caption", MB_OK);
    
        //End of the startup executed code
        //********************************
    
        // Step 3: The Message Loop
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    
    }
    

    resource.h

    #define IDR_MYMENU 101
    #define IDI_MYICON 201
    
    #define ID_FILE_EXIT 9001
    #define ID_ABOUT 9002
    
    #define IDD_ABOUT 9101
    #define IDC_STATIC 9109
    

    resource.rc

    IDR_MYMENU MENU
    BEGIN
        POPUP "&File"
        BEGIN
            MENUITEM "E&xit", ID_FILE_EXIT
        END
    
        POPUP "&?"
        BEGIN
            MENUITEM "&About...", ID_ABOUT
        END
    END
    
    //ICON FOR APP
    IDI_MYICON ICON "WinAPI.ico"
    
    //**************
    //*ABOUT DIALOG*
    //**************
    IDD_ABOUT DIALOG DISCARDABLE 50, 50, 239, 66
    STYLE DS_MODALFRAME | WS_CAPTION | WS_SYSMENU
    CAPTION "About this Program..."
    FONT 8, "MS Sans Serif"
    BEGIN
        DEFPUSHBUTTON "&OK",IDOK,174,18,50,14
        PUSHBUTTON "&Cancel",IDCANCEL,174,35,50,14
        GROUPBOX "About this program...",IDC_STATIC,7,7,225,52
        CTEXT "An example of WINAPI-Dialogs.Coded by Mat",IDC_STATIC,16,18,144,33
    END
    

    Und sobald, dass ich mit geöffnetem Dialogfenster einem anderen Programm
    den Fokus gebe, ist das Programm nur noch per Taskmanager beendbar. Der
    Dialog zeigt mir den Ausschnit des vorangehenden Fenster an.
    http://mat.tac-design.net/pic/gfx/dialog2.png

    Ich hoffe ihr könnt mir helfen und ich danke Euch schon im Voraus
    für eure Hilfe! 🙂

    Freundliche Grüsse

    edit: sfds



  • 1. Grundregel bei der WndProc: Nachrichten, die du bearbeitest, solltest du in der Regel nicht an DefWindowProc weiterreichen. Also nicht break, sondern return verwenden.

    WM_CLOSE musst du gar nicht behandeln, DefWindowProc ruft bei der Nachricht schon selbst DestroyWindow auf.

    Schwerer ist aber ein anderer Fehler (der führt wohl zum beschriebenen Phänomen): deine AboutDlgProc gibt in jedem Fall TRUE zurück. Wenn eine Nachricht nicht bearbeitet wurde, sollte diese Funktion allerdings FALSE zurückgeben (s. SDK-Doku).



  • Danke vielmals!
    Ich habe den Fehler korrigiert und jetzt funktioniert alles wie gewünscht.
    Die ersten zwei Erklärung habe ich zwar überhaupt nicht verstanden, aber
    ich denke nicht, dass das so wichtig ist, oder? 🙂

    Der korrigierte Code:
    [cpp]
    //Procedure for the About-Dialog
    BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    switch(Message)
    {
    case WM_INITDIALOG:

    return TRUE;
    case WM_COMMAND:
    switch(LOWORD(wParam))
    {
    case IDOK:
    EndDialog(hwnd, IDOK);
    break;
    case IDCANCEL:
    EndDialog(hwnd, IDCANCEL);
    break;
    }
    break;
    default:
    return FALSE;
    }
    return TRUE;
    }
    [/cpp]

    Danke nochmals! Hier wird man (endlich) kompetent geholfen! 👍


Anmelden zum Antworten