• Advertisement
Sign in to follow this  

problem with my win32 code

This topic is 4682 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I was just writing some basic window-creating code, but it keeps crashing on CreateWindowEx. Can anyone tell what's wrong? (I use devcpp)
#include <windows.h>

const char my_windowclassname[] = "my_class";

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CREATE:
            
        break;
        
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            DefWindowProc(hwnd,msg,wParam,lParam);
    }
    return 0;   
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    HWND my_windowhandle;
    MSG my_message;
    WNDCLASSEX my_windowclass;

    my_windowclass.cbSize        = sizeof(WNDCLASSEX);
    my_windowclass.style         = CS_HREDRAW|CS_VREDRAW|CS_OWNDC|CS_DBLCLKS;
    my_windowclass.lpfnWndProc   = WndProc;
    my_windowclass.cbClsExtra    = 0;
    my_windowclass.cbWndExtra    = 0;
    my_windowclass.hInstance     = hInstance;
    my_windowclass.hIcon         = LoadIcon(NULL,IDI_APPLICATION);
    my_windowclass.hCursor       = LoadCursor(NULL,IDC_ARROW);
    my_windowclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    my_windowclass.lpszMenuName  = 0;
    my_windowclass.lpszClassName = my_windowclassname;
    my_windowclass.hIconSm       = LoadIcon(NULL,IDI_APPLICATION);

    if(!RegisterClassEx(&my_windowclass))
    {
        MessageBox(NULL,"could not create windowclass","error",MB_OK|MB_ICONERROR);
        return 0;
    }  
    
    my_windowhandle = CreateWindowEx(WS_EX_CLIENTEDGE,my_windowclassname,"some_window",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,800,600,NULL,NULL,hInstance,NULL);
    
    if(!my_windowhandle)
    {
        MessageBox(NULL,"could not create window","error",MB_OK|MB_ICONERROR);
        return 0;
    }
    
    ShowWindow(my_windowhandle, nCmdShow);
    
    UpdateWindow(my_windowhandle);
    
    while(GetMessage(&my_message, NULL, 0, 0) > 0)
    {
        TranslateMessage(&my_message);
        DispatchMessage(&my_message);
    }
    
    return my_message.wParam;
}

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
The problem was that you were always returning 0, even for unhandled messages... see below

[source lang="cpp]
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
// indicate success here, it just makes things easier to follow
case WM_CREATE:
return 0;
break;

case WM_CLOSE:
DestroyWindow(hwnd);
break;

case WM_DESTROY:
PostQuitMessage(0);
break;
}

// must return the result, not just call it
return DefWindowProc(hwnd,msg,wParam,lParam);
}


Share this post


Link to post
Share on other sites
You always return 0 if you message isn't handled. You should return the value of DefWindowProc. Here's the corrected WndProc:

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

case WM_CLOSE:
DestroyWindow(hwnd);
break;

case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
[/soruce]
Edit: Too slow

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Actually, for WM_CLOSE and WM_DESTROY, you should also return 0; instead of breaking... that way you are indicating that you've handled the messages.

Only let DefWindowProc get called if you don't manually handle the messages yourself.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement