0
votes

When I compile my code, a window should be opened but it does not. I have created a class, HWND, and application handlers; still nothing.

I am kinda new, sorry about the question.

The application runs fine without errors, but the window does not seem to appear.

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>


LRESULT CALLBACK myCallBack(HWND regularWnd, UINT message, WPARAM wparam, LPARAM lparam){
    switch(message){
    case 0x0201:
    printf("left Click");
    MessageBox(regularWnd, "Left Click", "event handler", MB_OK);
    break;
    case WM_CLOSE: 
    DestroyWindow(regularWnd);
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    DefWindowProc(regularWnd, message, wparam, lparam);
    break;
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE newHInstance, HINSTANCE prevHINSTANCE, LPSTR lpCmdLine, int cmdShow){

    WNDCLASSEX regularWndClass;

    regularWndClass.cbSize = sizeof(WNDCLASSEX);
    regularWndClass.cbWndExtra = 0;
    regularWndClass.cbClsExtra = 0;
    regularWndClass.style = 0;
    regularWndClass.lpszClassName = "regularWindowClass";
    regularWndClass.lpszMenuName = NULL;
    regularWndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+2);
    regularWndClass.lpfnWndProc = myCallBack;
    regularWndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    regularWndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    regularWndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    regularWndClass.hInstance = newHInstance;

    if(!RegisterClassEx(&regularWndClass) < 0){
        perror("Error Wnd class: ");
        exit(0);
    }

    HWND regularWnd;

    regularWnd = CreateWindowEx(WS_EX_CLIENTEDGE, "regularWindowClass", "The title of my window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, NULL, NULL, newHInstance, NULL);

    if(regularWnd < 0){
        perror("window error : ");
        exit(0);
    }

    ShowWindow(regularWnd, cmdShow);
    UpdateWindow(regularWnd);

    MSG message;
    while(GetMessage(&message, NULL, 0, 0) > 0){
        TranslateMessage(&message);
        DispatchMessage(&message);
    }
    return message.wParam;
}
2
I don't think CreateWindowEx() will set errno. Change this to use GetLastError() and FormatMessage() -- but that's just a hint ;) - user2371524
I recommend you to carefully read the documentation of the ShowWindow function on MSDN: msdn.microsoft.com/en-us/library/windows/desktop/… - 303
Don't use perror, because that checks errno, which isn't used by Win32 - Edward Karak

2 Answers

1
votes

The immediate error is in this line:

DefWindowProc(regularWnd, message, wparam, lparam);

A window procedure is supposed to return an LRESULT and DefWindowProc does this when necessary, but you don't pass it along. Change this to

return DefWindowProc(regularWnd, message, wparam, lparam);

and your window will appear as expected.

Apart from that, the doesn't use errno, so perror() will not work. You have to use GetLastError() and FormatMessage() for meaningful error messages, and with WinMain() (which strongly suggests subsystem windows), you won't have a console per default to display them ....

Finally, UpdateWindow() is completely unnecessary.

1
votes
LRESULT CALLBACK myCallBack(HWND regularWnd, UINT message, WPARAM wparam, LPARAM lparam)
{
    switch (message) {
        case 0x0201:
            printf("left Click");
            MessageBox(regularWnd, "Left Click", "event handler", MB_OK);
            return 0;
        case WM_CLOSE:
            DestroyWindow(regularWnd);
            return 0;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(regularWnd, message, wparam, lparam);
}

Change your window procedure to this

Also, use GetLastError and FormatMessage to print errors; not perror, that's or C standard library calls. Here's an example of how to use this function

https://msdn.microsoft.com/en-us/library/windows/desktop/ms680582(v=vs.85).aspx