Windows API: Prepared by Fareeha Lecturer Dcs Iiui 1
Windows API: Prepared by Fareeha Lecturer Dcs Iiui 1
Lecture 2 2
Types of Window API
• Two basic varieties
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
Lecture 2 4
Win32 API Programming
• Event-driven, graphics oriented
Lecture 2 6
• Events occur in response to
Lecture 2 7
Win32 API Program
• Structure--2 main tasks:
• Initial activities
– Creating program own window
– Startup activities
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
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
• WM_MOUSEMOVE--mouse moved
(lParam=x,y coords)
Lecture 2 16