Presentation Outline Introduction Painting and Repainting GDI.

24
Presentation Outline Introduction Painting and Repainting GDI

Transcript of Presentation Outline Introduction Painting and Repainting GDI.

Page 1: Presentation Outline Introduction Painting and Repainting GDI.

Presentation Outline

IntroductionPainting and RepaintingGDI

Page 2: Presentation Outline Introduction Painting and Repainting GDI.

Introduction

Client area is the part of the window on which a program is free to draw and deliver visual information to the user.

When a program displays text or graphics in its client area, it is often said to be "painting" its client area.

Page 3: Presentation Outline Introduction Painting and Repainting GDI.

Painting and Repainting (2)

Windows is a message-driven system.

Windows informs applications of various events by posting messages in the application's message queue or sending messages to the appropriate window procedure.

Windows informs a window procedure by posting a WM_PAINT message.

Page 4: Presentation Outline Introduction Painting and Repainting GDI.

The WM_PAINT Message A window procedure receives a WM_PAINT

message whenever one of the following events occurs:

hidden area of the window is brought into view when a user moves a window or uncovers a window.

The user resizes the window (if the window class style has the CS_HREDRAW and CW_VREDRAW bits set).

The program uses the InvalidateRect or InvalidateRgn function to explicitly generate a WM_PAINT message.

Page 5: Presentation Outline Introduction Painting and Repainting GDI.

The WM_PAINT Message (2)

Windows may sometimes post a WM_PAINT message when:

Windows removes a dialog box or message box that was overlaying part of the window.

A menu is pulled down and then released. A tool tip is displayed.

Page 6: Presentation Outline Introduction Painting and Repainting GDI.

The WM_PAINT Message (2)

In a few cases, Windows always saves the area of the display it overwrites and then restores it. This is the case whenever:

The mouse cursor is moved across the client area. An icon is dragged across the client area.

Page 7: Presentation Outline Introduction Painting and Repainting GDI.

Valid and Invalid Rectangles

Repainting is required only for the rectangular area uncovered when the dialog box is removed.

That area is known as an "invalid region" or "update region."

The presence of an invalid region in a client area is what prompts Windows to place a WM_PAINT message in the application's message queue.

Your window procedure receives a WM_PAINT message only if part of your client area is invalid.

Page 8: Presentation Outline Introduction Painting and Repainting GDI.

Valid and Invalid Rectangles(2) Windows internally maintains a "paint

information structure" for each window.

This structure contains, among other information, the coordinates of the smallest rectangle that encompasses the invalid region. This is known as the "invalid rectangle.“

Windows does not place multiple WM_PAINT messages in the message queue.

Page 9: Presentation Outline Introduction Painting and Repainting GDI.

Valid and Invalid Rectangles(3)

InvalidateRect.

GetUpdateRect

BeginPaint

ValidateRect

Page 10: Presentation Outline Introduction Painting and Repainting GDI.

An Introduction to GDI To paint the client area of your window, we use Windows

Graphics Device Interface (GDI) functions.

Windows provides several GDI functions for writing text strings to the client area of the window

TextOut (hdc, x, y, psText, iLength) ;

TextOut writes a character string to the client area of the window. The psText argument is a pointer to the character string, and iLength is the length of the string in characters. The x and y arguments define the starting position of the character

string in the client area. The hdc argument is a "handle to a device context"

Page 11: Presentation Outline Introduction Painting and Repainting GDI.

The Device Context

The device context is a data structure maintained internally by GDI.

A device context is associated with a particular display device, such as a video display or a printer.

Some of the values in the device context are graphics "attributes." These attributes define some particulars of how GDI drawing functions work.

With TextOut, the color of the text, the color of the text background, font for the text to be displayed

Page 12: Presentation Outline Introduction Painting and Repainting GDI.

The Device Context When a program needs to paint, it must first

obtain a handle to a device context.

Windows fills the internal device context structure with default attribute values.

You can change these defaults by calling various GDI functions.

After a program has finished painting its client area, it should release the device context handle.

Page 13: Presentation Outline Introduction Painting and Repainting GDI.

Getting a Device Context Handle:

You use this method when you process WM_PAINT messages.

Two functions are involved: BeginPaint and EndPaint. These two functions require the handle to the window and the

address of a structure variable of type PAINTSTRUCT PAINTSTRUCT ps ;

While processing a WM_PAINT message, the window procedure first calls BeginPaint.

The BeginPaint function generally causes the background of the invalid region to be erased in preparation for painting.

The function also fills in the fields of the ps structure. The value returned from BeginPaint is the device context handle.

HDC hdc ;

Page 14: Presentation Outline Introduction Painting and Repainting GDI.

Getting a Device Context Handle:

The HDC data type is defined as a 32-bit unsigned integer.

The program may then use GDI functions, such as TextOut, that require the handle to the device context.

A call to EndPaint releases the device context handle.

case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ;

[use GDI functions] EndPaint (hwnd, &ps) ; return 0 ;

Page 15: Presentation Outline Introduction Painting and Repainting GDI.

The Paint Information Structure

Windows maintains a paint information structure for each window.

typedef struct tagPAINTSTRUCT{

HDC hdc ; BOOL fErase ; RECT rcPaint ; BOOL fRestore ;

BOOL fIncUpdate ; BYTE rgbReserved[32] ;} PAINTSTRUCT ;

Page 16: Presentation Outline Introduction Painting and Repainting GDI.

The Paint Information Structure

Windows fills in the fields of this structure when your program calls BeginPaint.

Program can use only the first three fields. The others are used internally by Windows.

wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;

Page 17: Presentation Outline Introduction Painting and Repainting GDI.

The boundaries of the invalid rectangle.

Page 18: Presentation Outline Introduction Painting and Repainting GDI.

Getting a Device Context Handle: Method Two

To get a handle to the device context of the client area of the window, you call GetDC to obtain the handle and ReleaseDC after you're done with it

hdc = GetDC (hwnd) ;

[use GDI functions]

ReleaseDC (hwnd, hdc) ;

Page 19: Presentation Outline Introduction Painting and Repainting GDI.

TextOut: The Details TextOut is the most common GDI function for

displaying text. Its syntax is

TextOut (hdc, x, y, psText, iLength) ; The attributes of the device context control the

characteristics of this displayed text. For instance, one attribute of the device context specifies the text color. The default color (we discover with some degree of comfort) is black. The default device context also defines a text background color, and this is white. When a program writes text to the display, Windows uses this background color to fill in the rectangular space surrounding each character, called the "character box."

Page 20: Presentation Outline Introduction Painting and Repainting GDI.

The System Font

The device context also defines the font that Windows uses when you call TextOut to display text. The default is a font called the "system font" or (using the identifier in the WINGDI.H header file) SYSTEM_FONT.

The system font is the font that Windows uses by default for text strings in title bars, menus, and dialog boxes.

Page 21: Presentation Outline Introduction Painting and Repainting GDI.

Text metrics Windows copies the various values of text metrics into a structure of

type TEXTMETRIC defined in WINGDI.H. The TEXTMETRIC structure has 20 fields, but we're interested in only the first seven:

typedef struct tagTEXTMETRIC{ LONG tmHeight ;

LONG tmAscent ; LONG tmDescent ; LONG tmInternalLeading ;

LONG tmExternalLeading ; LONG tmAveCharWidth ; LONG tmMaxCharWidth ;

[other structure fields] }TEXTMETRIC, * PTEXTMETRIC ;

Page 22: Presentation Outline Introduction Painting and Repainting GDI.

Text metrics

Page 23: Presentation Outline Introduction Painting and Repainting GDI.

Formatting Text

case WM_CREATE:

hdc = GetDC (hwnd) ;

GetTextMetrics (hdc, &tm) ;

cxChar = tm.tmAveCharWidth ; cyChar = tm.tmHeight + tm.tmExternalLeading ;

ReleaseDC (hwnd, hdc) ;

return 0 ;

Page 24: Presentation Outline Introduction Painting and Repainting GDI.

Use of Textout

int iLength ;

TCHAR szBuffer [40] ;

[ other program lines ]

iLength = wsprintf (szBuffer, TEXT ("The sum of %i and %i is %i"), iA, iB, iA + iB) ;

TextOut (hdc, x, y, szBuffer, iLength) ;Combine the two statements into one:

TextOut (hdc, x, y, szBuffer, wsprintf (szBuffer, TEXT ("The sum of %i and %i is %i"),iA, iB, iA + iB)) ;