You are on page 1of 16

Windows API

Prepared by Fareeha Lecturer 1


DCS IIUI
What is Window API
• API Stands for Application Programming Interface

• Set of Functions that are part of Windows OS

• Program created by calling Functions present in APIs

• Internal of functions don’t have to be known

• All API functions contained in DLL

Lecture 2 2
Types of Window API
• Two basic varieties

– API for 16-bit windows (Win16 API)

– API for 32-bit windows (Win32 API)

Both are almost same


Win16 API is subset of Win32 API

Lecture 2 3
Core Components of APIs
Win16 API Win32 API
USER.EXE USER.DLL Responsible for window
management, including messages,
menus, cursor
GDI.EXE GDI.DLL Graphical device interface

KRNL386.EXE KRNL386.DLL Handles low level functions

In Win16 extension is .EXE but actually all are DLLs.

Lecture 2 4
Win32 API Programming
• Event-driven, graphics oriented

• Example: User clicks mouse over a


program’s window area—

• – Windows decodes HW signals from mouse


• – figures out which window user has selected
• – sends a message to that window’s program:
Lecture 2 5
• "User has clicked over (X,Y)”

• "Do something and return control to me”

– Program reads message data, does what's


needed, returns control to Windows

Lecture 2 6
• Events occur in response to

– Message being passed between windows


– User interaction with OS and application

• EDM is similar to heart, Message loop.


• Message loop is While loop()
• Windows provide Message Queue for each
process

Lecture 2 7
Win32 API Program
• Structure--2 main tasks:
• Initial activities
– Creating program own window
– Startup activities

• Process messages from Windows (the


message loop)

Lecture 2 8
PSEUDOCODE
• Initialize variables, memory space
• Create & show program's Window
• Loop
– Fetch any msg sent from Windows to this pgm
– If message is QUIT
• terminate program, return control to Windows
– If message is something else
• take actions based on message & parameters
• return control to Windows
• End Loop
Lecture 2 9
Essential Parts of a Windows
Programming
• The source program (.c/.cpp file):

– A. WinMain() function
• Entering point of window application
• Like main()
• Windows searches it at run time

Lecture 2 10
Functions performed by WinMain()
1. Declarations, initialization, etc.
•Program window declare through Window Class Structure
(WNDCLASS)
• which define class icon, cursor shape, background color, either
want Menu Resource, Window class name

2. register window class


• Pass window settings to operating system
•OS assign ID to this program window
• so that OS can send message to correct window

Lecture 2 11
Functions performed by WinMain()
3. create a window based on a registered class
4. show window
5. Create a message loop
 get messages from Windows (send to application queue)
 dispatch back to Windows for forwarding to correct
callback message-processing function
Message Queue
• Windows provided area for holding yours application's
messages
• All messages sent by Windows are stored in queue until
called for
Lecture 2 12
Types of Messages
• Queued Messages
– Caused by user Input from either mouse or
Keyboard
– Windows places them in queue
– WinMain() extract them from queue for processing

• Non-Queued Messages
– Concerned with windows management
– Handle by Windows itself

Lecture 2 13
The Message Loop
• Every thing in Windows happen in response to message
• Standard Mechanism for retrieving messages from message Queue
• while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
• Program must keep checking for messages
• Retrieve a message from message queue
• OS Fills MSG struct
• msg ID value.
• data passed in msg
• more data in msg
• time msg was sent
• mouse cursor position (x,y) when message was posted

Lecture 2 14
B. WndProc(): the msg-processing function.
The Window Procedure

• "callback" function (called by Windows)

• Handle message for particular window

• Should contain a switch/case statement :

– Looks at message ID of current message


– Acts appropriately on "interesting" messages
– Handle all queued messages
– Forwards other messages (non queued) to default Window
procedure--DefWindowProc()
Lecture 2 15
Some Important Messages

• WM_COMMAND--User clicked on menu item


(Param) = menu item ID

• WM_*BUTTONDOWN--left/right mouse button pressed


(* = L or R, lParam=x,y coordinates)

• WM_MOUSEMOVE--mouse moved
(lParam=x,y coords)

• WM_PAINT--Part of window was exposed & should be redrawn

• WM_KEYDOWN--keyboard key pressed (wParam= virtual key code)

Lecture 2 16

You might also like