How to create window in C++


Code:-


#include <windows.h>

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

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR nCmdLine, int nCmdShow){
    HWND hwnd;
    MSG msg;
    WNDCLASSEX wc;
  
    memset(&wc, 0, sizeof(wc));
  
    wc.cbSize            = sizeof(WNDCLASSEX);
    wc.hbrBackground    = GetSysColorBrush(COLOR_3DFACE);
    wc.hCursor            = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
    wc.hIconSm            = LoadIcon(NULL, IDI_APPLICATION);
    wc.hInstance        = hInstance;
    wc.lpszClassName    = "Windowtut";
    wc.lpfnWndProc        = WndProc;
  
    if(!RegisterClassEx(&wc)){
        MessageBox(hwnd, "Windows Registration failed", "Error..", MB_ICONERROR);
        return 0;
    }
  
    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, "Windowtut", "Window Example",
    WS_VISIBLE | WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    600,
    400,
    NULL, NULL, NULL, NULL
    );
  
    if(hwnd == NULL){
        MessageBox(hwnd, "Windows Creation failed", "Error..", MB_ICONERROR);
        return 0;
    }
  
    while(GetMessage(&msg, 0, NULL, 0) > 0){
        DispatchMessage(&msg);
        TranslateMessage(&msg);
    }
  
    return msg.wParam;
}




Output:-

No comments:

Post a Comment