GDI and Device Context

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 17

Visual Programming – Unit I

J.Brindha,
Lecturer,
IT Department.
Presentation Outline

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

• Client area
• Size
• Painting
• Attributes
• Windows – message driven system
Painting & Repainting
• Drawing text and Graphics in client area
• Another application’s window covering our
window
• WM_PAINT message
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
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.
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
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.
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
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 text
2. Background color
3. X & Y coordinates
4. Font
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 ;
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 ;
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 ;
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 ;
The boundaries of the invalid
rectangle.
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) ;
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 ;

You might also like