Windows Programming - Final
Windows Programming - Final
• An event could be a keystroke, a
mouse click, or a command for a
window to repaint itself, among
other things.
#include <windows.h>
STEP 2. Create main WinMain function and HWND hwnd;
Register the Window Class. /* A 'HANDLE', hence the H, or a pointer to our
window */
int WINAPI WinMain (HINSTANCE hInstance, MSG msg;
HINSTANCE hPrevInstance, LPSTR lpszCmdLine, /* A temporary location for all messages */
int nCmdShow)
{ /* REGISTERING THE WINDOW BY
/* HINSTANCE hInstance: WINDOW ID, Handle SETTING ESSENTIAL PARAMETERS */
to the programs executable module (the .exe file in
memory) memset(&wc, 0, sizeof(wc));
HINSTANCE hPrevInstance: Always NULL for /* zero out the struct and set the stuff we want to
Win32 programs. hPrevInstance used to be the handle modify */
to the previously run instance of your program (if any) wc.cbSize = sizeof(WNDCLASSEX);
in Win16 /* set the size of the structure */
LPSTR lpCmdLine: The command line arguments as wc.lpfnWndProc = WndProc;
a single string. In Win32 Data type for char* is /* This is where we will send messages to */
LPSTR. /* Pointer to the window procedure for this
int nCmdShow: Window type (max, normal or min). window*/
An integer value which may be passed wc.hInstance = hInstance;
to ShowWindow(). /* Handle to the application instance */
*/ wc.lpszClassName = "WindowClass";
WNDCLASSEX wc; /*Name to identify the class with.*/
/* A properties struct of our window */
/* IF THE WINDOW IS NOT REGISTERED
DISPLAY ERROR MESSAGE */
if(!RegisterClassEx (&wc))
{
MessageBox (NULL, "Window Registration Failed!",
"Error!", MB_ICONEXCLAMATION|MB_OK);
return 0;
}
/*
Call RegisterClassEx() and check for failure, if it fails
pop up a message which says so and abort the program
by returning from the WinMain() function.
*/
STEP 3: Create and Show the Window and the /* IF THE WINDOW IS NOT CREATED
Message Loop DISPLAY ERROR MESSAGE AND RETURN*/
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE, if(hwnd == NULL)
/* extended window style for window border*/ {
"WindowClass", /* give registered window name as it MessageBox(NULL, "Window Creation
tells which window to create*/ Failed!","Error!",MB_ICONEXCLAMATION|
"Caption", /* Title of your window */ MB_OK);
WS_VISIBLE|WS_OVERLAPPEDWINDOW,
/*Window style parameter */ return 0;
CW_USEDEFAULT, /* X*/ }
CW_USEDEFAULT, /* Y */ /*Show Window Function*/
640, /* width */ ShowWindow(hwnd, nCmdShow);
480, /* height */
/* X and Y co-ordinates for the top left corner of your /* The nCmdShow parameter is optional and is same
window, and the width and height of the window. The as SW_SHOWNORMAL for normal sized window.
units are pixels. */ However, using this parameter passed
NULL, /* Parent Window handle. The parent handle into WinMain() gives whoever is running your
is NULL as this is our main or Top Level window*/ program to specify whether or not they want your
NULL, /* Menu handle */ window to start off visible, maximized, minimized
hInstance, /* Application instance handle*/ (SW_SHOWMINIMIZED,
NULL); /* A pointer to window creation data*/ SW_SHOWMAXIMIZED) etc.*/
/*
THE MESSAGE LOOP
This is the heart of our program where all input is
processed and sent to WndProc. GetMessage() is
requesting the next available message to be removed
from the message queue and returned for processing.
Note that GetMessage blocks code flow until it
receives something, so this loop will not produce
unreasonably high CPU usage */
while(GetMessage(&msg, NULL, 0, 0) )
{
/* If no error is received... */
TranslateMessage(&msg);
/* Translate key codes to chars if present */
DispatchMessage(&msg);
/* Send it to WndProc */
}
return msg.wParam;
}
STEP 4: The Window Procedure /* When close (X) button is pressed
DestroyWindow() sends WM_DESTROY message
/* This is where all the messages that are sent to our to the window getting destroyed, and then destroys
window get processed. any remaining child windows before finally
LRESULT is a typedef of a long int removing target window from the system. Since this
CALLBACK is a typedef of _stdcall. */ is the only window in this program, we want the
LRESULT CALLBACK WndProc program to exit, so we call PostQuitMessage(). This
( posts the WM_QUIT message to the message loop.
HWND hwnd, /* HWND parameter is the handle of */
your window, the one that the message applies to */ case WM_DESTROY:
UINT Message, /* Win32 Unsigned Int datatype: PostQuitMessage(0);
UINT message received*/ break;
WPARAM wParam, /* Parameters passed for different /* Upon destruction, tell the main thread to stop */
types of messages */
LPARAM lParam /* Parameters passed for different default:
types of messages */ return DefWindowProc(hwnd, Message,
) wParam, lParam);
{ /* All other messages (a lot of them) are processed
switch(Message) using default procedures */
{ }
case WM_CLOSE: return 0;
DestroyWindow(hwnd); }
break;
Complete Windows Real World Program
#include <windows.h> if(!RegisterClassEx (&wc))
LRESULT CALLBACK WndProc {
(HWND hwnd, UINT Message,WPARAM wParam, MessageBox (NULL, "Window Registration Failed!", "Error!",
LPARAM lParam) MB_ICONEXCLAMATION|MB_OK);
{switch(Message) { return 0;
case WM_CLOSE: }
DestroyWindow(hwnd);break; hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,
case WM_DESTROY: "WindowClass","Caption",WS_VISIBLE|
PostQuitMessage(0); break; WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
default: CW_USEDEFAULT,640,480,NULL,NULL,hInstance,NULL);
return DefWindowProc(hwnd, Message, wParam, if(hwnd == NULL)
lParam); } {
return 0; MessageBox(NULL, "Window Creation
} Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
int WINAPI WinMain (HINSTANCE hInstance, return 0;
HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int }
nCmdShow) ShowWindow(hwnd, nCmdShow);
{
WNDCLASSEX wc; while(GetMessage(&msg, NULL, 0, 0))
HWND hwnd; {
MSG msg; TranslateMessage(&msg);
memset(&wc, 0, sizeof(wc)); DispatchMessage(&msg);
wc.cbSize = sizeof(WNDCLASSEX); }
wc.lpfnWndProc = WndProc; return msg.wParam;
wc.hInstance = hInstance; }
wc.lpszClassName = "WindowClass";
#include <windows.h>
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE
hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASSEX wc;HWND hwnd; helper.h file
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
/* perform application initialization.*/
wc.hInstance = hInstance;
wc.lpszClassName = "WindowClass"; InitInstance ( hInstance, nCmdShow, "title" ) ;
if(!RegisterClassEx (&wc))
{MessageBox (NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION|MB_OK);
/*For the sake of simplicity, for further conventions
return 0;} of these slides, we will be using helper.h header file
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, in which the InitInstance() function is defined and
"WindowClass","Caption",WS_VISIBLE|
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
performs all the tasks of initializing, registering,
CW_USEDEFAULT,640,480,NULL,NULL,hInstance,NULL); creating and showing the window.
if(hwnd == NULL) {MessageBox(NULL, "Window
CreationFailed!","Error!",MB_ICONEXCLAMATION|
MB_OK);return 0;}
It takes 3 parameters:
ShowWindow(hwnd, nCmdShow); handle or WINDOW ID,
while(GetMessage(&msg, NULL, 0, 0) ) nCmdShow: Window type (max, normal or min)
{
TranslateMessage(&msg);
Caption or title of the present window.
DispatchMessage(&msg); */
}
return msg.wParam;
}
LRESULT CALLBACK WndProc ( HWND, UINT, WPARAM, LPARAM ) ;
HINSTANCE hInst ; // current instance
BOOL InitInstance ( HINSTANCE hInstance, int nCmdShow, char* pTitle )
{ PROCEDURE to include helper.h
char classname[ ] = "MyWindowClass" ;
HWND hWnd ;
WNDCLASSEX wcex ;
wcex.cbSize = sizeof ( WNDCLASSEX ) ;
wcex.style = CS_HREDRAW | CS_VREDRAW ; 1. Create a new file named helper.h (use .h extension)
wcex.lpfnWndProc = ( WNDPROC ) WndProc ;
wcex.cbClsExtra = 0 ;
2. Copy the contents given on the left as it is in this newly
wcex.cbWndExtra = 0 ; created file.
wcex.hInstance = hInstance ; 3. Save it in current working directory of your project.
wcex.hIcon = NULL ; 4. Use this file by writing #include “helper.h” in your
wcex.hCursor = LoadCursor ( NULL, IDC_ARROW ) ;
wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 ) ;
window programs
wcex.lpszMenuName = NULL ;
wcex.lpszClassName = classname ;
wcex.hIconSm = NULL ;
REMEMBER: To copy this file to your project directory—the
if ( !RegisterClassEx ( &wcex ) )
return FALSE ;
directory in which you are going to create the program.
hInst = hInstance ; // Store instance handle in our global variable
hWnd = CreateWindow ( classname, pTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL,
NULL, hInstance, NULL ) ;
if ( !hWnd )
return FALSE ;
ShowWindow ( hWnd, nCmdShow ) ;
UpdateWindow ( hWnd ) ;
return TRUE ;
}
THE NEW PROGRAMS WILL LOOK AS:
• A base interface:
COM defines one special interface, IUnknown, to implement some of the
essential functionality.
All COM components are required to implement the IUnknown interface and are
derived from IUnknown.
IUnknown has three methods:
o QueryInterface: Mechanism that a client uses to get an interface
pointer from a COM component and also checks whether a COM
interface is supported by a COM component or not.
o AddRef: Called when another COM component is using the
interface.
o Release: Called when COM component no longer requires that
interface
COM Fundamentals for developing Windows
Applications