user7813357
user7813357

Reputation:

C programming HWND

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;
}

Upvotes: 0

Views: 661

Answers (2)

user2371524
user2371524

Reputation:

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.

Upvotes: 1

lost_in_the_source
lost_in_the_source

Reputation: 11237

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

Upvotes: 1

Related Questions