











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lecture handout is for Windows Programming course. It was provided by Prof. Jaimini Chinmay at Ambedkar University, Delhi. It includes: Painting, Drawing, Window, System, Dispatch, Application, Function, Structure, Internal, Queue
Typology: Study notes
1 / 19
This page cannot be seen from the preview
Don't miss anything!












The WM_PAINT message is sent when the system or another application makes a request to paint a portion of an application's window. The message is sent by the DispatchMessage function to a window procedure when the application obtains a WM_PAINT message from message Queue by using the GetMessage or PeekMessage functions. A window receives this message through its WindowProc function. Windows always specifies invalid area of any window in terms of a least bounding rectangle; hence, the entire window is not repainted.
WM_PAINT message is generated by the system only when any part of application window becomes invalid.
The WM_PAINT message is generated by the system and should not be sent by an application.
The DefWindowProc function validates the update region. The function may also send the WM_NCPAINT message to the window procedure if the window frame must be painted and send the WM_ERASEBKGND message if the window background must be erased.
The system sends this message when there are no other messages in the application's message queue. DispatchMessage determines where to send the message; GetMessage determines which message to dispatch. GetMessage returns the WM_PAINT message when there are no other messages in the application's message queue, and DispatchMessage sends the message to the appropriate window procedure.
A window may receive internal paint messages as a result of calling RedrawWindow with the RDW_INTERNALPAINT flag set. In this case, the window may not have an update region. An application should call the GetUpdateRect function to determine whether the window has an update region. If GetUpdateRect returns zero, the application should not call the BeginPaint and EndPaint functions.
An application must check for any necessary internal painting by looking at its internal data structures for each WM_PAINT message, because a WM_PAINT message may have been caused by both a non-NULL update region and a call to RedrawWindow with the RDW_INTERNALPAINT flag set.
The system sends an internal WM_PAINT message only once. After an internal WM_PAINT message is returned from GetMessage or PeekMessage or is sent to a window by UpdateWindow, the system does not post or send further WM_PAINT messages until the window is invalidated or until RedrawWindow is called again with the RDW_INTERNALPAINT flag set.
For some common controls, the default WM_PAINT message processing checks the wParam parameter. If wParam is non-NULL, the control assumes that the value is an HDC and paints using that device context.
An application draws in a window at a variety of times: when first creating a window, when changing the size of the window, when moving the window from behind another window, when minimizing or maximizing the window, when displaying data from an opened file, and when scrolling, changing, or selecting a portion of the displayed data.
The system manages actions such as moving and sizing a window. If an action affects the content of the window, the system marks the affected portion of the window as ready for updating and, at the next opportunity, sends a WM_PAINT message to the window procedure of the window. The message is a signal to the application to determine what must be updated and to carry out the necessary drawing.
Some actions are managed by the application, such as displaying open files and selecting displayed data. For these actions, an application can mark for updating the portion of the window affected by the action, causing a WM_PAINT message to be sent at the next opportunity. If an action requires immediate feedback, the application can draw while the action takes place, without waiting for WM_PAINT. For example, a typical application highlights the area the user selects rather than waiting for the next WM_PAINT message to update the area.
In all cases, an application can draw in a window as soon as it is created. To draw in the window, the application must first retrieve a handle to a display device context for the window. Ideally, an application carries out most of its drawing operations during the processing of WM_PAINT messages. In this case, the application retrieves a display device context by calling the BeginPaint function. If an application draws at any other time, such as from within WinMain or during the processing of keyboard or mouse messages, it calls the GetDC or GetDCEx function to retrieve the display DC.
Typically, an application draws in a window in response to a WM_PAINT message. The system sends this message to a window procedure when changes to the window have altered the content of the client area. The system sends the message only if there are no other messages in the application message queue.
Upon receiving a WM_PAINT message, an application can call BeginPaint to retrieve the display device context for the client area and use it in calls to GDI functions to carry out whatever drawing operations are necessary to update the client area. After completing the drawing operations, the application calls the EndPaint function to release the display device context.
user needs immediate feedback, such as when selecting text and dragging or sizing an object. In such cases, the application usually draws while processing keyboard or mouse messages.
To draw in a window without using a WM_PAINT message, the application uses the GetDC or GetDCEx function to retrieve a display device context for the window. With the display device context, the application can draw in the window and avoid intruding into other windows. When the application has finished drawing, it calls the ReleaseDC function to release the display device context for use by other applications.
When drawing without using a WM_PAINT message, the application usually does not invalidate the window. Instead, it draws in such a fashion that it can easily restore the window and remove the drawing. For example, when the user selects text or an object, the application typically draws the selection by inverting whatever is already in the window. The application can remove the selection and restore the original contents of the window by simply inverting again.
The application is responsible for carefully managing any changes it makes to the window. In particular, if an application draws a selection and an intervening WM_PAINT message occurs, the application must ensure that any drawing done during the message does not corrupt the selection. To avoid this, many applications remove the selection, carry out usual drawing operations, and then restore the selection when drawing is complete.
The window background is the color or pattern used to fill the client area before a window begins drawing. The window background covers whatever was on the screen before the window was moved there, erasing existing images and preventing the application's new output from being mixed with unrelated information.
The system paints the background for a window or gives the window the opportunity to do so by sending it a WM_ERASEBKGND message when the application calls BeginPaint. If an application does not process the message but passes it to DefWindowProc , the system erases the background by filling it with the pattern in the background brush specified by the window's class. If the brush is not valid or the class has no background brush, the system sets the fErase member in the PAINTSTRUCT structure that BeginPaint returns, but carries out no other action. The application then has a second chance to draw the window background, if necessary.
If it processes WM_ERASEBKGND , the application should use the message's wParam parameter to draw the background. This parameter contains a handle to the display device context for the window. After drawing the background, the application should return a nonzero value. This ensures that BeginPaint does not erroneously set the fErase member of the PAINTSTRUCT structure to a nonzero value (indicating the background should be erased) when the application processes the subsequent WM_PAINT message.
An application can define a class background brush by assigning a brush handle or a system color value to the hbrBackground member of the WNDCLASS structure when registering the class with the RegisterClass function. The GetStockObject or CreateSolidBrush function can be used to create a brush handle. A system color value can be one of those defined for the SetSysColors function. (The value must be increased by one before it is assigned to the member.)
An application can process the WM_ERASEBKGND message even though a class background brush is defined. This is typical in applications that enable the user to change the window background color or pattern for a specified window without affecting other windows in the class. In such cases, the application must not pass the message to DefWindowProc.
It is not necessary for an application to align brushes, because the system draws the brush using the window origin as the point of reference. Given this, the user can move the window without affecting the alignment of pattern brushes.
The coordinate system for a window is based on the coordinate system of the display device. The basic unit of measure is the device unit (typically, the pixel). Points on the screen are described by x- and y-coordinate pairs. The x-coordinates increase to the right; y-coordinates increase from top to bottom. The origin (0,0) for the system depends on the type of coordinates being used.
The system and applications specify the position of a window on the screen in screen coordinates. For screen coordinates, the origin is the upper-left corner of the screen. The full position of a window is often described by a RECT structure containing the screen coordinates of two points that define the upper-left and lower-right corners of the window.
The system and applications specify the position of points in a window by using client coordinates. The origin in this case is the upper-left corner of the window or client area. Client coordinates ensure that an application can use consistent coordinate values while drawing in the window, regardless of the position of the window on the screen.
The dimensions of the client area are also described by a RECT structure that contains client coordinates for the area. In all cases, the upper-left coordinate of the rectangle is included in the window or client area, while the lower-right coordinate is excluded. Graphics operations in a window or client area are excluded from the right and lower edges of the enclosing rectangle.
Occasionally, applications may be required to map coordinates in one window to those of another window. An application can map coordinates by using the MapWindowPoints function. If one of the windows is the desktop window, the function effectively converts
InvalidateRect function is used to make window or part of it, invalidate.
BOOL InvalidateRect( HWND hWnd, // handle to window CONST RECT *lpRect, // rectangle coordinates BOOL bErase // erase state );
hWnd: Handle to the window whose update region has changed. If this parameter is NULL, the system invalidates and redraws all windows, and sends the WM_ERASEBKGND and WM_NCPAINT messages to the window procedure before the function returns.
lpRect: Pointer to a RECT structure that contains the client coordinates of the rectangle to be added to the update region. If this parameter is NULL, the entire client area is added to the update region.
bErase: Specifies whether the background within the update region is to be erased when the update region is processed. If this parameter is TRUE, the background is erased when the BeginPaint function is called. If this parameter is FALSE, the background remains unchanged.
Return Values: If the function succeeds, the return value is nonzero.If the function fails, the return value is zero.
The PAINTSTRUCT structure contains information for an application. This information can be used to paint the client area of a window owned by that application.
typedef struct tagPAINTSTRUCT { HDC hdc; //Handle to the Device context BOOL fErase; /erase back ground of this parameter is true/ RECT rcPaint; /rectangle to the invalidate region/ BOOL fRestore; BOOL fIncUpdate; //updation true/false BYTE rgbReserved[32]; //rgb values } PAINTSTRUCT, *PPAINTSTRUCT;
hdc Handle to the display DC to be used for painting. fErase Specifies whether the background must be erased. This value is nonzero if the application should erase the background. The application is responsible for
erasing the background if a window class is created without a background brush. For more information, see the description of the hbrBackground member of the WNDCLASS structure. rcPaint Specifies a RECT structure that specifies the upper left and lower right corners of the rectangle in which the painting is requested, in device units relative to the upper-left corner of the client area. fRestore Reserved; used internally by the system. fIncUpdate Reserved; used internally by the system. rgbReserved Reserved; used internally by the system.
The DrawText function draws formatted text in the specified rectangle. It formats the text according to the specified method (expanding tabs, justifying characters, breaking lines, and so forth).
int DrawText( HDC hDC , // handle to DC LPCTSTR lpString , // text to draw int nCount , // text length LPRECT lpRect , // formatting dimensions UINT uFormat // text-drawing options );
hDC: Handle to the device context.
lpString: Pointer to the string that specifies the text to be drawn. If the nCount parameter is –1, the string must be null-terminated.
If uFormat includes DT_MODIFYSTRING, the function could add up to four additional characters to this string. The buffer containing the string should be large enough to accommodate these extra characters.
nCount: Specifies the length of the string. For the ANSI function it is a BYTE count and for the Unicode function it is a WORD count. Note that for the ANSI function, characters in SBCS code pages take one byte each, while most characters in DBCS code pages take two bytes; for the Unicode function, most currently defined Unicode characters (those in the Basic Multilingual Plane (BMP)) are one WORD while Unicode surrogates are two WORD s. If nCount is –1, then the lpString parameter is assumed to be a pointer to a null-terminated string and DrawText computes the character count automatically.
number of characters per tab is eight. The DT_WORD_ELLIPSIS, DT_PATH_ELLIPSIS, and DT_END_ELLIPSIS values cannot be used with the DT_EXPANDTABS value.
DT_EXTERNALLEADING Includes the font external leading in line height. Normally, external leading is not included in the height of a line of text.
DT_HIDEPREFIX Windows 2000/XP: Ignores the ampersand (&) prefix character in the text. The letter that follows will not be underlined, but other mnemonic-prefix characters are still processed. For example: input string: "A&bc&&d" normal: "Abc&d" DT_HIDEPREFIX: "Abc&d"
Compare with DT_NOPREFIX and DT_PREFIXONLY.
DT_INTERNAL Uses the system font to calculate text metrics.
DT_LEFT Aligns text to the left.
DT_MODIFYSTRING Modifies the specified string to match the displayed text. This value has no effect unless DT_END_ELLIPSIS or DT_PATH_ELLIPSIS is specified.
DT_NOCLIP Draws without clipping. DrawText is somewhat faster when DT_NOCLIP is used.
DT_NOFULLWIDTHCHARBREAK (^) Windows 98/Me, Windows 2000/XP: Prevents a line break at a DBCS (double- wide character string), so that the line breaking rule is equivalent to SBCS strings. For example, this can be used in Korean windows, for more readability of icon labels. This value has no effect unless DT_WORDBREAK is specified.
DT_NOPREFIX Turns off processing of prefix characters. Normally, DrawText interprets the mnemonic-prefix character & as a directive to underscore the character that follows, and the mnemonic-prefix characters && as a directive to print a single &. By specifying DT_NOPREFIX, this processing is turned off. For example,
input string: "A&bc&&d" normal: "Abc&d" DT_NOPREFIX: "A&bc&&d"
Compare with DT_HIDEPREFIX and DT_PREFIXONLY.
DT_PATH_ELLIPSIS For displayed text, replaces characters in the middle of the string with ellipses so that the result fits in the specified rectangle. If the string contains backslash () characters, DT_PATH_ELLIPSIS preserves as much as possible of the text after the last backslash.
The string is not modified unless the DT_MODIFYSTRING flag is specified.
Compare with DT_END_ELLIPSIS and DT_WORD_ELLIPSIS.
DT_PREFIXONLY Windows 2000/XP: Draws only an underline at the position of the character following the ampersand (&) prefix character. Does not draw any other characters in the string. For example, input string: "A&bc&&d" normal: "Abc&d" DT_PREFIXONLY: " _ "
Compare with DT_HIDEPREFIX and DT_NOPREFIX.
DT_RIGHT Aligns text to the right.
DT_RTLREADING Layout in right-to-left reading order for bi- directional text when the font selected into the hdc is a Hebrew or Arabic font. The default reading order for all text is left-to- right.
DT_SINGLELINE Displays text on a single line only. Carriage returns and line feeds do not break the line.
DT_TABSTOP Sets tab stops. Bits 15–8 (high-order byte of the low-order word) of the uFormat parameter specify the number of characters for each tab. The default number of characters per tab is eight. The DT_CALCRECT, DT_EXTERNALLEADING,
The TabbedTextOut function writes a character string at a specified location, expanding tabs to the values specified in an array of tab-stop positions. Text is written in the currently selected font, background color, and text color.
LONG TabbedTextOut( HDC hDC , // handle to DC int X , // x-coord of start int Y , // y-coord of start LPCTSTR lpString , // character string int nCount , // number of characters int nTabPositions , // number of tabs in array CONST LPINT lpnTabStopPositions , // array of tab positions int nTabOrigin // start of tab expansion );
hDC: Handle to the device context.
X: Specifies the x-coordinate of the starting point of the string, in logical units.
Y: Specifies the y-coordinate of the starting point of the string, in logical units.
lpString: Pointer to the character string to draw. The string does not need to be zero- terminated, since nCount specifies the length of the string.
nCount: Specifies the length of the string pointed to by lpString. For the ANSI function it is a BYTE count and for the Unicode function it is a WORD count. Note that for the ANSI function, characters in SBCS code pages take one byte each, while most characters in DBCS code pages take two bytes; for the Unicode function, most currently defined Unicode characters (those in the Basic Multilingual Plane (BMP)) are one WORD while Unicode surrogates are two WORD s.
nTabPositions: Specifies the number of values in the array of tab-stop positions.
lpnTabStopPositions: Pointer to an array containing the tab-stop positions, in logical units. The tab stops must be sorted in increasing order; the smallest x-value should be the first item in the array.
nTabOrigin: Specifies the x-coordinate of the starting position from which tabs are expanded, in logical units.
Return Values: If the function succeeds, the return value is the dimensions, in logical units, of the string. The height is in the high-order word and the width is in the low-order word. If the function fails, the return value is zero.
If the nTabPositions parameter is zero and the lpnTabStopPositions parameter is NULL, tabs are expanded to eight times the average character width.
If nTabPositions is 1, the tab stops are separated by the distance specified by the first value in the lpnTabStopPositions array.
If the lpnTabStopPositions array contains more than one value, a tab stop is set for each value in the array, up to the number specified by nTabPositions.
The nTabOrigin parameter allows an application to call the TabbedTextOut function several times for a single line. If the application calls TabbedTextOut more than once with the nTabOrigin set to the same value each time, the function expands all tabs relative to the position specified by nTabOrigin.
By default, the current position is not used or updated by the TabbedTextOut function. If an application needs to update the current position when it calls TabbedTextOut , the application can call the SetTextAlign function with the wFlags parameter set to TA_UPDATECP. When this flag is set, the system ignores the X and Y parameters on subsequent calls to the TabbedTextOut function, using the current position instead.
Primitive shapes include: Lines and Curves, filled shapes like:
Ellipse, Chord, Pie, Polygon, Rectangles
Line can be drawn using MoveToEx and LineTo Function. MoveToEx function moves the points at specified location.
Note: MoveToEx effects all drawing functions.
The LineTo function draws a line from the current position up to, but not including, the specified point.
BOOL LineTo( HDC hdc , // device context handle int nXEnd , // x-coordinate of ending point int nYEnd // y-coordinate of ending point );
hdc: Handle to a device context. nXEnd: Specifies the x-coordinate, in logical units, of the line's ending point. nYEnd: Specifies the y-coordinate, in logical units, of the line's ending point.
Return Values: If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.
fnObject: Specifies the type of stock object. This parameter can be one of the following values.
Value Meaning BLACK_BRUSH Black brush. DKGRAY_BRUSH Dark gray brush. DC_BRUSH Windows 2000/XP: Solid color brush. The default color is white. The color can be changed by using the SetDCBrushColor function. For more information, see the Remarks section. GRAY_BRUSH Gray brush. HOLLOW_BRUSH Hollow brush (equivalent to NULL_BRUSH). LTGRAY_BRUSH Light gray brush. NULL_BRUSH Null brush (equivalent to HOLLOW_BRUSH). WHITE_BRUSH White brush. BLACK_PEN Black pen. DC_PEN Windows 2000/XP: Solid pen color. The default color is white. The color can be changed by using the SetDCPenColor function. For more information, see the Remarks section. WHITE_PEN White pen. ANSI_FIXED_FONT Windows fixed-pitch (monospace) system font. ANSI_VAR_FONT Windows variable-pitch (proportional space) system font. DEVICE_DEFAULT_FONT Windows NT/2000/XP: Device-dependent font. DEFAULT_GUI_FONT Default font for user interface objects such as menus and dialog boxes. This is MS Sans Serif. Compare this with SYSTEM_FONT. OEM_FIXED_FONT Original equipment manufacturer (OEM) dependent fixed-pitch (monospace) font. SYSTEM_FONT System font. By default, the system uses the system font to draw menus, dialog box controls, and text.
Windows 95/98 and Windows NT: The system font is MS Sans Serif.
Windows 2000/XP: The system font is Tahoma SYSTEM_FIXED_FONT Fixed-pitch (monospace) system font. This stock object is provided only for compatibility with 16-bit Windows versions earlier than 3.0. DEFAULT_PALETTE Default palette. This palette consists of the static colors in the system palette.
Return Values: If the function succeeds, the return value is a handle to the requested logical object. If the function fails, the return value is NULL.
Use the DKGRAY_BRUSH, GRAY_BRUSH, and LTGRAY_BRUSH stock objects only in windows with the CS_HREDRAW and CS_VREDRAW styles. Using a gray stock brush in any other style of window can lead to misalignment of brush patterns after a window is moved or sized. The origins of stock brushes cannot be adjusted.
The HOLLOW_BRUSH and NULL_BRUSH stock objects are equivalent.
The font used by the DEFAULT_GUI_FONT stock object could change. Use this stock object when you want to use the font that menus, dialog boxes, and other user interface objects use.
The SelectObject function selects an object into the specified device context (DC). The new object replaces the previous object of the same type.
HGDIOBJ SelectObject( HDC hdc , // handle to DC HGDIOBJ hgdiobj // handle to object );
hdc: Handle to the DC. Hgdiobj: Handle to the object to be selected. The specified object must have been created by using one of the following functions.
Object Functions Bitmap Created by any bitmap function like CreateBitmap , CreateCompatibleBitmap and CreateDIBSection etc.
(Bitmaps can be selected for memory DCs only, and for only one DC at a time.) Brush Created by CreateBrushIndirect or CreateSolidBrush Font Created by CreateFont function Pen Created by CreatePen Region Created by any region function e.g. CreatePolygonRgn , CreateRectRgn , CreateRectRgnIndirect
Return Values: If the selected object is not a region and the function succeeds, the return value is a handle to the object being replaced.
If an error occurs and the selected object is not a region, the return value is NULL. Otherwise, it is HGDI_ERROR.