Create Basic Window
2003-07-03 16:53:46
Category: c:windows
Description: A basic example on creating a window using the windows API.
Author: detour
Viewed: 11950
Rating: (75 votes)


/* win32api.c by detour@metalshell.com
 *
 * A basic example on creating a window using
 * the windows API.
 *
 * http://www.metalshell.com/
 *
 */
 
#include <windows.h>
 
/* The name of your window class, can be used multiple
   times after registered. */
const char *ClassName = "MyWindow";
 
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, 
            WPARAM wParam, LPARAM lParam);
 
// When using the windows API you use WinMain instead of main
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrev, 
                   LPSTR lpCmdLine, int nShowCmd) {
 
    // The window class struct.
    WNDCLASSEX wc;
    // Handle to our window.
    HWND hWnd;
    // Will hold messages sent to our window.
    MSG msg;
 
    // The size of the structure
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
    wc.hCursor = LoadCursor(hInstance, IDC_ARROW);
    wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
    wc.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = ClassName;
    wc.lpszMenuName = NULL;
    wc.style = 0;
 
    // Register the class to be used with CreateWindow.
    if(!RegisterClassEx(&wc)) {
        MessageBox(NULL, "Could not register window.", "Error",
            MB_ICONEXCLAMATION | MB_OK);
        return -1;
    }
 
    // Create the window using the "MyWindow" class.
    hWnd = CreateWindowEx(WS_EX_WINDOWEDGE, ClassName, "Basic Window", 
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 200, 150,
        NULL, NULL, hInstance, NULL);
 
    // If the window was not created show error and exit.
    if(hWnd == NULL) {
        MessageBox(NULL, "Could not create window.", "Error",
            MB_ICONEXCLAMATION | MB_OK);
        return -1;
    }
 
    // Set the windows show state, to show it.
    ShowWindow(hWnd, nShowCmd);
    // Draw the window.
    UpdateWindow(hWnd);
 
    // Retrieve messages from the message queue.
    while(GetMessage(&msg, NULL, 0, 0) > 0) {
        // Convert virtual key messages into a char
        TranslateMessage(&msg);
        // Send the message to our WndProc function
        DispatchMessage(&msg);
    }
 
    return msg.wParam;
}
 
// This will handle the messages.
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, 
                         WPARAM wParam, LPARAM lParam) {
 
    switch(uMsg) {
        // Recieved when the user hits the X box or alt-F4
        case WM_CLOSE:
            // Tell the program to end, and dispath WM_DESTROY
            DestroyWindow(hWnd);
            break;
        case WM_DESTROY:
            // End the program.
            PostQuitMessage(0);
            break;
        default:
            // DefWindowProc processes anything we don't.
            return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
 
    return 0;
}