17
Visual Programming – Unit I J.Brindha, Lecturer, IT Department.

GDI and Device Context

Embed Size (px)

Citation preview

Page 1: GDI and Device Context

Visual Programming – Unit I

J.Brindha,

Lecturer,

IT Department.

Page 2: GDI and Device Context

Presentation Outline

• Concepts• Painting and Repainting• Graphics Device Interface (GDI)• Device Context• Drawing

Page 3: GDI and Device Context

Concepts

• Client area • Size • Painting • Attributes • Windows – message driven system

Page 4: GDI and Device Context

Painting & Repainting

• Drawing text and Graphics in client area

• Another application’s window covering our window

• WM_PAINT message

Page 5: GDI and Device Context

The WM_PAINT Message

• WM_PAINT message – Invokes windows procedure to paint client area

• Thereafter, window procedure is set ready to process additional WM_PAINT messages and repaint the entire client area

• The program should accumulate all the information necessary to paint the client area

• Events– Hidden area of the window is brought into view– The user resizes the window – The program uses the ScrollWindow or ScrollDC

function– The program uses the InvalidateRect or InvalidateRgn

function

Page 6: GDI and Device Context

The WM_PAINT Message (2)

• Events Contd. – 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 7: GDI and Device Context

Valid and Invalid Rectangles

• Happens when a dialog box overlies part of the client area.

• Repainting is required• Termed as "invalid region" or "update region.“• Prompts windows to place WM_PAINT message in the

application's message queue• Windows internally maintains a "paint information

structure" for each window. This structure contains the coordinates of the smallest rectangle

• Stores updated coordinates in case of a new invalid region

• No multiple WM_PAINT messages in the message queue

Page 8: GDI and Device Context

Valid and Invalid Rectangles(3)• A window procedure can invalidate a

rectangle in its own client area by calling InvalidateRect.

• GetUpdateRect returns the coordinates.• BeginPaint - the entire client area is

validated. • ValidateRect – validates rectangular area• WM_PAINT message currently in the

queue is removed.

Page 9: GDI and Device Context

GDI• Windows' Graphics Device Interface (GDI)

functions• Various 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 10: GDI and Device Context

The Device Context

• DC, data structure maintained internally by GDI.• Associated with a display device, such as a

video display or a printer. • Some of the values in the device context are

graphics "attributes“. • ForTextOut the attributes of the DC determine

1. Color of the text2. Background color3. X & Y coordinates4. Font

Page 11: GDI and Device Context

Getting a Device Context Handle: Method 1

• Two functions are involved: BeginPaint and EndPaint. • Parameters passed are

1. Handle to the window,2. Address of a structure variable of type PAINTSTRUCT,

defined in the WINUSER.H header file. 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. This is commonly saved in a variable named hdc. You define this variable in your window procedure like so:

HDC hdc ;

Page 12: GDI and Device Context

Getting a Device Context Handle: Method 1

• 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.

• Typically, processing of the WM_PAINT message looks like this:

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

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

Page 13: GDI and Device Context

Getting a Device Context Handle: Method 1

• If a window procedure does not process WM_PAINT messages, it must pass the WM_PAINT message to DefWindowProc, the default window procedure

case WM_PAINT: BeginPaint (hwnd, &ps) ; EndPaint (hwnd, &ps) ; return 0 ;

Page 14: GDI and Device Context

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 15: GDI and Device Context

The boundaries of the invalid rectangle.

Page 16: GDI and Device Context

Getting a Device Context Handle: Method 2

• 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 17: GDI and Device Context

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 ;