Este artículo describe brevemente el método de Delphi para implementar una cuenta regresiva a nivel de milisegundos con ejemplos. En términos generales, se puede obtener el número de vibraciones del contador de frecuencia de alto rendimiento del sistema en un milisegundo. Si el número de vibraciones del reloj supera los 10 milisegundos, la pantalla de edit3 se actualizará para mostrar el tiempo real transcurrido desde el inicio del conteo. al momento del conteo. El código de implementación específico es el siguiente:
unidad Unidad1; interfaz utiliza Windows, Mensajes, SysUtils, Clases, Gráficos, Controles, Formularios, Diálogos, ExtCtrls, StdCtrls, mmsystem; escriba TForm1 = clase (TForm) Edit1: TEdit2: TEdit3: TEdit Button1: TButton; : TBotón; Temporizador1: TTemporizador1: TLabel2: TLabel; Label3: TLabel; procedimiento FormCreate (Remitente: TObject); procedimiento Button1Click (Remitente: TObject); procedimiento Timer1Timer (Remitente: TObject); procedimiento Button2Click (Remitente: TObject); var Form1: TForm1; acttime1, acttime2: cardinal; smmcount, stimercount, spcount: single; htimeid:integer; iten:integer; protimecallback:tfntimecallback; procedimiento timeproc(utimerid, umessage: uint; dwuser, dw1, dw2: dword) procedimiento proendcount;implementación{$R *.DFM}//timesetevent función de devolución de llamada proendcount; comenzar acttime2:=gettickcount-acttime1; form1.button2.enabled:=false; form1.button1.enabled:=true; form1.timer1.enabled:=false; smmcount:=60; spcount:=-1; procedimiento timeproc(utimerid, umessage: uint; dwuser, dw1, dw2: dword) stdcall;comenzar form1.edit2.text:=floattostr(smmcount); smmcount:=smmcount-0.01;end;procedimiento TForm1.FormCreate(Remitente: TObject);comenzar botón1.caption :='Iniciar cuenta regresiva'; :='Finalizar cuenta atrás'; botón2.enabled :=false; botón1.enabled :=true; timer1.enabled:=false; smmcount:=60; stimercount:=60; spcount:=60;end;procedure TForm1.Button1Click(Remitente: TObject);var lgtick1,lgtick2,lgper:tlargeinteger; habilitado :=verdadero; botón1.habilitado :=falso; timer1.enabled :=true; timer1.interval :=10; protimecallback:=timeproc; htimeid:=timesetevent(10,0,protimecallback,1,1); acttime1:=gettickcount; en El número de vibraciones en un milisegundo query Performancefrequency(lgper ftemp:=lgper/1000; iten:=trunc(ftemp*10); query Performancecounter(lgtick1); lgtick2:=lgtick1; spcount:=60; mientras que spcount>0 comienza query Performancecounter(lgtick2); //Si el número de vibraciones del reloj excede los 10 milisegundos, actualice la visualización de edit3 si lgtick2 - lgtick1 > iten. comenzar lgtick1 := lgtick2; spcount := spcount - 0.01; floattostr(spcount); application.processmessages; end; end;end;procedimiento TForm1.Timer1Timer(Remitente: TObject);comenzar edit1.text := floattostr(stimercount:=stimercount-0.01;end;procedimiento TForm1.Button2Click( Remitente: TObject); comenzar proendcount; //Muestra el tiempo real transcurrido desde el inicio del conteo hasta el conteo showmessage('tiempo real transcurrido'+inttostr(acttime2)+'milisegundos');end;end.