⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch12.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 5 页
字号:
shows the MemBmp program running.</P><P><A HREF="javascript:popUp('28671211.gif')"><B>FIGURE 12.11.</B></A><B> </B><I>TheMemBmp program running.</I></P><P><H4>LISTING 12.2. MemBmpU.pas.</H4><PRE>unit MemBmpU;interface</PRE><PRE></PRE><PRE>uses  Windows, Messages, SysUtils, Classes, Graphics,   Controls, Forms, Dialogs, StdCtrls;const  DisplayText = `TurboPower Software Co.';type  TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    Button3: TButton;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure Button3Click(Sender: TObject);  private    { Private declarations }     Done : Boolean;  public    { Public declarations }   end;var  Form1: TForm1;implementation{$R *.DFM} procedure TForm1.Button1Click(Sender: TObject);var  I : Integer;begin  Canvas.Font.Name := `Arial Bold';  Canvas.Font.Size := 16;  Canvas.Brush.Color := clSilver;  Done := false;  while not Done do begin    for I := -Canvas.TextWidth(DisplayText)       to Pred(Width) do begin      Sleep(1);      Application.ProcessMessages;      if Done then        Break;      Canvas.Font.Color := clGray;      Canvas.Brush.Style := bsClear;</PRE><PRE>      Canvas.TextOut(i + 2, 12, DisplayText);</PRE><PRE>      Canvas.Font.Color := clBlack;      Canvas.Brush.Style := bsClear;      Canvas.TextOut(i, 10, DisplayText);      Canvas.Font.Color := clSilver;      Canvas.TextOut(i + 2, 12, DisplayText);      Canvas.TextOut(i, 10, DisplayText);    end;  end;end;procedure TForm1.Button2Click(Sender: TObject);var  Bitmap : TBitmap;  I : Integer;begin  Bitmap := TBitmap.Create;  Bitmap.Width := Width;  Bitmap.Height := 40;  Bitmap.Canvas.Font.Name := `Arial Bold';  Bitmap.Canvas.Font.Size := 16;  Bitmap.Canvas.Brush.Color := clSilver;  Bitmap.Canvas.FillRect(Rect(0, 0, Width, 40));  Done := False;  while not Done do begin    for I := -Bitmap.Canvas.TextWidth(DisplayText)       to Pred(Width) do begin      Application.ProcessMessages;      if (Done) then        Break;      Sleep(1);      Bitmap.Canvas.Font.Color := clGray;      Bitmap.Canvas.Brush.Style := bsClear;      Bitmap.Canvas.TextOut(2, 12, DisplayText);      Bitmap.Canvas.Font.Color := clBlack;      Bitmap.Canvas.Brush.Style := bsClear;      Bitmap.Canvas.TextOut(0, 10, DisplayText);      Canvas.Draw(I, 0, Bitmap);    end;  end;  Bitmap.Free;end;procedure TForm1.Button3Click(Sender: TObject);begin  Done := True;end;end.</PRE><P>The MemBmp program is included with the book's code, along with other exampleprograms (called BrushTst, Capture, Gradient, and Rotate) that illustrate the conceptsdiscussed today.</P><P><H2><A NAME="Heading12"></A>Multimedia Programming</H2><P><I>Multimedia programming</I> is a deceivingly innocent phrase. The reason forthis is that multimedia programming encompasses a wide range of possibilities. Multimediatypically includes wave audio, MIDI audio, AVI video clips, and animation. I don'twant to confuse multimedia programming with game programming.</P><PRE></PRE><P>Game programming certainly involves multimedia, but it is much more involved thanadding sound or video to an application. I'm going to lightly cover multimedia inthis section and show you what you can do given the tools that Delphi provides rightout of the box. I'm not going to talk about graphics or multimedia APIs such as OpenGLor Direct Draw. For more information on graphics programming using those libraries,get a good book that specializes in graphics programming. <I>Delphi 4 Unleashed</I>(ISBN: 0-672-312-859) would be a good choice.</P><P><H3><A NAME="Heading13"></A>Wave Audio with the Windows API</H3><P>Normally I wouldn't talk a lot about Windows API functions because most of thetime VCL offers you a better way of performing a task than does the API. In the caseof playing a wave file, however, there is nothing more simple than the Win32 APIPlaySound function. Playing a wave file by using this function is very easy. Thefirst thing you need to do is add MmSystem to your unit's uses list. Then call PlaySoundwith the proper parameters:</P><P><PRE>PlaySound(`test.wav', 0, SND_FILENAME);</PRE><P>That's pretty simple, isn't it? As you can see, the first parameter to PlaySoundis used to specify a sound file to play. The last parameter is used to specify flagsthat determine how the sound should be played. When playing a wave audio file ondisk, you specify SND_FILENAME as the last parameter (you can specify other flagsas well, but I'll get to that in just a bit).</P><P>The PlaySound function can also play system sounds as well as files on disk. Toplay a system sound, specify the sound alias in the first parameter passed to PlaySound,and SND_ALIAS in the flags parameter--for example,</P><P><PRE>PlaySound(`SystemAsterisk', 0, SND_ALIAS);</PRE><P>This code plays the system sound associated with the Asterisk event (assumingone has been set up on your system). To see a list of system sound events, look atthe Sound applet in the Control Panel. To determine the sound alias for the event,you can look in the Windows Registration Database (the Registry). The aliases arelisted under the HKEY_CURRENT_USER key.</P><P>If the requested sound cannot be found, Windows will play the default sound (the<I>ding</I> if you have the default sound scheme). You can prevent Windows from playingthe default sound by specifying the SND_NODEFAULT flag. For example, if you wantto play a system sound but don't want the default sound to play if the system soundcannot be found, you could use this code:</P><P><PRE>PlaySound(`MailBeep', 0, SND_ALIAS or SND_NODEFAULT);</PRE><PRE>Notice that the SND_ALIAS and SND_NODEFAULT flags are or'd together.</PRE><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> The Win32 API MessageBeep function can also be used to play system	sounds by their index number. See MessageBeep in the Win32 online help for more information.	<HR></BLOCKQUOTE><P>There are two other flags that are important when dealing with the PlaySound function:</P><UL>	<LI>The SND_ASYNC flag stipulates that the sound should be played asynchronously.	When this flag is used, the sound starts playing and immediately returns control	to the calling application. This means that the sound can play while your application	goes on its way.	<P>	<LI>The SND_SYNC flag stipulates that control should not return to the calling application	until after the sound is finished playing. SND_SYNC is the default for PlaySound,	so you shouldn't have to specifically set this flag.</UL><P>There are many other flags that can be used to control how sounds are played withPlaySound. For complete information, see the PlaySound topic in the Win32 onlinehelp.</P><P><H3><A NAME="Heading14"></A>The TMediaPlayer Component</H3><P>VCL provides the MediaPlayer component for simple multimedia operations. Thiscomponent, located on the System tab of the Component palette, can play wave files,MIDI files, AVI videos, and more. TMediaPlayer is easy to use, too. For just playingwave files I usually use the PlaySound function as described in the previous section.For anything more complex, though, you can use the MediaPlayer component.</P><P>The most obvious way of using TMediaPlayer is to simply drop one on a form. Whenyou do, the media player controller is displayed. This controller has buttons forplay, pause, stop, next, previous, step, back, record, and eject. Figure 12.12 showsa form with a MediaPlayer.</P><P><A HREF="javascript:popUp('28671212.gif')"><B>FIGURE 12.12.</B></A><B> </B><I>Aform with a </I>MediaPlayer<I> component.</I></P><P><BR>Using the media player in its most basic form is extremely simple. All you have todo is set the FileName property to the name of a valid multimedia file and clickthe Play button on the MediaPlayer. You can select any .WAV, .MID, or .AVI file.The MediaPlayer knows what to do with the file automatically and no additional interventionis required. Most of the time, you want to do something a little more interestingwith the MediaPlayer, and for those times you'll have to dig a little deeper.</P><P>Although the visual control bar of the MediaPlayer is nice in some situations,you will probably use the MediaPlayer without the control bar at times. You can manipulatethe MediaPlayer through code to play, start, stop, or rewind the media. If you don'twant to see the MediaPlayer control bar at runtime, you'll have to set the Visibleproperty to False.</P><P><H3><A NAME="Heading15"></A>MediaPlayer Properties, Methods, and Events</H3><P>TMediaPlayer has a lot of properties. Most of them are pretty easy to understand,but some are a bit more complicated. Table 12.5 lists the primary properties of TMediaPlayer.</P><P><H4>TABLE 12.5. PRIMARY TMediaPlayer 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">AutoOpen</TD>		<TD ALIGN="LEFT">Specifies whether the device should be opened as soon as the media player is created.			Default: False</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">AutoRewind</TD>		<TD ALIGN="LEFT">When True, the media position pointer will be reset to the beginning of the media			after the media has played. Default: True</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">DeviceType</TD>		<TD ALIGN="LEFT">The type of the multimedia device. Set to dtAutoSelect to have the device type automatically			selected based on the filename extension. Default: dtAutoSelect</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Display</TD>		<TD ALIGN="LEFT">Used to set the display window to a specific component (for video devices).</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">DisplayRect</TD>		<TD ALIGN="LEFT">Used to set the size and position of the playback window for video devices. The video			is resized to fit this rectangle.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">EnabledButtons</TD>		<TD ALIGN="LEFT">The buttons on the MediaPlayer that should be enabled. Default: All Buttons</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">EndPos</TD>		<TD ALIGN="LEFT">The media ending position. The media is played from StartPos to EndPos. If EndPos			is not specified, the media plays to the end. The value you assign to EndPos depends			on the type of media being played.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Error</TD>		<TD ALIGN="LEFT">The error code of the last operation.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">ErrorMessage</TD>		<TD ALIGN="LEFT">A textual description of the last error.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Frames</TD>		<TD ALIGN="LEFT">The number of frames to move when the Back or Next methods are called, or when the			Back or Next buttons on the MediaPlayer control bar are clicked.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Length</TD>		<TD ALIGN="LEFT">The length of the media. The value of Length depends on the type of media being played			and the current value of TimeFormat.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Mode</TD>		<TD ALIGN="LEFT">The state of the device. Can be mpNotReady, mpStopped, mpPlaying, mpRecording, mpSeeking,			mpPaused, or mpOpen.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Notify</TD>		<TD ALIGN="LEFT">When True, the OnNotify event is generated when the MediaPlayer finishes an operation.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">NotifyValue</TD>		<TD ALIGN="LEFT">The results of the last notification operation. Can be nvSuccessful, nvSuperseded,			nvAborted, or nvFailure.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Position</TD>		<TD ALIGN="LEFT">The current position in the media.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">StartPos</TD>		<TD ALIGN="LEFT">The media starting position. The media is played from StartPos to EndPos. If StartPos			is not specified, the media is played from the beginning. The value you assign to			StartPos depends on the type of media being played.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">TimeFormat</TD>		<TD ALIGN="LEFT">The time format to use for this device. The time format can be specified in milliseconds,			frames, bytes, samples, tracks/minutes/seconds, hours/minutes/seconds, and more.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Tracks</TD>		<TD ALIGN="LEFT">The number of tracks the media contains (for CD audio devices).</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">VisibleButtons</TD>		<TD ALIGN="LEFT">Specifies the buttons that should be displayed on the Medi

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -