An Introduction To Windows Win32 API Programming
An Introduction To Windows Win32 API Programming
Program reads message data, does what's needed, returns control to Windows
(C) Richard R. Eckert (C) Richard R. Eckert
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
End Loop
(C) Richard R. Eckert
Example Program
See Windows Program: winapp2.cpp
Hungarian Notation
help clarify variable types precede name with key letters representing type named after Hungarian Microsoft programmer, Charles Simonyi
prefix data type --------------------------------by BYTE (unsigned char) b BOOL (int, TRUE=1 FALSE=0) c char dw DWORD (4-byte unsigned long) fn function h handle l long (4 bytes) n short (int) near pointer p pointer sz null-terminated char string w word (two bytes) lpsz long ptr to null-terminated str
(C) Richard R. Eckert
RegisterClass(&wndclass);
typedef struct tagWNDCLASS { UINT style; LRESULT CALLBACK lpfnWndProc)(); int cbClsExtra; int cbWndExtra; HINSTANCE hInstance; HICON hIcon; HCURSOR hCursor; HBRUSH hBackground; LPSTR lpszMenuName LPSTR lpszClassName } WNDCLASS; if (!RegisterClass (&wndclass)) return 0;
CreateWindow() arguments:
window class name window caption window style (OR of style masks) initial x , y position in pixels initial width , height parent window handle (if main window, NULL) window menu handle (NULL if class menu used) program instance handle (passed in from Windows) creation parameters (for extra data, usually NULL)
(C) Richard R. Eckert
ShowWindow (hWnd,nCmdShow);
makes window visible on screen hWnd: which window to make visible nCmdShow: how (normal, minimized, etc.)
set by Windows environment when program is started; value is passed in from Windows; "normal" can be overridden
UpdateWindow (hWnd);
Causes client area to be updated Painted with background brush
GetMessage()
Program must keep checking for messages Use message loop with GetMessage() BOOL GetMessage( LPMSG lpMsg, //ptr to msg struct HWND hWnd, //target window UINT wMsg1, //1st msg in range UINT wMsg2, //last msg in range)
(C) Richard R. Eckert
GetMessage()
Reads next msg from app's msg queue Fills MSG struct pointed to by first param. Place in a loop:
while (GetMessage(&msg, NULL, 0, 0)) { ... } return(msg.wParam);
Returns non-0, except for WM_QUIT msg Terminates msg loop & returns control to Windows
(C) Richard R. Eckert
Message Processing
What goes inside the message loop:
TranslateMessage (&msg)-"Cooks" keyboard input Converts raw key codes to ANSI codes
DispatchMessage (&msg)-Sends message on to Windows, which forwards it to program's "Window Procedure": WndProc()-2nd member of WNDCLASS structure Programmer must write this function
(C) Richard R. Eckert (C) Richard R. Eckert
WndProc()
LRESULT CALLBACK WndProc ( HWND hWnd, UINT wMessage, WPARAM wParam, LPARAM lParam) Parameters-Same as first four fields of MSG structure:
window associated with message message ID (what message is) message data (wParam & lParam)
Must be #included in .CPP & .RC files Can use Dev Studio's resource editors to prepare .rc & .h files visually
ID numbers generated automatically (C) Richard R. Eckert
Program
GDI
(C) Richard R. Eckert
Hardware
Device Context
Windows programs dont draw directly on HW, but on a Device Context (DC)
Abstracts the device it represents Like a painters canvas Specifies drawing attribute settings
e.g., text color
Program
n n
GDI
Driver
Hardware
Thus graphics I/O done in a standard way Programs will run unaltered on other HW platforms
Colors in Windows
Uses four-byte numbers to represent colors Simplest method--direct color:
typedef DWORD COLORREF; --------------------------------------------------------|0 | Blue (0-255) | Green (0-255) | Red (0-255) | -------------------------------------------------------- MSB=0:
==> RGB color used (default) other bytes specify R, G, B intensities
(C) Richard R. Eckert
RGB() Macro
Specify Red, Green, Blue intensities RGB() generates a COLORREF value can be used in color-setting functions, e.g.
COLORREF cr; cr = RGB (0,0,255); /* blue */
Stock Objects
Predefined in Windows Obtain with GetStockObject();
gets a handle to a predefined pen/brush/font
Example
SelectObject (hDC, GetStockObject(BLACK_PEN));
(C) Richard R. Eckert
Font
wParam==IDM_CLEAR ("Clear Screen clicked): call InvalidateRect() ==> Windows sends WM_PAINT msg client area needs to be repainted default Window Procedure repaints client area with class background brush effectively erases window's client area
User hits ANSI character keyboard key/s ==> WM_CHAR msg (wParam=char code)
copy character into a buffer output buffer to upper left corner w/TextOut()
User takes action to close window (double clicks on System menu or hits Alt-F4) ==> WM_DESTROY msg
post WM_QUIT msg to app's queue causes program to exit event loop and return control to Windows
2. Get into Developer Studio, open a new Workspace, & create an empty Win32 application
File | New | Projects tab | Win32 Application
7. Save your script file (should be called winapp2.rc). 8. Build the project