Simple Window in c

This is a simple window in c and win32 Api, the same as Masm from before, this one with comments.

Enjoy.

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) //main 
{
     HWND   hwnd; //window handler
     MSG    msg; //message
     WNDCLASSEX  wndclass = {0}; //windows class that olds the info about the window like style ivon etc init to 0
      
    //init the class props
     wndclass.cbSize = sizeof(wndclass); //size of the class
     wndclass.style  = CS_HREDRAW | CS_VREDRAW; //style of the window
     wndclass.lpfnWndProc = WndProc; //the function that will deal with all the messages
     wndclass.hInstance  = hInstance;	//instance of our window again
     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //background color
     wndclass.lpszClassName = "SimpleWindow"; //the name of our class
     
     RegisterClassEx(&wndclass); //register the windows
     
     //now create the window
     hwnd = CreateWindowEx(NULL, "SimpleWindow", "My First Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
						  CW_USEDEFAULT, 320, 200, NULL, NULL, hInstance, NULL);
                          
    ShowWindow(hwnd, iCmdShow);	
    
    UpdateWindow(hwnd); // This tells the window to draw itself so we can see it
    
    while(1)
    {
        if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if(msg.message == WM_QUIT)
				break;
    
            TranslateMessage(&msg);
            DispatchMessage(&msg); //sends message to wndproc
        }
    }
    UnregisterClass("SimpleWindow",hInstance);	//remove class
    
    return msg.wParam;
}

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s