4.3. WinAPI Basics.pptx

Download Report

Transcript 4.3. WinAPI Basics.pptx

WinAPI Basics
The program in traditional OS
The Program Starts and Runs
● ● ●
2. The Program
consists of one
main function
called by OS
1.The Program
calls OS
whenever needs
OS Service Call – Input/Output
Jal or Call to OS Subroutine
● ● ●
The Program Continues
● ● ●
OS Service Call – Input/Output
Jal or Call to OS Subroutine
● ● ●
The Program Ends
OS Input/Output
Subroutines
WinAPI Basics
The program in Windows
1. In Windows, the interaction between the program and the OS is a messagebased interaction. It’s the Windows that calls the program continuously.
2. In Windows the program consists of 2 parts:
• WinMain( ) is called first and interacts with the OS getting the Messages in
Message Loop from Message Queue and sending them to the second part
of program.
• Window CallBack Function ( ) processes the Messages. Here you write
your program’s main body.
WinAPI Basics
Message based interaction of Windows & Application
WinMain ( )
(1) The WinMain() function is
called once upon launching
this program by OS
OS
(2) Event
Window Initialization
(3) OS drivers or
interruption handlers
generate a message
Message
Queue
Message
Loop
Dispatch
Message
(4) Program Requests OS
to send the message
when it’s the turn of this
program to run
Message
(5) OS Sends the
message to this
program’s window
function
OS
WindowFunc ( )
Message
Switch
Key
Redraw
OS
Accept
the Key
Request to redraw
the screen
Redraw the screen
WinMain ( )
{
/* A minimal windows skeleton program. */
Define a window structure
#include <windows.h>
Windows Skeleton Program
Register the window structure
char szWinName[]
= "MyWin";
/* name of
windows
class
*/
LRESULT CALLBACK
WindowFunc(HWND,
UINT,
WPARAM,
LPARAM);
Create the window
HWND hwnd;
Display the window
MSG msg;int WINAPI WinMain(HINSTANCE
if(!RegisterClassEx(&wcl))
return 0; hThisInst, HINSTANCE
Run the message
loop
hPrevInst,
LPSTR lpszArgs,
int nWinMode)
/*Now
This
function
is class
called
by been
Windows
OS and
WNDCLASSEX
wcl;
/*
that
a window
has
registered,
{ message from the message queue.
}
**a is
passed
/*
Define
window
class.
**
window
cana be
created.
*/ */
WindowFunchwnd
(*********************************************/
) wcl.cbSize
= sizeof(WNDCLASSEX);
= CreateWindow(
LRESULT
CALLBACK WindowFunc(HWND
UINT
message,WPARAM
{
wcl.hInstance
=/*hThisInst;
/* hwnd,
handle
to*/this
instance */
szWinName,
name of window
class
wParam,
wcl.lpszClassName
= szWinName;
class name */
"Windows
Skeleton
Program", /*
/* window
title */
Switch ( message
) LPARAM lParam)
{
wcl.lpfnWndProc
= WindowFunc;
/* window
function*/*/
WS_OVERLAPPEDWINDOW,
/* window
style:normal
{
If you compile
the
above
VS2008
then
you’ll
get compile
switch(message)
wcl.style
0;program
/* default
style */
/*{Xincoordinate
- let
windows
decide */
/* Display
the =
window.
*/
case message
1 : CW_USEDEFAULT,
WinAPI Basics
WM_DESTROY:
/* terminate
the program
error
toCW_USEDEFAULT,
Unicode
usage
of Ystrings.
wcl.hIcon
= case
LoadIcon(NULL,IDI_APPLICATION);/*std
icon */
*/*/
/*
coordinate
- let
_ _ShowWindow(hwnd,
_related
nWinMode);
// Setup to
showwindows
window decide
___
PostQuitMessage(0);
wcl.hIconSm=
LoadIcon(NULL,IDI_WINLOGO);/*small
icon
CW_USEDEFAULT,
/*
width
let windows
decide
*/
• Either
you
should change
the
project
from
Unicode
to */
// -settings
Request
to update
window
_ _UpdateWindow(hwnd);
_
break;
wcl.hCursor=
LoadCursor(NULL,IDC_ARROW);/*cursor
style */
/*
heightlet windows decide */
/* Create
the message
loop.
*/
case Multibyte
message
2 : CW_USEDEFAULT,
default:
wcl.lpszMenuName
NULL;
/* 0))
no window
menu */*/
HWND_DESKTOP, =/*
no parent
_ _while(GetMessage(&msg,
_
NULL,
0,
• Or,
which
isNULL,
more desirable,
redefine
theprocess
strings
to Unicode
by
/* Let
NT
any
messages
___
wcl.cbClsExtra
= 0;Windows
/* no extra
*/
{
___
** /*handle
not
specified
in instance
the
preceding
Compiler
directives
TEXT,
__T,
or this
using
the needed
types
wchar_t,
wcl.cbWndExtra
= 0;
/* Linformation
*/
hThisInst,
of
of
theswitch
program
TranslateMessage(&msg);
/*
allow
use
of
keyboard
*/ */
default :/* Make
**
statement.
*/ white.*/*/
window
background
NULL the
/*
no
additional
arguments
TCHAR
instead
of char.
DispatchMessage(&msg);
/*return
control to windows */
___
return =DefWindowProc(hwnd,
message,
(HBRUSH) GetStockObject(WHITE_BRUSH);
wcl.hbrBackground
_ _}
_);
wParam,lParam);
___
}
return
Example
- msg.wParam;
wc.lpszClassName
= L"minwindowsapp";
}
}
}
return 0;
}