CS 1253 Visual Programming Unit I Topic 4
- Slides: 56
CS 1253 Visual Programming Unit I Topic 4 BASIC DRAWING
Presentation Outline The Device Context Getting a Device context Handle Getting Device Context Information The Device Context Attributes
Introduction The subsystem of Microsoft Windows responsible for displaying graphics on video displays and printers is known as the Graphics Device Interface (GDI). GDI is an extremely important part of Windows. Not only do the applications you write for Windows use GDI for the display of visual information, but Windows itself uses GDI for the visual display of user interface items such as menus, scroll bars, icons, and mouse cursors.
The Device Context When you want to draw on a graphics output device such as the screen or printer, you must first obtain a handle to a device context (or DC). In giving your program this handle, Windows is giving you permission to use the device. You then include the handle as an argument to the GDI functions to identify to Windows the device on which you wish to draw. The device context contains many "attributes" that determine how the GDI functions work on the device. These attributes allow GDI functions to have just a few arguments, such as starting coordinates. The GDI functions do not need arguments for everything else that Windows needs to display the object on the device.
The Device Context For example, when you call Text. Out, you need specify in the function only the device context handle, the starting coordinates, the text, and the length of the text. You don't need to specify the font, the color of the text, the color of the background behind the text, or the intercharacter spacing. These are all attributes that are part of the device context. When you want to change one of these attributes, you call a function that does so. Subsequent Text. Out calls to that device context use the new attribute.
Getting a Device Context Handle Windows provides several methods for obtaining a device context handle. If you obtain a video display device context handle while processing a message, you should release it before exiting the window procedure. After you release the handle, it is no longer valid. The most common method for obtaining a device context handle and then releasing it involves using the Begin. Paint and End. Paint calls when processing the WM_PAINT message: hdc = Begin. Paint (hwnd, &ps) ; [other program lines] End. Paint (hwnd, &ps) ;
Getting a Device Context Handle Windows programs can also obtain a handle to a device context while processing messages other than WM_PAINT: hdc = Get. DC (hwnd) ; [other program lines] Release. DC (hwnd, hdc) ;
Getting a Device Context Handle (2) The Begin. Paint, Get. DC, and Get. Window. DC calls obtain a device context associated with a particular window on the video display. A much more general function for obtaining a handle to a device context is Create. DC: hdc = Create. DC (psz. Driver, psz. Device, psz. Output, p. Data) ; [other program lines] Delete. DC (hdc) ;
Getting Device Context Information A device context usually refers to a physical display device such as a video display or a printer. Often, you need to obtain information about this device, including the size of the display, in terms of both pixels and physical dimensions, and its color capabilities. You can get this information by calling the Get. Device. Cap ("get device capabilities") function: i. Value = Get. Device. Caps (hdc, i. Index) ;
Getting Device Context Information The i. Index argument is one of 29 identifiers defined in the WINGDI. H header file. For example, the i. Index value of HORZRES causes Get. Device. Caps to return the width of the device in pixels; a VERTRES argument returns the height of the device in pixels. If hdc is a handle to a screen device context, that's the same information you can get from Get. System. Metrics. If hdc is a handle to a printer device context, Get. Device. Caps returns the height and width of the printer display area in pixels. You can also use Get. Device. Caps to determine the device's capabilities of processing various types of graphics.
DEVCAPS 1 display for a 256 -color, 640 -by 480 VGA.
The Size of the Device The Get. Device. Caps function helps you obtain information regarding the physical size of the output device, be it the video display or printer. Within a Windows program you can use the Get. Device. Caps function to obtain the assumed resolution in dots per inch that the user selected in the Display applet of the Control Panel
Finding Out About Color A video display capable of displaying only black pixels and white pixels requires only one bit of memory per pixel. Color displays require multiple bits per pixels. The more bits, the more colors; or more specifically, the number of unique simultaneous colors is equal to 2 to the number of bits per pixel. i. Bits. Pixel = Get. Device. Caps (hdc, BITSPIXEL) ; i. Colors = Get. Device. Caps (hdc, NUMCOLORS) ;
The Device Context Attributes Device Context Attribute Default Function(s) to Change Function to Obtain Mapping Mode MM_TEXT Set. Map. Mode Get. Map. Mode Window Origin (0, 0) Set. Window. Org. Ex Offset. Window. Org. Ex Get. Window. Org. Ex Viewport Origin (0, 0) Set. Viewport. Org. Ex Offset. Viewport. Org. Ex Get. Viewport. Org. Ex Window Extents (1, 1) Set. Window. Ext. Ex Set. Map. Mode Scale. Window. Ext. Ex Get. Window. Ext. Ex Viewport Extents (1, 1) Set. Viewport. Ex Set. Map. Mode Scale. Viewport. Ex Get. Viewport. Ex Pen BLACK_PEN Select. Object Brush WHITE_BRUSH Select. Object Font SYSTEM_FONT Select. Object Bitmap None Select. Object Current Position (0, 0) Move. To. Ex Line. To Polyline. To Poly. Bezier. To Get. Current. Position. Ex Background Mode OPAQUE Set. Bk. Mode Get. Bk. Mode Background Color White Set. Bk. Color Get. Bk. Color Text Color Black Set. Text. Color Get. Text. Color
Saving Device Contexts Normally when you call Get. DC or Begin. Paint, Windows gives you a device context with default values for all the attributes. Any changes you make to the attributes are lost when the device context is released with the Release. DC or End. Paint call. If your program needs to use non-default device context attributes, you'll have to initialize the device context every time you obtain a new device context handle:
Saving Device Contexts Although this approach is generally satisfactory, you might prefer that changes you make to the attributes be saved when you release the device context so that they will be in effect the next time you call Get. DC or Begin. Paint. You can accomplish this by including the CS_OWNDC flag as part of the window class style when you register the window class: wndclass. style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC ;
Saving Device Contexts Now each window that you create based on this window class will have its own private device context that continues to exist when the window is destroyed. When you use the CS_OWNDC style, you need to initialize the device context attributes only once, perhaps while processing the WM_CREATE message: case WM_CREATE: hdc = Get. DC (hwnd) ; [initialize device context attributes] Release. DC (hwnd, hdc) ; The attributes continue to be valid until you change them.
Saving Device Contexts In some cases you might want to change certain device context attributes, do some painting using the changed attributes, and then revert to the original device context. To simplify this process, you save the state of a device context by calling id. Saved = Save. DC (hdc) ; Now you can change some attributes. When you want to return to the device context as it existed before the Save. DC call, you use Restore. DC (hdc, id. Saved) ; You can call Save. DC any number of times before you call Restore. DC.
Presentation Outline Drawing Dots and Lines Setting Pixels Straight Lines The Bounding Box Functions Bezier Splines Using Stock Pens Creating, Selecting, and Deleting Pens Filling in the Gaps Drawing Modes Drawing Filled Areas The Polygon Function and the Polygon-Filling Mode Brushing the Interior
Presentation Outline The GDI Mapping Mode Device Coordinates and Logical Coordinates The Device Coordinate Systems The Viewport and the Window Working with MM_TEXT The Metric Mapping Modes The "Roll Your Own" Mapping Modes The MM_ISOTROPIC Mapping Mode MM_ANISOTROPIC: Stretching the Image to Fit Rectangles, Regions, and Clipping Working with Rectangles Random Rectangles Creating and Painting Regions Clipping with Rectangles and Regions
Drawing Dots and Lines Windows Graphics Device Interface makes use of device drivers for the graphics output devices attached to your computer. In theory, all that a graphics device driver needs for drawing is a Set. Pixel function and a Get. Pixel function. The only problem is performance. A function that is several calls away from each Set. Pixel function will be painfully slow.
Setting Pixels The Set. Pixel function sets the pixel at a specified x- and ycoordinate to a particular color: Set. Pixel (hdc, x, y, cr. Color) ; The Get. Pixel function returns the color of the pixel at the specified coordinate position: cr. Color = Get. Pixel (hdc, x, y) ;
Straight Lines Windows can draw straight lines, elliptical lines (curved lines on the circumference of an ellipse), and Bezier splines. Windows 98 supports seven functions that draw lines: Line. To Draws a straight line. Polyline and Polyline. To Draw a series of connected straight lines. Polyline Draws multiple polylines. Arc Draws elliptical lines. Poly. Bezier and Poly. Bezier. To Draw Bezier splines.
Straight Lines Five attributes of the device context affect the appearance of lines that you draw using these functions: �current pen position (for Line. To, Polyline. To, Poly. Bezier. To, and Arc. To only), �pen, �background mode, �background color, and �drawing mode.
Straight Lines To draw a straight line, you must call two functions. The first function specifies the point at which the line begins, and the second function specifies the end point of the line Move. To. Ex (hdc, x. Beg, y. Beg, NULL) ; Line. To (hdc, x. End, y. End) ; Move. To. Ex doesn't actually draw anything; The Line. To function then draws a straight line from the current position to the point specified in the Line. To function. If you ever need the current position Get. Current. Position. Ex (hdc, &pt) ; where pt is a POINT structure.
Straight Lines The following code draws a grid in the client area of a window, spacing the lines 100 pixels apart starting from the upper left corner. The variable hwnd is assumed to be a handle to the window, hdc is a handle to the device context, and x and y are integers: Get. Client. Rect (hwnd, &rect) ; for (x = 0 ; x < rect. right ; x+= 100) { Move. To. Ex (hdc, x, 0, NULL) ; Line. To (hdc, x, rect. bottom) ; } for (y = 0 ; y < rect. bottom ; y += 100) { Move. To. Ex (hdc, 0, y, NULL) ; Line. To (hdc, rect. right, y) ; }
Straight Lines POINT apt[5] = { 100, 200, 100, 100 } ; Move. To. Ex (hdc, apt[0]. x, apt[0]. y, NULL) ; for (i = 1 ; i < 5 ; i++) Line. To (hdc, apt[i]. x, apt[i]. y) ; When you have an array of points that you want connected with lines, you can draw the lines more easily using the Polyline function. Polyline (hdc, apt, 5) ;
Straight Lines Although you can use Polyline and Polyline. To to draw just a few lines, the functions are most useful when you need to draw a complex curve. You do this by using hundreds or even thousands of very short lines. If they're short enough and there are enough of them, together they'll look like a curve. For example, suppose you need to draw a sine wave.
Drawing Sinewave hdc = Begin. Paint (hwnd, &ps) ; Move. To. Ex (hdc, 0, cy. Client / 2, NULL) ; Line. To (hdc, cx. Client, cy. Client / 2) ; for (i = 0 ; i < NUM ; i++) { apt[i]. x = i * cx. Client / NUM ; apt[i]. y = (int) (cy. Client / 2 * (1 - sin (TWOPI * i / NUM))) ; } Polyline (hdc, apt, NUM) ;
Drawing Sinewave
The Bounding Box Functions Rectangle, Ellipse, Round. Rect, Chord, and Pie functions are not strictly line-drawing functions. The functions draw lines, but they also fill an enclosed area with the current area-filling brush. This brush is solid white by default.
Rectangle (hdc, x. Left, y. Top, x. Right, y. Bottom) ;
Ellipse (hdc, x. Left, y. Top, x. Right, y. Bottom) ;
Round Rectangle Round. Rect (hdc, x. Left, y. Top, x. Right, y. Bottom, x. Corner. Ellipse, y. Corner. Ellipse) ;
Arc, Chord, Pie Arc (hdc, x. Left, y. Top, x. Right, y. Bottom, x. Start, y. Start, x. End, y. End) ;
Arc, Chord, Pie Chord (hdc, x. Left, y. Top, x. Right, y. Bottom, x. Start, y. Start, x. End, y. End) ;
Arc, Chord, Pie (hdc, x. Left, y. Top, x. Right, y. Bottom, x. Start, y. Start, x. End, y. End) ;
Bezier Splines Poly. Bezier (hdc, apt, 4) ;
Using Stock Pens When you call any of the line-drawing functions , Windows uses the "pen" currently selected in the device context to draw the line. The pen determines the line's color, its width, and its style, which can be solid, dotted, or dashed. The pen in the default device context is called BLACK_PEN is one of three "stock pens" that Windows provides. The other two are WHITE_PEN and NULL_PEN is a pen that doesn't draw. You can also create your own customized pens.
Using Stock Pens HPEN h. Pen ; You obtain the handle to one of the stock pens by a call to Get. Stock. Object. h. Pen = Get. Stock. Object (WHITE_PEN) ; Now you must "select" that pen into the device context: Select. Object (hdc, h. Pen) ;
Creating, Selecting, and Deleting Pens h. Pen = Create. Pen (i. Pen. Style, i. Width, cr. Color) ;
Drawing Filled Areas Function Rectangle Ellipse Round. Rect Figure Rectangle with square corners Ellipse Rectangle with rounded corners Chord Arc on the circumference of an ellipse with endpoints connected by a chord Pie Polygon Pie wedge defined by the circumference of an ellipse Multisided figure Polygon Multiple multisided figures
Brushing the Interior h. Brush = Create. Hatch. Brush (i. Hatch. Style, cr. Color) ;
The GDI Mapping Mode Increasing Value Mapping Mode Logical Unit x-axis y-axis MM_TEXT Pixel Right Down MM_LOMETRIC 0. 1 mm Right Up MM_HIMETRIC 0. 01 mm Right Up MM_LOENGLISH 0. 01 in. Right Up MM_HIENGLISH 0. 001 in. Right Up MM_TWIPS 1/1440 in. Right Up MM_ISOTROPIC Arbitrary (x = y) Selectable MM_ANISOTROPIC Arbitrary (x !=y) Selectable
The GDI Mapping Mode Set. Map. Mode (hdc, i. Map. Mode) ; where i. Map. Mode is one of the eight mapping mode identifiers. You can obtain the current mapping mode by calling i. Map. Mode = Get. Map. Mode (hdc) ; The default mapping mode is MM_TEXT. In this mapping mode, logical units are the same as physical units, which allows us (or, depending on your perspective, forces us) to work directly in units of pixels.
The Viewport and the Window The mapping mode defines how Windows maps logical coordinates that are specified in GDI functions to device coordinates, where the particular device coordinate system depends on the function you use to obtain the device context. The mapping mode is said to define the mapping of the "window" (logical coordinates) to the "viewport" (device coordinates).
Working with MM_TEXT For the MM_TEXT mapping mode, the default origins and extents are shown below. Window origin: (0, 0) Viewport origin: (0, 0) Window extent: (1, 1) Viewport extent: (1, 1) Can be changed Cannot be changed x. Viewport = x. Window - x. Win. Org + x. View. Org y. Viewport = y. Window - y. Win. Org + y. View. Org
Working with MM_TEXT Set. Viewport. Org. Ex (hdc, cx. Client / 2, cy. Client / 2, NULL) ;
The MM_ISOTROPIC Mapping Mode The MM_ISOTROPIC mapping mode is ideal for using arbitrarily scaled axes while preserving equal logical units on the two axes. Rectangles with equal logical widths and heights are displayed as squares, and ellipses with equal logical widths and heights are displayed as circles.
MM_ANISOTROPIC: Stretching the Image to Fit In the MM_ANISOTROPIC mapping mode, Windows makes no adjustments to the values you set. This means that MM_ANISOTROPIC does not necessarily maintain the correct aspect ratio.
Rectangles, Regions, and Clipping Working with Rectangles Fill. Rect (hdc, &rect, h. Brush) ; Frame. Rect (hdc, &rect, h. Brush) ; Invert. Rect (hdc, &rect) ; Fill. Rect fills the rectangle with the specified brush. Frame. Rect uses the brush to draw a rectangular frame, but it does not fill in the rectangle. Invert. Rect inverts all the pixels in the rectangle, turning ones to zeros and zeros to ones. This function turns a white area to black, a black area to white, and a green area to magenta
Creating and Painting Regions A region is a description of an area of the display that is a combination of rectangles, polygons, and ellipses. Used for drawing or clipping. Like pens and brushes, regions are GDI objects. can delete any regions that you create by calling Delete. Object.
Creating and Painting Regions When you create a region, Windows returns a handle to the region of type HRGN. The simplest type of region describes a rectangle. You can create a rectangular region in one of two ways: h. Rgn = Create. Rect. Rgn (x. Left, y. Top, x. Right, y. Bottom) ; or h. Rgn = Create. Rect. Rgn. Indirect (&rect) ; You can also create elliptical regions using h. Rgn = Create. Elliptic. Rgn (x. Left, y. Top, x. Right, y. Bottom) ; or h. Rgn = Create. Elliptic. Rgn. Indirect (&rect) ;
Creating and Painting Regions i. Rgn. Type = Combine. Rgn (h. Dest. Rgn, h. Src. Rgn 1, h. Src. Rgn 2, i. Combine) ; This function combines two source regions (h. Src. Rgn 1 and h. Src. Rgn 2) and causes the destination region handle (h. Dest. Rgn) to refer to that combined region. i. Combine Value New Region RGN_AND Overlapping area of the two source regions RGN_OR All of the two source regions RGN_XOR All of the two source regions, excluding the overlapping area RGN_DIFF All of h. Src. Rgn 1 not in h. Src. Rgn 2 RGN_COPY All of h. Src. Rgn 1 (ignores h. Src. Rgn 2)
Creating and Painting Regions Once you have a handle to a region, you can use it with four drawing functions: Fill. Rgn (hdc, h. Rgn, h. Brush) ; Frame. Rgn (hdc, h. Rgn, h. Brush, x. Frame, y. Frame) ; Invert. Rgn (hdc, h. Rgn) ; Paint. Rgn (hdc, h. Rgn) ;
Clipping with Rectangles and Regions Select. Clip. Rgn (hdc, h. Rgn) ; Windows also includes several functions to manipulate this clipping region, such as Exclude. Clip. Rect to exclude a rectangle from the clipping region, Intersect. Clip. Rect to create a new clipping region that is the intersection of the previous clipping region and a rectangle, and Offset. Clip. Rgn to move a clipping region to another part of the client area.
- Specific topic examples
- Tapic about internet
- Perbedaan linear programming dan integer programming
- Greedy algorithm vs dynamic programming
- What is system programing
- Linear vs integer programming
- Perbedaan linear programming dan integer programming
- Unit 10, unit 10 review tests, unit 10 general test
- Visual studio flowchart
- Introduction to visual basic
- Visual programming course outline
- Language a
- Visual basic programming language
- What is visual logic
- Introduction to visual basic programming
- Visual basic object oriented
- Visual programming
- Apakah maksud visual pada kata pemrograman visual ?
- Unit 4 programming assignment 1
- Whats vdu
- Metode pembiayaan langsung (direct financing method)
- Unit 4 lesson 7 right triangles and trigonometry unit test
- Si unit to english unit
- Unit test unit test review algebra 2
- Contoh kebutuhan mengitung unit cos unit kerja rekam medis
- Unit process and unit operation
- Unit operation and unit process
- Kerangka konseptual standar akuntansi pemerintahan
- What is the topic sentence
- What is the topic sentence? *
- Sexy writing structure
- Expository paragraph sample
- World school style debate
- Easy topic about life
- Module 3 topic 1
- Curriculum guide for driver education in virginia module 10
- Drivers ed module 7 topic 3
- Module 5 curves hills and mountains
- Module 11 topic 1 insuring a vehicle
- Module 8 topic 1 drivers ed
- Topic 15 periods authors and genres
- Which is your favourite means of transport and why
- Topic and closing sentences
- Levels of comprehension
- Topic about sports
- Better than
- Key sentence sample
- Main idea and supporting details example
- What are two parts of a topic sentence
- Write a topic sentence
- Topic sentence and supporting details examples pdf
- Topic sentence
- The celts timeline
- Topic 8 water and climate
- Topic 7 economic performance and challenges
- Topic 19 animation and expression techniques
- Landscape development and environmental changes