用 DELPHI 为 WINDOWS 做一个带声音的模拟闹钟

    技术2022-05-11  123

       嗨,大家好。我是梦寻,这是我的第一次发帖子,请大家多多鼓励、多多支持,毕竟嘛,予人方便予己方便。OK,转入正题,现在就来讲如何做一个带响声的表盘式的 CLOCK。   首先我们应选择新建一个程序,并在窗体中加入以下元件:    Timer1: TTimer;    Image1: TImage;//时钟的表盘    Edit1: TEdit;  //日期框    Label1: TLabel;    PopupMenu1: TPopupMenu;   而后在 {$R *.DFM} 下面加入这么一句 {$R CLOCKSOU.RES} <--时钟的声音文件,  (注:CLOCKSOU.RES 是先用windows的记事本新建一个文件,并加入以下几行        CLICKBEL  WAVE "CLICKBEL.WAV"        TIMEBELL  WAVE "TIMEBELL.WAV"    并存为文件 CLOCKSOU.RC 然后在 DOS解面下用BRCC32.exe 将 CLOCKSOU.RC 编译 为 DELPHI 格式的资源文件 CLOCOSOU.RES,当然,您如果嫌步骤太烦,那吗最简单的, 到我的主页 http://www.suye.8u8.com 下载完整的程序代码)   OK,在完成上面的步骤后我们就可以在窗体的代码筐中输入以下代码。  unit Unit1;

    interface

    uses  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,  ExtCtrls,unit2, StdCtrls,inifiles,MMSystem, Menus;type  TForm1 = class(TForm)    Timer1: TTimer;    Image1: TImage;    Edit1: TEdit;    Label1: TLabel;    PopupMenu1: TPopupMenu;    pop1: TMenuItem;    pop2: TMenuItem;    pop4: TMenuItem;    pop3: TMenuItem;    pop5: TMenuItem;    N1: TMenuItem;    procedure Timer1Timer(Sender: TObject);    procedure timdrow(tim:real;long,col:integer);    procedure FormCreate(Sender: TObject);    procedure Image1Click(Sender: TObject);    procedure pop1Click(Sender: TObject);    procedure pop2Click(Sender: TObject);    procedure pop4Click(Sender: TObject);    procedure pop5Click(Sender: TObject);    procedure pop3Click(Sender: TObject);    procedure tiniwr();    procedure FormClose(Sender: TObject; var Action: TCloseAction);    procedure N1Click(Sender: TObject);

      private    { Private declarations }  public    { Public declarations }  end;

    var  Form1: TForm1;  CLOCKBELL,wid,pofse:integer;  tim1,tim2,tim3,tim4:real;  dtme: TDateTime;  timini:tinifile;  dtim,textdef,tmfilename:string;implementation

    {$R *.DFM}{$R LL1.RES}  //时钟的发声文件procedure tform1.tiniwr(); begintimini:=TIniFile.Create(tmfilename);with timini dobeginWriteString('内容','文字',textdef);Writestring('参数','时间',dtim);WriteBool('参数','报时允许',pop1.checked);WriteBool('参数','秒响允许',pop2.checked);WriteBool('参数','定时允许',pop3.checked);end; timini.free; end;

    procedure tform1.timdrow(tim:real;long,col:integer); //指针的过程函数。var   xx,yy:integer;   vtt,vt:real;beginIF (TIM2=59) AND (TIM3=0) THEN TIM:=TIM-1;IF (COL=3)or (col=6) THEN  if col=3 then VT:=TIM1*5+(TIM2/720)    else begin          VT:=TIM1*5+((TIM2-1)/720);          col:=5;          end     ELSE vt:=tim;vt:=Pi-pi*vt/30-2*pi*(vt-15);  //得出指针末端的 Y 坐标vtt:=int(long*sin(vt))+45;     //得出指针末端的 X 坐标xx:=strtoint(floattostr(vtt));vtt:=int(long*cos(vt))+45;yy:=strtoint(floattostr(vtt));case col of                    //判断所要画的指针是 分针、秒针或时针0:beginimage1.Canvas.pen.Color:=clgray; image1.Canvas.pen.Width:=1;        END;1:BEGINimage1.Canvas.pen.Color:=clRed;image1.Canvas.pen.Width:=1;END;2:BEGINimage1.Canvas.pen.color:=clblue;image1.Canvas.pen.Width:=2;END;3:BEGINimage1.canvas.pen.color:=clgreen;image1.Canvas.pen.Width:=2;END;4:beginimage1.canvas.pen.color:=clblack;image1.Canvas.pen.Width:=1;END;5:beginimage1.canvas.pen.color:=clgray;image1.Canvas.pen.Width:=3;END;end;IMAGE1.Canvas.Ellipse(43,43,47,47);image1.Canvas.MoveTo(45,45);image1.Canvas.Lineto(xx,yy);end;

    procedure TForm1.Timer1Timer(Sender: TObject);var  str1,str2:string;beginstr1:=timetostr(time());STR2:=COPY(STR1,2,1);if str2=':' then pofse:=0 else pofse:=1;EDIT1.TEXT:='   '+DATETOSTR(DATE());     //日期盘上的日期数据 str2:=copy(str1,pofse+6,2);tim4:=strtofloat(str2);if tim4<>tim3 then                      //判断是否到了移动指针的时间beginif (tim3=0) or (tim3=15) or (tim3=30)  or (tim3=45) then timdrow(tim3,39,4) else timdrow(tim3,39,0); tim3:=tim4;if pop2.checked then PlaySound(PChar('CLICKBEL'),           hInstance, snd_ASync or snd_Resource); //秒针的“滴答”发声 str2:=copy(str1,1,pofse+1);

    IF(TIM3=0)AND(TIM2=59)THEN  //判断是否到了发出整点报时“钟声”的时间    BEGIN     CLOCKBELL:=STRTOINT(STR2);     IF CLOCKBELL=0 THEN CLOCKBELL:=12;     END;     IF CLOCKBELL>12 THEN CLOCKBELL:=CLOCKBELL-12; timdrow(tim1,26,6); tim1:=strtofloat(str2); timdrow(tim2,33,5); str2:=copy(str1,pofse+3,2); tim2:=strtofloat(str2); timdrow(tim2,33,2); IF (TIM2<>59) AND (TIM4<>0) THEN timdrow(tim1-1,26,3) ELSE TIMDROW(TIM1,26,3); tim2:=strtofloat(str2); str2:=copy(str1,pofse+3,2); timdrow(tim3,39,1);IF CLOCKBELL>-1 THEN        BEGIN      IF (CLOCKBELL<>0)and(pop2.checked) THEN              PlaySound(PChar('TIMEBELL'), hInstance,                        snd_ASync or snd_Resource);//时钟的整点“钟声”发声      DEC(CLOCKBELL);      END;if (pop3.checked)and(strtotime(dtim)<=time()) then//判断是否到了定时的时间   begin     form2.Button2.Caption:='取消';     clockbell:=120;     form2.Show;   //显示给自己的提示。    end;END;end;

    procedure TForm1.FormCreate(Sender: TObject); //读入程序开始的初始化设定var   str1,str2:string;begintmfilename:=ExtractFilePath(Application.EXEName)+'clock1.ini';timini:=TIniFile.Create(tmfilename);with timini dobeginTextdef:=readString('内容','文字','在这里输入您给自己的提醒');dtim:=readstring('参数','时间','23:59:59');pop1.checked:=readBool('参数','报时允许',true);pop2.checked:=readBool('参数','秒响允许',true);pop3.checked:=readBool('参数','定时允许',false);form1.Left:=readinteger('位置','X坐标',566);form1.top:=readinteger('位置','Y坐标',8);end; timini.free;dtme:=time(); pop2.checked:=true; pop1.Checked:=true; str1:=timetostr(time()); STR2:=COPY(STR1,2,1); If str2=':' then pofse:=0 else pofse:=1; image1.Canvas.BRUSH.COLOR:=CLBLACK;; str2:=copy(str1,pofse+6,2); tim3:=strtofloat(str2); str2:=copy(str1,pofse+3,2); tim2:=strtofloat(str2); str2:=copy(str1,1,pofse+1); tim1:=strtofloat(str2); CLOCKBELL:=-1;end;

    procedure TForm1.Image1Click(Sender: TObject);//表盘鼠标点击时的的处理beginPopupMenu1.Popup(FORM1.LEFT+40,FORM1.TOP+150);end;

    procedure TForm1.pop1Click(Sender: TObject);beginif pop1.checked then pop1.checked:=false else pop1.checked:=true;form1.tiniwr();end;

    procedure TForm1.pop2Click(Sender: TObject);beginif pop2.checked then pop2.checked:=false else pop2.checked:=true;form1.tiniwr();end;

    procedure TForm1.pop4Click(Sender: TObject);beginform2.show;form1.tiniwr();end;

    procedure TForm1.pop5Click(Sender: TObject);beginclose;end;

    procedure TForm1.pop3Click(Sender: TObject);beginif pop3.Checked thenbeginpop3.Checked:=false;form2.Button2.Caption:='取消';end   else     begin     pop3.Checked:=true;     form2.Button2.Caption:='关闭';     end;form1.tiniwr();end;

    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);begintimini:=TIniFile.Create('tmfilename');with timini dobeginWriteinteger('位置','X坐标',form1.Left);//保存时钟窗口的初始 X 坐标Writeinteger('位置','Y坐标',form1.top);//保存时钟窗口的初始 Y 坐标end; timini.free;

    end;

    procedure TForm1.N1Click(Sender: TObject);beginshowmessage('模拟闹钟 0.1,'+#13+'  Copy(R)ight by Suye.2001');;end;

    end.    好了,经过以上的一些操作后,我们的一个闹钟就几呼完成了,您只要做一些小变化就全部完成了,OK,就聊到这,您如果还有什么问题,欢迎到我的主页http://www.suye.8u8.com 询问,说不定您将会得到不错的收获哦。                                 梦寻,2001.02.04


    最新回复(0)