Sunday, September 8, 2013

Windows Simple Program-Win32

  1. SIMPLE WINDOW PROGRAM

             PROGRAM:
#include <windows.h>
LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow )
{
 static  TCHAR szAppName[]=TEXT ("HELLO");
HWND hwnd;
MSG msg;
// DECLARING AND INTIAZING THE WINDOW CLASS
WNDCLASS wc;                                                       // Window class declaration
wc.style = CS_HREDRAW| CS_VREDRAW;        // Window style
wc. lpfnWndProc = WndProc;                                   // Window Procedure
wc.cbClsExtra = 0;                                                     // Extra Parameters
wc.cbWndExtra = 0;                                                   // Extra Parameters
wc.hInstance = hInstance;                                          // Instance handle
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION); // Icon
wc.hCursor = LoadCursor (NULL, IDC_ARROW);            // Window Cursor
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
                                                                                    // Set Background Color
wc.lpszMenuName = NULL;                                     // Menu Handle
wc.lpszClassName = szAppName;                             // Class Name
// REGISTERING THE WINDOW CLASS
if (!RegisterClass (&wc))
{
            MessageBox(NULL, TEXT("This program requires WindowsNT!"), szAppName, MB_ICONERROR);
            return 0;
}
// CREATING THE WINDOW
hwnd = CreateWindow ( szAppName,   // window class name
TEXT("The Hello Program"), // window caption 
WS_OVERLAPPEDWINDOW,      // window style
CW_USEDEFAULT,          // initial x position
CW_USEDEFAULT,  // initial x position
CW_USEDEFAULT,         // initial x position
CW_USEDEFAULT,              // initial x position
NULL,                         // parent window  handle
NULL,                               // window menu handle
hInstance,                          //program instance handle
NULL);                           // creation parameters
// DISPLAYING THE WINDOW
            ShowWindow (hwnd, iCmdShow);              
            UpdateWindow (hwnd);
// PROCESSING THE MESSAGE LOOP
            while (GetMessage(&msg,NULL,0,0) // extracts message from Message Queue
            {
                                                TranslateMessage (&msg);  // dispatches the message received
                        DispatchMessage (&msg);
            }
            return msg.wParam;
}
// WINDOW PROCEDURE FUNCTION
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
            HDC hdc;
            PAINTSTRUCT ps;
            RECT rect;
            switch (message)
            {
            case WM_PAINT:    // Used to draw on client area using objects
                        hdc = BeginPaint (hwnd, &ps); // Gets Device Context
                        GetClientRect (hwnd, &rect);   // To get Client area size
                        DrawText (hdc, TEXT ("HELLO WINDOWS 98"),-1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
                        EndPaint(hwnd,&ps);    // Releases Device Context
                        return 0;

            case WM_DESTROY:     // Used to close the window
                        PostQuitMessage(0);  // used to terminate while loop in WinMain()
                        return 0;
            }
            return DefWindowProc (hwnd, message, wparam, lparam);
}



Sample Output:




No comments:

Post a Comment