visual programming – unit i
TRANSCRIPT
Visual Programming – Unit IVisual Programming – Unit I
J.Brindha, J.Brindha,
Lecturer,Lecturer,
IT Department.IT Department.
Presentation OutlinePresentation Outline
► ConceptsConcepts► Painting and RepaintingPainting and Repainting► Graphics Device Interface (GDI)Graphics Device Interface (GDI)► Device ContextDevice Context► DrawingDrawing
ConceptsConcepts
► Client area Client area ► Size Size ► Painting Painting ► Attributes Attributes ► Windows – message driven systemWindows – message driven system
Painting & RepaintingPainting & Repainting
► Drawing text and Graphics in client areaDrawing text and Graphics in client area► Another application’s window covering our Another application’s window covering our
windowwindow► WM_PAINT messageWM_PAINT message
The WM_PAINT MessageThe WM_PAINT Message
► WM_PAINT message – Invokes windows WM_PAINT message – Invokes windows procedure to paint client areaprocedure to paint client area
► Thereafter, window procedure is set ready Thereafter, window procedure is set ready to process additional WM_PAINT messages to process additional WM_PAINT messages and repaint the entire client areaand repaint the entire client area
► The program should accumulate all the The program should accumulate all the information necessary to paint the client information necessary to paint the client areaarea
► EventsEvents Hidden area of the window is brought Hidden area of the window is brought
into viewinto view The user resizes the window The user resizes the window The program uses the The program uses the ScrollWindowScrollWindow or or
ScrollDCScrollDC function function The program uses the The program uses the InvalidateRectInvalidateRect or or
InvalidateRgnInvalidateRgn function function
The WM_PAINT MessageThe WM_PAINT Message ► Events Contd. Events Contd.
Windows removes a dialog box or Windows removes a dialog box or message box that was overlaying part of message box that was overlaying part of the window. the window.
A menu is pulled down and then A menu is pulled down and then released. released.
A tool tip is displayed. A tool tip is displayed.
Valid and Invalid RectanglesValid and Invalid Rectangles
► Happens when a dialog box overlies part of Happens when a dialog box overlies part of the client area. the client area.
► Repainting is requiredRepainting is required► Termed as "invalid region" or "update region.“Termed as "invalid region" or "update region.“► Prompts windows to place WM_PAINT Prompts windows to place WM_PAINT
message in the application's message queuemessage in the application's message queue► Windows internally maintains a "paint Windows internally maintains a "paint
information structure" for each window. This information structure" for each window. This structure contains the coordinates of the structure contains the coordinates of the smallest rectangle smallest rectangle
► Stores updated coordinates in case of a new Stores updated coordinates in case of a new invalid regioninvalid region
► No multiple WM_PAINT messages in the No multiple WM_PAINT messages in the message queuemessage queue
Valid and Invalid RectanglesValid and Invalid Rectangles
► A window procedure can invalidate a A window procedure can invalidate a rectangle in its own client area by calling rectangle in its own client area by calling InvalidateRectInvalidateRect. .
► GetUpdateRectGetUpdateRect returns the coordinates. returns the coordinates.► BeginPaintBeginPaint - the entire client area is - the entire client area is
validated. validated. ► ValidateRectValidateRect – validates rectangular area – validates rectangular area► WM_PAINT message currently in the queue WM_PAINT message currently in the queue
is removed. is removed.
GDIGDI
► Windows' Graphics Device Interface (GDI) Windows' Graphics Device Interface (GDI) functionsfunctions
► Various GDI functions for writing text strings to Various GDI functions for writing text strings to the client area of the windowthe client area of the window
► TextOut (hdc, x, y, psText, iLength) ; TextOut (hdc, x, y, psText, iLength) ; TextOutTextOut writes a character string to the client writes a character string to the client area of the window. area of the window. The The psTextpsText argument is a pointer to the argument is a pointer to the
character string, and character string, and iLengthiLength is the length of the string in is the length of the string in
characters. characters. The The xx and and yy arguments define the starting arguments define the starting
position of the character string in the client position of the character string in the client area. area.
The The hdchdc argument is a "handle to a device argument is a "handle to a device contextcontext
The Device ContextThe Device Context
► DC, data structure maintained internally DC, data structure maintained internally by GDI.by GDI.
► Associated with a display device, such as a Associated with a display device, such as a video display or a printer. video display or a printer.
► Some of the values in the device context Some of the values in the device context are graphics "attributes“. are graphics "attributes“.
► ForTextOut ForTextOut the attributes of the DC the attributes of the DC determinedetermine1. Color of the text1. Color of the text2. Background color2. Background color3. X & Y coordinates3. X & Y coordinates4. Font4. Font
Getting a Device Context Handle: Getting a Device Context Handle: Method 1Method 1
► Two functions are involved: Two functions are involved: BeginPaintBeginPaint and and EndPaintEndPaint. .
► Parameters passed are Parameters passed are 1. Handle to the window,1. Handle to the window,2. Address of a structure variable of type 2. Address of a structure variable of type PAINTSTRUCTPAINTSTRUCT PAINTSTRUCT ps ; PAINTSTRUCT ps ;
► BeginPaintBeginPaint function – Erase the background of function – Erase the background of the invalid region the invalid region
The function also fills in the fields of the The function also fills in the fields of the psps structure. structure.
Returns device context handleReturns device context handle saved in a variable, saved in a variable, hdchdc. .
HDC hdc ; HDC hdc ;
Getting a Device Context Handle: Getting a Device Context Handle: Method 1Method 1
► The HDC data type is defined as a 32-bit The HDC data type is defined as a 32-bit unsigned integer. The program may then use GDI unsigned integer. The program may then use GDI functions, such as functions, such as TextOutTextOut, that require the , that require the handle to the device context. A call to handle to the device context. A call to EndPaintEndPaint releases releases
the device context handle. the device context handle. ► Typically, processing of the WM_PAINT message looks Typically, processing of the WM_PAINT message looks
like this: like this: case WM_PAINT: case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ; hdc = BeginPaint (hwnd, &ps) ; [use GDI functions][use GDI functions]
EndPaint (hwnd, &ps) ; EndPaint (hwnd, &ps) ; return 0 ; return 0 ;
Getting a Device Context Handle: Getting a Device Context Handle: Method 1Method 1
► If a window procedure does not process If a window procedure does not process WM_PAINT messages, it must pass the WM_PAINT messages, it must pass the WM_PAINT message to WM_PAINT message to DefWindowProcDefWindowProc, the default , the default
window procedure window procedure
case WM_PAINT: case WM_PAINT: BeginPaint (hwnd, &ps) ; BeginPaint (hwnd, &ps) ; EndPaint (hwnd, &ps) ; EndPaint (hwnd, &ps) ; return 0 ;return 0 ;
The Paint Information The Paint Information StructureStructure
► Windows maintains a paint information Windows maintains a paint information structure for each window. structure for each window.
► typedef struct tagPAINTSTRUCTtypedef struct tagPAINTSTRUCT{ {
HDC hdc ; HDC hdc ; BOOL fErase ; BOOL fErase ; RECT rcPaint ; RECT rcPaint ; BOOL fRestore ; BOOL fRestore ;
BOOL fIncUpdate ; BOOL fIncUpdate ; BYTE rgbReserved[32] ;BYTE rgbReserved[32] ;} PAINTSTRUCT ; } PAINTSTRUCT ;
The boundaries of the invalid rectangleThe boundaries of the invalid rectangle
Getting a Device Context Handle: Getting a Device Context Handle: Method 2Method 2
► To get a handle to the device context of the To get a handle to the device context of the client area of the window, you call client area of the window, you call GetDCGetDC to to obtain the handle and obtain the handle and ReleaseDCReleaseDC after after you're done with it: you're done with it:
► hdc = GetDC (hwnd) ;hdc = GetDC (hwnd) ;
[use GDI functions][use GDI functions]
ReleaseDC (hwnd, hdc) ;ReleaseDC (hwnd, hdc) ;
Text metricsText metrics
► defined in WINGDI.Hdefined in WINGDI.H► Has 20 fieldsHas 20 fields
typedef struct tagTEXTMETRICtypedef struct tagTEXTMETRIC{{ LONG tmHeight ; LONG tmHeight ;
LONG tmAscent ; LONG tmAscent ; LONG tmDescent ; LONG tmDescent ; LONG tmInternalLeading ; LONG tmInternalLeading ;
LONG tmExternalLeading ; LONG tmExternalLeading ; LONG tmAveCharWidth ; LONG tmAveCharWidth ; LONG tmMaxCharWidth ; LONG tmMaxCharWidth ;
[other structure fields][other structure fields] }TEXTMETRIC, * PTEXTMETRIC ;}TEXTMETRIC, * PTEXTMETRIC ;
The GDI PrimitivesThe GDI Primitives
•GDI ObjectsPensBrushes BitmapsRegionsFontsPalettes
•ProcedureCreate the objectSelect the object Deselect & Destroy the object
Sample
HPEN hPen ; hPen = CreatePen (iPenStyle, iWidth, crColor) ; hPen = GetStockObject (WHITE_PEN) SelectObject (hdc, hPen) ; DeleteObject (hPen) ; hPen = GetCurrentObject (hdc, OBJ_PEN) ;
Some more functionsSome more functions
► CreateICCreateIC► CreateCompatibleDC , DeleteDC CreateCompatibleDC , DeleteDC ► GetDeviceCapsGetDeviceCaps► CreateMetaFile ,CloseMetaFileCreateMetaFile ,CloseMetaFile► SaveDC SaveDC ► RestoreDCRestoreDC
DrawingDrawing
► setPixel & getPixelsetPixel & getPixel► FunctionsFunctions
LineToLineTo PolylinePolyline and Polyline -and Polyline - series of connected series of connected
straight lines. straight lines. PolyPolylinePolyPolyline - multiple polylines. - multiple polylines. ArcArc - elliptical lines. - elliptical lines. PolyBezierPolyBezier and PolyBezierToand PolyBezierTo - Bezier splines. - Bezier splines. ArcTo and AngleArcArcTo and AngleArc - elliptical lines. - elliptical lines. PolyDrawPolyDraw - series of connected straight lines - series of connected straight lines
and Bezier splines and Bezier splines
•RectangleRectangle - a rectangle. - a rectangle. •EllipseEllipse - an ellipse. - an ellipse. •RoundRectRoundRect - a rectangle with rounded - a rectangle with rounded corners. corners. •PiePie - a part of an ellipse that looks like - a part of an ellipse that looks like a pie slice. a pie slice. •ChordChord - part of an ellipse formed by a - part of an ellipse formed by a chord.chord.
Some more functionsSome more functions
► MoveToEx (hdc, x, y, NULL) MoveToEx (hdc, x, y, NULL) ► GetCurrentPositionEx (hdc, &pt) GetCurrentPositionEx (hdc, &pt) ► Polyline (hdc, apt, 5) Polyline (hdc, apt, 5) ► Ellipse (hdc, xLeft, yTop, xRight, yBottom) Ellipse (hdc, xLeft, yTop, xRight, yBottom) ► Rectangle (hdc, xLeft, yTop, xRight, Rectangle (hdc, xLeft, yTop, xRight,
yBottom) yBottom) ► xCornerEllipse = (xRight - xLeft) / 4 xCornerEllipse = (xRight - xLeft) / 4 ► yCornerEllipse = (yBottom- yTop) / 4 yCornerEllipse = (yBottom- yTop) / 4 ► RoundRect (hdc, xLeft, yTop, xRight, RoundRect (hdc, xLeft, yTop, xRight,
yBottom, xCornerEllipse, yCornerEllipse) yBottom, xCornerEllipse, yCornerEllipse)
►GDI Mapping ModesGDI Mapping Modes8 modes8 modesCoordinates -> logical units -> Device Coordinates -> logical units -> Device
units/pixelsunits/pixelssetMapModesetMapModeGetMapMode GetMapMode
More FunctionsMore Functions
► FillRect (hdc, &rect, hBrush) ; FillRect (hdc, &rect, hBrush) ; ► FrameRect (hdc, &rect, hBrush) ;FrameRect (hdc, &rect, hBrush) ;► InvertRect (hdc, &rect) ; InvertRect (hdc, &rect) ; ► Move a rectangle a number of units along Move a rectangle a number of units along
the the xx and and yy axes: OffsetRect (&rect, x, y) ; axes: OffsetRect (&rect, x, y) ; ► Increase or decrease the size of a rectangle: Increase or decrease the size of a rectangle:
InflateRect (&rect, x, y) ; InflateRect (&rect, x, y) ; ► Set the fields of a rectangle equal to 0: Set the fields of a rectangle equal to 0:
SetRectEmpty (&rect) ; SetRectEmpty (&rect) ; ► Copy one rectangle to another: CopyRect Copy one rectangle to another: CopyRect
(&DestRect, &SrcRect) ; (&DestRect, &SrcRect) ;
•Obtain the intersection of two rectangles: Obtain the intersection of two rectangles: IntersectRect (&DestRect, &SrcRect1, IntersectRect (&DestRect, &SrcRect1, &SrcRect2) ; &SrcRect2) ; •Obtain the union of two rectangles: Obtain the union of two rectangles: UnionRect (&DestRect, &SrcRect1, &SrcRect2) UnionRect (&DestRect, &SrcRect1, &SrcRect2) ; ; •Determine whether a rectangle is empty: Determine whether a rectangle is empty: bEmpty = IsRectEmpty (&rect) ; bEmpty = IsRectEmpty (&rect) ; •Determine whether a point is in a rectangle: Determine whether a point is in a rectangle: bInRect = PtInRect (&rect, point) bInRect = PtInRect (&rect, point) •SelectClipRgn (hdc, hRgn) ;SelectClipRgn (hdc, hRgn) ;