prepared by fareeha lecturer dcs iiui 1 windows api

16
Prepared by Fareeha Lecturer DCS IIUI 1 Windows API

Upload: oswald-parks

Post on 12-Jan-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Prepared by Fareeha Lecturer DCS IIUI 1 Windows API

Prepared by Fareeha Lecturer DCS IIUI

1

Windows API

Page 2: Prepared by Fareeha Lecturer DCS IIUI 1 Windows API

Lecture 2 2

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

Page 3: Prepared by Fareeha Lecturer DCS IIUI 1 Windows API

Lecture 2 3

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

Page 4: Prepared by Fareeha Lecturer DCS IIUI 1 Windows API

Lecture 2 4

Core Components of APIs

Win16 API Win32 APIUSER.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.

Page 5: Prepared by Fareeha Lecturer DCS IIUI 1 Windows API

Lecture 2 5

Win32 API Programming

• Event-driven, graphics oriented

• Example: User clicks mouse over aprogram’s window area—

• – Windows decodes HW signals from mouse• – figures out which window user has selected• – sends a message to that window’s program:

Page 6: Prepared by Fareeha Lecturer DCS IIUI 1 Windows API

Lecture 2 6

• "User has clicked over (X,Y)”

• "Do something and return control to me”

– Program reads message data, does what'sneeded, returns control to Windows

Page 7: Prepared by Fareeha Lecturer DCS IIUI 1 Windows API

Lecture 2 7

• 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

Page 8: Prepared by Fareeha Lecturer DCS IIUI 1 Windows API

Lecture 2 8

Win32 API Program

• Structure--2 main tasks:

• Initial activities– Creating program own window– Startup activities

• Process messages from Windows (the

message loop)

Page 9: Prepared by Fareeha Lecturer DCS IIUI 1 Windows API

Lecture 2 9

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

Page 10: Prepared by Fareeha Lecturer DCS IIUI 1 Windows API

Lecture 2 10

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

Page 11: Prepared by Fareeha Lecturer DCS IIUI 1 Windows API

Lecture 2 11

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

Page 12: Prepared by Fareeha Lecturer DCS IIUI 1 Windows API

Lecture 2 12

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

Page 13: Prepared by Fareeha Lecturer DCS IIUI 1 Windows API

Lecture 2 13

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

Page 14: Prepared by Fareeha Lecturer DCS IIUI 1 Windows API

Lecture 2 14

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

Page 15: Prepared by Fareeha Lecturer DCS IIUI 1 Windows API

Lecture 2 15

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()

Page 16: Prepared by Fareeha Lecturer DCS IIUI 1 Windows API

Lecture 2 16

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)