📄 ch12.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><SCRIPT LANGUAGE="JavaScript"><!--function popUp(pPage) { var fullURL = document.location; var textURL = fullURL.toString(); var URLlen = textURL.length; var lenMinusPage = textURL.lastIndexOf("/"); lenMinusPage += 1; var fullPath = textURL.substring(0,lenMinusPage); popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394'); figDoc= popUpWin.document; zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>'; zhtm += '</head>'; zhtm += '<BODY bgcolor="#FFFFFF">'; zhtm += '<IMG SRC="' + fullPath + pPage + '">'; zhtm += '<P><B>' + pPage + '</B>'; zhtm += '</BODY></HTML>'; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 }//--> </SCRIPT><link rel="stylesheet" href="/includes/stylesheets/ebooks.css"> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"> <TITLE>Teach Yourself Borland Delphi 4 in 21 Days -- Ch 12 -- Graphics and Multimedia Programming</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><CENTER><H1><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"></H1></CENTER><CENTER><H1><BR>Teach Yourself Borland Delphi 4 in 21 Days</H1></CENTER><CENTER><P><A HREF="../ch11/ch11.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch13/ch13.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><CENTER><H1>- 12 -</H1></CENTER><CENTER><H1>Graphics and Multimedia Programming</H1></CENTER><UL> <LI><A HREF="#Heading1">Graphics the Easy Way</A> <LI><A HREF="#Heading2">Device Contexts and TCanvas</A> <LI><A HREF="#Heading3">GDI Objects</A> <UL> <LI><A HREF="#Heading4">Pens, Brushes, and Fonts</A> <LI><A HREF="#Heading5">Bitmaps and Palettes</A> <LI><A HREF="#Heading6">Clipping Regions</A> <LI><A HREF="#Heading7">Drawing Text</A> <LI><A HREF="#Heading8">Drawing Bitmaps</A> <LI><A HREF="#Heading9">Creating a Memory Bitmap</A> <LI><A HREF="#Heading10">Saving a Memory Bitmap</A> <LI><A HREF="#Heading11">Sample Memory Bitmap Program</A> </UL> <LI><A HREF="#Heading12">Multimedia Programming</A> <UL> <LI><A HREF="#Heading13">Wave Audio with the Windows API</A> <LI><A HREF="#Heading14">The TMediaPlayer Component</A> <LI><A HREF="#Heading15">MediaPlayer Properties, Methods, and Events</A> <LI><A HREF="#Heading16">Wave Audio</A> <LI><A HREF="#Heading17">Setting the Output Volume</A> <LI><A HREF="#Heading18">Recording Wave Audio</A> </UL> <LI><A HREF="#Heading19">MIDI Audio</A> <LI><A HREF="#Heading20">CD Audio</A> <UL> <LI><A HREF="#Heading21">AVI Video</A> </UL> <LI><A HREF="#Heading22">Summary</A> <LI><A HREF="#Heading23">Workshop</A> <UL> <LI><A HREF="#Heading24">Q&A</A> <LI><A HREF="#Heading25">Quiz</A> <LI><A HREF="#Heading26">Exercises</A> </UL></UL><P><HR SIZE="4"><CENTER><H1></H1></CENTER><P>Graphics and multimedia programming represent the fun part of programming. Inthis chapter, you are introduced to graphics and multimedia programming with Delphi.In the case of graphics programming, most of that introduction comes in the formof an examination of the TCanvas and TBitmap classes.</P><P>I start with a look at the easiest ways to display graphics in Delphi. After that,you learn about the Windows Graphics Device Interface and the components that makeup that interface. Along the way, you learn about the various line and shape drawingroutines and the different ways to display bitmaps. Later in the day, you learn aboutoffscreen bitmaps and how they can benefit you. The multi-media programming sectionsdeal with how to play sound files with the Windows API. You also learn how to playwave audio, MIDI, and AVI video files using the TMediaPlayer class. So let's getstarted!</P><P><H2><A NAME="Heading1"></A>Graphics the Easy Way</H2><P>Graphics programming does not have to be difficult. Sometimes all you need todo is display a picture or a simple shape on a form. VCL provides ready-made componentsfor those times. I'll take a brief look at some of these components before movingon to real graphics programming.</P><P>The Shape component (found on the Additional tab of the Component palette) canbe used to add simple shapes to your forms. Using the Shape component is easy. Justdrop one on a form and change the Brush, Pen, and Shape properties as desired. Youcan draw circles, ellipses, squares, rectangles, and rectangles with rounded edges.Change the Brush property to modify the background color of the shape. Change thePen property to change the color or thickness of the border surrounding the shape.</P><P>An Image component can be used to display a bitmap on a form. This component isgreat for many graphics operations, including a bitmap background for a form. ThePicture property of TImage is an instance of the TPicture class. You can select thepicture at design time through the Object Inspector, or you can load a picture atruntime. For example, here's how you change the image in the component at runtime:</P><P><PRE>Image1.Picture.Bitmap.LoadFromFile(`bkgnd.bmp');</PRE><P>The Stretch property determines whether the image will be enlarged or compressedto fit the size of the component. The Center property determines whether the bitmapis centered in the component. The AutoSize property can be used to force the componentto size itself according to the size of the image.</P><P>I'll also mention the PaintBox component here. If you want drawing confined toa certain area of a form, this component provides a canvas on which you can draw.The only significant property of the PaintBox component is the Canvas property. Thisproperty is an instance of the TCanvas class. It is with this class that most drawingis done in a Delphi application. Let's look at the TCanvas class now.</P><P><H2><A NAME="Heading2"></A>Device Contexts and TCanvas</H2><P>Windows uses the term <I>device context</I> to describe a canvas on which youcan draw. A device context can be used to draw on many surfaces:</P><UL> <LI>To a window's client area or frame</UL><UL> <LI>To the desktop <P> <LI>To memory <P> <LI>To a printer or other output device</UL><P>These are just a few examples. There are other, more obscure device contexts (menus,for example), but those listed above are the device contexts that you will be mostinterested in.</P><P>Dealing with device contexts at the API level can be a bit complex. First, youhave to obtain a handle to a device context from Windows. Then you have to selectvarious objects into the device context (pens, brushes, or fonts). After that, youcan draw on the device context. When you are done drawing, you have to be sure thatthe objects you select into the device context are removed before deleting the devicecontext. If you forget to remove objects selected into the device context, your applicationwill leak memory (use up memory that is never released back to the system). It'sa tedious process to say the least.</P><P>The good news is that VCL provides the TCanvas class to make dealing with devicecontexts easier. Let me give you a quick example. The following code uses the WindowsAPI to draw a circle on the screen with a blue outline and red interior:</P><P><PRE>procedure TForm1.Button1Click(Sender: TObject);var DC : HDC; Brush, OldBrush : HBrush; Pen, OldPen : HPen;begin DC := GetDC(Handle); Brush := CreateSolidBrush(RGB(255, 0, 0)); Pen := CreatePen(PS_SOLID, 1, RGB(0, 0, 255)); OldBrush := SelectObject(DC, Brush); OldPen := SelectObject(DC, Pen); Ellipse(DC, 20, 20, 120, 120); SelectObject(DC, OldBrush); SelectObject(DC, OldPen); ReleaseDC(Handle, DC);end;</PRE><P>This code isn't so terribly bad, but it's easy to forget to restore objects whenyou are done with them. When that happens, your application will leak resources.Now look at the equivalent VCL code:</P><P><PRE>Canvas.Brush.Color := clRed;Canvas.Pen.Color := clBlue;Canvas.Ellipse(20, 20, 120, 120);</PRE><P>Not only is this code shorter and more readable, it is also much more robust.The TCanvas class takes care of freeing resources as needed, so you don't have toworry about it. TCanvas is a simpler <I>and</I> more effective approach than usingthe API.</P><P>The TCanvas class has many properties and methods. I'll look at some of theseproperties and methods while working through today's material. Table 12.1 lists theprimary properties of TCanvas and Table 12.2 lists the primary methods.</P><P><H4>TABLE 12.1. PRIMARY TCanvas PROPERTIES.</H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"><I>Property</I></TD> <TD ALIGN="LEFT"><I>Description</I></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Brush</TD> <TD ALIGN="LEFT">The brush color or pattern used for filling shapes.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">ClipRect</TD> <TD ALIGN="LEFT">The current clipping rectangle for the canvas. Any drawing is confined to this rectangle. This property is read-only.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CopyMode</TD> <TD ALIGN="LEFT">Determines how drawing will be performed (normal, inverse, xor, and so on).</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Font</TD> <TD ALIGN="LEFT">The font the canvas uses for drawing text.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Handle</TD> <TD ALIGN="LEFT">The handle (HDC) of the canvas. Used when calling the Windows API directly.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Pen</TD> <TD ALIGN="LEFT">Determines the style and color of lines drawn on the canvas.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">PenPos</TD> <TD ALIGN="LEFT">The current drawing position in x and y coordinates.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Pixels</TD> <TD ALIGN="LEFT">An array of the canvas' pixels.</TD> </TR></TABLE><H4>TABLE 12.2. PRIMARY TCanvas METHODS.</H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"><I>Method</I></TD> <TD ALIGN="LEFT"><I>Description</I></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"> <H4>ARC </TD> <TD ALIGN="LEFT"> <H4>DRAWS AN ARC ON THE CANVAS USING THE CURRENT PEN. </TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">BrushCopy</TD> <TD ALIGN="LEFT">Displays a bitmap with a transparent background.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CopyRect</TD> <TD ALIGN="LEFT">Copies part of an image to the canvas.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Draw</TD> <TD ALIGN="LEFT">Copies an image from memory to the canvas.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Ellipse</TD> <TD ALIGN="LEFT">Using the current pen, draws an ellipse that is filled with the current brush on the canvas.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">FloodFill</TD> <TD ALIGN="LEFT">Fills an area of the canvas with the current brush.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">LineTo</TD> <TD ALIGN="LEFT">Draws a line from the current drawing position to the location specified in the x and y parameters.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">MoveTo</TD> <TD ALIGN="LEFT">Sets the current drawing position.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Pie</TD> <TD ALIGN="LEFT">Draws a pie shape on the canvas.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Polygon</TD> <TD ALIGN="LEFT">Draws a polygon on the canvas from an array of points and fills it with the current brush.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Polyline</TD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -