0% found this document useful (0 votes)
75 views61 pages

Introduction To Windows Concepts

The document provides information about creating Windows applications using APIs. It discusses that Windows uses a GUI and programs are graphical in nature. It introduces key concepts like the Windows architecture being event-driven, APIs providing interfaces between applications and Windows, and the three primary API libraries - Kernel32.dll, GDI32.dll, and User32.dll. It then explains the steps to create a basic "Hello World" Windows application, including defining a window class, registering the class, and creating a window instance.

Uploaded by

ambrishsingh86
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views61 pages

Introduction To Windows Concepts

The document provides information about creating Windows applications using APIs. It discusses that Windows uses a GUI and programs are graphical in nature. It introduces key concepts like the Windows architecture being event-driven, APIs providing interfaces between applications and Windows, and the three primary API libraries - Kernel32.dll, GDI32.dll, and User32.dll. It then explains the steps to create a basic "Hello World" Windows application, including defining a window class, registering the class, and creating a window instance.

Uploaded by

ambrishsingh86
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 61

Introduction to Windows

Concepts
Windows is a Graphical User Interface (GUI) and programs
written in Windows will also be graphical in nature(eg , MS
Word , Excel , PowerPoint etc)
Windows provides the application with a set of in built functions
which are collectively called as the Application Programming
Interface(API) .
Writing the window based programs using the API functions is
called as SDK (Software development kit)
Application Programming Interface
API is a set of system defined function that any
application can call
An API is an Interface between application and
windows environment.
API’ s are also provided by O/S to Apllications.
Every Windows environment has its own unique
API.
The Windows Environment
Features:
Unlike DOS , Windows is a Graphical user interface:
Unlike many MS_DOS programs which deal directly with the hardware devices
like the video display , memory , keyboard etc , windows make use of the GDI to
interact with the hardware devices .
Unlike the MS_DOS programs which call the operating system to get the user
input , windows programs process the user input via messages from the operating
system .
MS_DOS programs do not support multiple programs to be executed but windows
based programs support multitasking.
Windows Architecture
Windows based program is an event driven program
Send
Hardware Message
events
Application message Message
queue loop
System
Window
Queue procedure

Default
window
procedure
Post
Message

How an application is started??


How a window processes messages??
Three Primary API in Windows O/S

These API Libs are present in System 32 directory. They


are created when windows is installed
1. Kernel32.dll - contains functions which deal with
major functions for allocating memory accessing
files etc.
2. GDI32.dll - Graphics Device Interface API Library
which contains functions for drawing graphics,
accessing hardware for displaying output
3. User32.dll - User Library used to provide support of
user interface routines like creating and managing
windows.
Working of a window based program using SDK
1) Fetch the entry point ie WinMain
2) Create the window class
3) Register the window class
4) Create the window
5) Show the window
6) Update the window
7) Retrieve the messages via the message loop
8) Translate the messages and dispatch the messages
9) Process the messages in the Window Procedure function
Hungarian Notation
•Many programmers in general and Windows
programmers in particular use a variable-
naming convention known as "Hungarian
Notation".
•It is called as "Hungarian Notation” in honor of
the legendary Microsoft programmer Charles
Simony.
•The variable name begins with a lowercase letter or letters that
denote the data type of the variable.
•For example:
•The sz prefix in szCmdLine stands for "string terminated by
zero."
•The h prefix in hInstance and hPrevInstance stands for
"handle"
•The i prefix in iCmdShow stands for "integer."
Prefix Data Type
c char or WCHAR or TCHAR
by BYTE (unsigned char)
n short
i int
x, y int used as x-coordinate or
y-coordinate
b or f BOOL (int); f stands for "flag"
Prefix Data Type
w WORD (unsigned short)
l LONG (long)
dw DWORD (unsigned long)
fn function
s string
string terminated by 0
sz
character
h handle
p Pointer
Hungarian Notation
•When naming structure variables, you can use the structure
name or an abbreviation of the structure name in lowercase
either as a prefix to the variable name or as the entire variable
name.
•For example:
•The msg variable is a structure of the MSG type
•wndclass is a structure of the WNDCLASS type
First Windows Application:
"Hello Win"
Two Functions

WinMain(…..) WndProc
•Entry Point •Also called as Window Procedure
•Responsible for •Responsible for processing
creating the window various messages sent by O/s &
others
WNDCLASSEX structure
The WNDCLASSEX structure contains window class
information
typedef struct _WNDCLASSEX {
UINT cbSize;
UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HINSTANCE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCTSTR lpszMenuName;
LPCTSTR lpszClassName;
HICON hIconSm;
} WNDCLASSEX, *PWNDCLASSEX;
WNDCLASS structure
•The two most important fields in the WNDCLASS structure are the
lpfnWndProc and lpszClassName .
•The (lpfnWndProc) is the address of a window procedure used
for all windows based on this class.
•The lpszClassName is the text name of the window class.
•This can be whatever you want.
•In programs that create only one window, the window class
name is commonly set to the name of the program.
wndclass.style = ?

•The statement
wndclass.style = CS_HREDRAW | CS_VREDRAW;
•combines two 32-bit "class style" identifiers with a C bitwise
OR operator.

•whenever the horizontal window size (CS_HREDRAW)


changes or
•the vertical window size (CS_VREDRAW) changes.
wndclass.lpfnWndProc = WndProc;

•The third field of the WNDCLASSEX structure is initialized by the


statement:
wndclass.lpfnWndProc = WndProc;
•This sets the window procedure for this window class to
WndProc.
•This window procedure will process all messages to all windows
created based on this window class.
•In C, when you use a function name in a statement like this, you're
really referring to a pointer to a function.
wndclass.cbClsExtra & wndclass.cbWndExtra

•The next two fields are used to reserve some extra space in
the class structure and the window structure that Windows
maintains internally:
•wndclass.cbClsExtra = 0 ;
•wndclass.cbWndExtra = 0 ;
•If a program does not use this feature, 0 is specified.
wndclass.hInstance = hInstance;

•The next field is simply the instance handle of the program


which is one of the parameters to WinMain:
wndclass.hInstance = hInstance;
wndclass.hIcon=LoadIcon(…)

•The statement
wndclass.hIcon=LoadIcon
(NULL,IDI_APPLICATION);
•sets an icon for all windows created based on this window
class. The icon is a small bitmap picture that represents the
program to the user.
•When the program is running, the icon appears in the
Windows taskbar and at the left side of the program window's
title bar.
wndclass.hIcon=LoadIcon(…)

Icon Macro Shape


IDI_APPLICATION
IDI_ERROR
IDI_INFORMATION
IDI_QUESTION
IDI_WARNING
IDI_WINLOGO
wndclass.hCursor=LoadCursor(…)

The statement
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW)
is similar to the previous statement.
•The LoadCursor function loads a predefined mouse cursor known
as IDC_ARROW and returns a handle to the cursor.
•This handle is assigned to the hCursor field of the WNDCLASSEX
structure.
•A small arrow appears over the client area of a window that is
created based on this class, which is called as the mouse cursor.
wndclass.hCursor=LoadCursor(…)

Cursor Macro Shape


IDC_ARROW Default Arrow Pointer
IDC_CROSS Cross Hair
IDC_BEAM Vertical I Beam
IDC_WAIT Hourglass
wndclass.hbrBackground=GetStockObject (..);

•This specifies the background color of the client area of windows


created based on this class.
•The hbr prefix of the hbrBackground field name stands for "handle
to a brush."
•wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
•This means that the background of the client area of the window
will be solid white, which is a common choice.
wndclass.hbrBackground=GetStockObject (..);

Macro Name Background Type


BLACK_BRUSH Black
DKGRAY_BRUSH Dark Grey
HOLLOW_BRUSH See-through window
LTGRAY_BRUSH Light Grey
WHITE_BRUSH White
wndclass.lpszMenuName = NULL;

•The next field specifies the window class menu. If the


application has no menu, then this field is set to NULL:
 wndclass.lpszMenuName = NULL;
 wndclass.lpszMenuName = “hi”;
 wndclass.lpszMenuName=
MAKEINTRESOURCE(IDR_MENU);
wndclass.lpszClassName=szAppName;
•Finally the class must be given a name. For a small program, this
can be simply be the name of the program, which is the "HelloWin"
string stored in the szAppName variable.

wndclass.lpszClassName=lpszClassName;
Registering the Window Class
•A window is always created based on a window class.
•The window class identifies the window procedure that processes
messages to the window.
•More than one window can be created based on a single window
class.
•For example, all button windows-including push buttons, check
boxes, and radio buttons-are created based on the same window
class.
Registering the Window Class

•The window class defines the window procedure and some other
characteristics of the windows that are created based on that class.
•When you create a window, you define additional characteristics
of the window that are unique to that window.
•Before you create an application window, you must register a
window class by calling RegisterClassEx.
RegisterClassEx(..)
The RegisterClassEx function registers a window class for subsequent use in
calls to the CreateWindow or CreateWindowEx function.

ATOM RegisterClassEx(
CONST WNDCLASSEX *lpwcx // class data
);

On registering the window class , internally an atom


table gets created which holds the address of the
window procedure where the messages get processed
CreateWindow(...)
It specifies the window class, window title, window style, and,
optionally, the initial position and size of the window. This function
also specifies the window's parent.
HWND CreateWindow(
LPCTSTR lpClassName,
LPCTSTR lpWindowName,
DWORD dwStyle,
int x,
int y,
int nWidth,
int nHeight,
HWND hWndParent,
HMENU hMenu,
HANDLE hInstance,
PVOID lpParam );
Creating the Window

•The window class defines general characteristics of a window,


thus allowing the same window class to be used for creating
many different windows.
•When you go ahead and create a window by calling
CreateWindow, you specify more detailed information about the
window.
ShowWindow(..)
The ShowWindow function sets the specified window's show state.
BOOL ShowWindow(
HWND hWnd, // handle to window
int nCmdShow // show state
);

Parameters
hWnd
[in] Handle to the window.
nCmdShow
[in] Specifies how the window is to be shown.
Window States
Value Meaning
SW_HIDE Hides the window and activates another
window.
SW_MAXIMIZE Maximizes the specified window.
SW_MINIMIZE Minimizes the specified window and
activates the next top- level window
SW_SHOW Activates the window and displays it in
its current size and position.
UpdateWindow
The UpdateWindow function updates the client area of the
specified window by sending a WM_PAINT message to the
window. The function sends a WM_PAINT message directly
to the window procedure of the specified window, bypassing
the application queue.

BOOL UpdateWindow(
HWND hWnd // handle to window
);
The Message Loop
•After the UpdateWindow call, the window is fully visible on the
video display.
•The program must now make itself ready to read keyboard and
mouse input from the user.
•Windows maintains a "message queue" for each Windows
program currently running under Windows.
•When an input event occurs, Windows translates the event into a
"message" that it places in the program's message queue.
The Message Loop

•A program retrieves these messages from the message queue by


executing a block of code known as the "message loop":
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
GetMessage(..)
This function retrieves a message from the calling thread’s message queue and
places it in the specified structure.
BOOL GetMessage(
LPMSG lpMsg,
HWND hWnd,
UINT wMsgFilterMin,
UINT wMsgFilterMax );
wMsgFilterMin
[in] Specifies the integer value of the lowest message value to be
retrieved.
wMsgFilterMax
[in] Specifies the integer value of the highest message value to be
retrieved.
GetMessage(…)
•The GetMessage call that begins the message loop retrieves a
message from the message queue:
GetMessage (&msg, NULL, 0, 0)
•This call passes to Windows a pointer to a MSG structure named
msg.
•The second, third, and fourth arguments are set to NULL or 0 to
indicate that the program wants all messages for all windows
created by the program.
TranslateMessage(..)
The TranslateMessage function translates
virtual-key messages into character messages

BOOL TranslateMessage(
CONST MSG *lpMsg // message
information
);
DispatchMessage(..)
The DispatchMessage function dispatches a message
to a window procedure. It is typically used to dispatch
a message retrieved by the GetMessage function.

LRESULT DispatchMessage(
CONST MSG *lpmsg // message
information
);
The Window Procedure
•A window procedure is always associated with a particular
window class that you register by calling RegisterClassEx.
•The CreateWindow function creates a window based on a
particular window class.
•More than one window can be created based on the same window
class.
•A window procedure is always defined: LRESULT CALLBACK
WndProc (HWND hwnd, UINT message,WPARAM wParam,
LPARAM lParam);
The Window Procedure
•The four parameters to the window procedure are identical to the first four
fields of the MSG structure.
•The first parameter is hwnd, the handle to the window receiving the
message.
•The second parameter is the same as the message field in the MSG
structure. It's a number that identifies the message.
Processing the Messages
•Every message that a window procedure receives is identified
by a number, which is the message parameter to the window
procedure.
•Generally, Windows programmers use a switch and case
construction to determine what message the window procedure
is receiving and how to process it accordingly.
•When a window procedure processes a message, it should
return 0 from the window procedure.
Processing the Messages

•All messages that a window procedure chooses not to process


must be passed to a Windows function named DefWindowProc.
•The value returned from DefWindowProc must be returned from
the window procedure.
Processing the Messages
switch (iMsg)
{
case WM_CREATE :
[process WM_CREATE message]
return 0 ;
case WM_PAINT :
[process WM_PAINT message]
return 0 ;
case WM_DESTROY :
[process WM_DESTROY message]
return 0 ;
}
return DefWindowProc(hwnd,iMsg, wParam, lParam);
Processing the Messages
•It is important to call DefWindowProc for default processing of all
messages that your window procedure does not process.
•Otherwise behavior regarded as normal, such as being able to
terminate the program, will not work.
WM_CREATE
•The very first message that a window procedure
receives
•WndProc receives this message while Windows is
processing the CreateWindow function in WinMain
•WndProc processes the WM_CREATE message and
returns controls back to Windows.
The WM_PAINT Message
•The second message that WndProc processes is
WM_PAINT.
•This message is extremely important in Windows
programming.
•It informs a program when part or all of the window's
client area is "invalid" and must be "updated," which
means that it must be redrawn or "painted."
How does a client area become invalid?

•The first WM_PAINT message (which normally occurs when the


program calls UpdateWindow in WinMain) directs the window
procedure to draw something on the client area.
•When the window is first created, the entire client area is invalid
because the program has not yet drawn anything on the window.
How does a client area become invalid?

•When you resize a window, the client area becomes


invalid eg when we set the style field of wndclass
structure to the flags CS_HREDRAW and CS_VREDRAW.
•This directs Windows to invalidate the whole window
when the size changes.
•The window procedure then receives a WM_PAINT
message.
How does a client area become invalid?
•When you minimize and then restore the window again
to its previous size, Windows does not save the contents
of the client area.
•Under a graphical environment, this would be too much
data to retain.
•Instead, Windows invalidates the window.
•The window procedure receives a WM_PAINT message
and itself restores the contents of its window.
How does a client area become invalid?

•When you move windows around the screen so that


they overlap, Windows does not save the area of a
window covered by another window.
•When that area of the window is later uncovered, it is
flagged as invalid.
•The window procedure receives a WM_PAINT message
to repaint the contents of the window.
The WM_PAINT Message
WM_PAINT processing almost always begins
with a call to BeginPaint:
hdc = BeginPaint (hwnd, &ps);

and ends with a call to EndPaint:


EndPaint (hwnd, &ps);
The WM_PAINT Message
•In both cases, the first argument is a handle to the
program's window, and the second argument is a pointer
to a structure of type PAINTSTRUCT.
•The PAINTSTRUCT structure contains some information
that a window procedure can use for painting the client
area.
The WM_PAINT Message
•During the BeginPaint call, Windows erases the background of
the client area if it hasn't been erased already.
•It erases the background using the brush specified in the
hbrBackground field of the WNDCLASS structure used to register
the window class.
•The BeginPaint call validates the entire client area and returns a
"handle to a device context."
The WM_PAINT Message
•A device context refers to a physical output device (such as a
video display) and its device driver.
•You need the device context handle to display text and graphics in
the client area of a window.
•Using the device context handle returned from BeginPaint, you
cannot draw outside the client area, even if you try.
•EndPaint releases the device context handle so that it is no longer
valid.
The WM_PAINT Message
•If a window procedure does not process WM_PAINT
messages, they must be passed on to DefWindowProc.
•DefWindowProc simply calls BeginPaint and EndPaint
in succession so that the client area is validated.
The WM_DESTROY Message

•The WM_DESTROY message is another important message.


•This message indicates that Windows is in the process of
destroying a window based on a command from the user.
•The message is a result of the user clicking on the Close button or
selecting Close from the program's system menu.
•Program responds to the WM_DESTROY message in a standard
way by calling
PostQuitMessage (0);
The WM_DESTROY Message

•This function inserts a WM_QUIT message in the program's


message queue.
•GetMessage returns nonzero for any message other than
WM_QUIT that it retrieves from the message queue.
•When GetMessage retrieves a WM_QUIT message, GetMessage
returns 0.
•This causes WinMain to drop out of the message loop.
The WM_DESTROY Message

•The program then executes the following statement:


return msg.wParam;
•The wParam field of the structure is the value passed to the
PostQuitMessage function (generally 0).
•The return statement exits from WinMain and terminates the
program.

You might also like