1 / 24

Presentation Outline

This presentation provides an overview of painting and repainting in Windows, including how the client area is used for drawing visual information. It covers topics such as window messages, WM_PAINT message, invalid rectangles, GDI functions for painting, and obtaining a device context handle.

khegwood
Download Presentation

Presentation Outline

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Presentation Outline • Introduction • Painting and Repainting • GDI

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

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

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

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

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

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

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

  9. Valid and Invalid Rectangles(3) • InvalidateRect. • GetUpdateRect • BeginPaint • ValidateRect

  10. 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"

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

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

  13. 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 ;

  14. 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 ;

  15. 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 ;

  16. 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) ;

  17. The boundaries of the invalid rectangle.

  18. 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) ;

  19. 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."

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

  21. 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 ;

  22. Text metrics

  23. Formatting Text • case WM_CREATE: hdc = GetDC (hwnd) ; GetTextMetrics (hdc, &tm) ; cxChar = tm.tmAveCharWidth ; cyChar = tm.tmHeight + tm.tmExternalLeading ; ReleaseDC (hwnd, hdc) ; return 0 ;

  24. 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)) ;

More Related