送你三个单元
1 组件单元,这是一个按下按钮发出声音的组件
其中的声音选择的属性符合你的要求
这个单元没什么好说,注意它有两个属性
一个是soundup,一个是sounddown
双击它们将会弹出一个声音文件选择窗体.即你要求的窗体
unit MdSounB;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
StdCtrls;
type
TMdSoundButton = class(TButton)
private
FSoundUp, FSoundDown: string;
protected
procedure MouseDown(Button: TMouseButton;
Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton;
Shift: TShiftState; X, Y: Integer); override;
published
property SoundUp: string
read FSoundUp write FSoundUp;
property SoundDown: string
read FSoundDown write FSoundDown;
end;
procedure Register;
implementation
uses MMSystem;
procedure TMdSoundButton.MouseDown(Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
inherited MouseDown (Button, Shift, X, Y);
PlaySound (PChar (FSoundDown), 0, snd_Async);
end;
procedure TMdSoundButton.MouseUp(Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
inherited MouseUp (Button, Shift, X, Y);
PlaySound (PChar (FSoundUp), 0, snd_Async);
end;
procedure Register;
begin
RegisterComponents('Md', [TMdSoundButton]);
end;
end.
2 弹出窗体的单元
这个也没什么好说只是一个普通的窗体
用OpenDialog来进行文件选择
unit PeFSound;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Buttons, StdCtrls;
type
TSoundForm = class(TForm)
Label1: TLabel;
btnOK: TBitBtn;
btnCancel: TBitBtn;
btnLoad: TSpeedButton;
OpenDialog1: TOpenDialog;
btnPlay: TBitBtn;
ComboBox1: TComboBox;
procedure btnLoadClick(Sender: TObject);
procedure btnPlayClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
SoundForm: TSoundForm;
implementation
{$R *.DFM}
uses
MMSystem;
procedure TSoundForm.btnLoadClick(Sender: TObject);
begin
if OpenDialog1.Execute then
ComboBox1.Text := OpenDialog1.FileName;
end;
procedure TSoundForm.btnPlayClick(Sender: TObject);
begin
PlaySound (PChar (ComboBox1.Text), 0, snd_Async);
end;
end.
3 重要的单元
为sound属性注册编辑窗体
unit PeSound;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, DesignIntf, DesignEditors;
type
TSoundProperty = class (TStringProperty) <<定义一个继承自Tstringpropery的
属性编辑器
public
function GetAttributes: TPropertyAttributes; override;
procedure GetValues(Proc: TGetStrProc); override;
procedure Edit; override;
end;
procedure Register;
implementation
uses
MdSounB,PeFSound;
function TSoundProperty.GetAttributes:
TPropertyAttributes;
begin
Result := [paDialog, paMultiSelect, paValueList, paSortList];
end;
procedure TSoundProperty.GetValues(Proc: TGetStrProc);<<实现这个方法将返回一个
代表索引值的列表
begin
Proc ('Maximize');
Proc ('Minimize');
Proc ('MenuCommand');
Proc ('MenuPopup');
Proc ('RestoreDown');
Proc ('RestoreUp');
Proc ('SystemAsterisk');
Proc ('SystemDefault');
Proc ('SystemExclamation');
Proc ('SystemExit');
Proc ('SystemHand');
Proc ('SystemQuestion');
Proc ('SystemStart');
Proc ('AppGPFault');
end;
procedure TSoundProperty.Edit;<<实现这个方法就是你要的特性了
<<双击属性编辑器将出弹出前面的那个窗体
begin
SoundForm := TSoundForm.Create (Application);
try
SoundForm.ComboBox1.Text := GetValue;
if SoundForm.ShowModal = mrOK then
SetValue (SoundForm.ComboBox1.Text);
finally
SoundForm.Free;
end;
end;
procedure Register;
begin
RegisterPropertyEditor (TypeInfo(string),
TMdSoundButton, 'SoundUp', TSoundProperty);<<调用RegisterPropertyEditor 来注
为组件的属性注册属性编辑器
RegisterPropertyEditor (TypeInfo(string),
TMdSoundButton, 'SoundDown', TSoundProperty);
end;
end.