Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

flicker free animated clock

Posted on 1999-07-06
9
Medium Priority
?
603 Views
Last Modified: 2008-02-20
I try to make a clock for my program and I use the next code . How can I perfect the code and how can I flicker free the clock ?
Cifra0 = Number 0 ....;

The code:

unit Clock;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  jpeg, ExtCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Clock1: TImage;
    Clock2: TImage;
    Clock3: TImage;
    Clock4: TImage;
    Clock5: TImage;
    Clock6: TImage;
    Timer1: TTimer;
    Cifra0: TImage;
    Cifra1: TImage;
    Cifra2: TImage;
    Cifra3: TImage;
    Cifra4: TImage;
    Cifra5: TImage;
    Cifra6: TImage;
    Cifra7: TImage;
    Cifra8: TImage;
    Cifra9: TImage;
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Initial:Integer;

implementation

{$R *.DFM}

procedure TForm1.Timer1Timer(Sender: TObject);
var Hour, Min, Sec, MSec:Word;
    SSec,MMin,HHour:String;
    Present:TTime;
    PicBmp : array[0..9] of TBitmap;
    sec1,sec2,min1,min2,hour1,hour2,aux,cod:Integer;

  begin
    PicBmp[0] := Cifra0.Picture.Bitmap;
    PicBmp[1] := Cifra1.Picture.Bitmap;
    PicBmp[2] := Cifra2.Picture.Bitmap;
    PicBmp[3] := Cifra3.Picture.Bitmap;
    PicBmp[4] := Cifra4.Picture.Bitmap;
    PicBmp[5] := Cifra5.Picture.Bitmap;
    PicBmp[6] := Cifra6.Picture.Bitmap;
    PicBmp[7] := Cifra7.Picture.Bitmap;
    PicBmp[8] := Cifra8.Picture.Bitmap;
    PicBmp[9] := Cifra9.Picture.Bitmap;

    Present := Now;
    DecodeTime(Present, Hour, Min, Sec, MSec);

    if Sec < 10 then
      SSec := '0' + IntToStr(Sec)
    else
      SSec := IntToStr(Sec);

    if Min < 10 then
      MMin := '0' + IntToStr(Min)
    else
      MMin := IntToStr(Min);

    if Hour < 10 then
      HHour := '0' + IntToStr(Hour)
    else
      HHour := IntToStr(Hour);

    Val(SSec[2], sec2, cod);
    Clock6.Picture.Bitmap := PicBmp[sec2];
    Val(SSec[1], aux, cod);

    if ((sec1 <> aux) or (Initial = 0)) then
      begin
      Val(SSec[1], sec1, cod);
      Clock5.Picture.Bitmap := PicBmp[sec1];
      end;

    Val(SSec[1], aux, cod);
    if (((aux = 0) and (sec2 = 0)) or (Initial = 0)) then
      begin
      Val(MMin[2], min2, cod);
      Clock4.Picture.Bitmap := PicBmp[min2];
      end;

    Val(MMin[2], aux, cod);
    if (((aux = 0) and (sec1 = 0) and (sec2 = 0)) or (Initial = 0)) then
      begin
      Val(MMin[1], min1, cod);
      Clock3.Picture.Bitmap := PicBmp[min1];
      end;

    Val(MMin[1], aux, cod);
    if (((aux = 0) and (sec1 = 0) and (sec2 = 0) and (min1 = 0)) or (Initial = 0))then
      begin
      Val(HHour[2], hour2, cod);
      Clock2.Picture.Bitmap := PicBmp[hour2];
      end;

    Val(HHour[2], aux, cod);
    if (((aux = 0) and (sec1 = 0) and (sec2 = 0) and (min1 = 0) and (min2 = 0))
       or (Initial = 0)) then
       begin
       Val(HHour[1], hour1, cod);
       Clock1.Picture.Bitmap := PicBmp[hour1];
       Initial := 1;
       end;
  end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Initial:=0;
end;

end.
0
Comment
Question by:ginsonic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1386886
hi ginsonic,

try this

at begin of your timer-event
LockwindowUpdate(Form1.handle);

at the end
LockwindowUpdate(0);

meikl
0
 
LVL 10

Expert Comment

by:Lischke
ID: 1386887
Don't use a dozen TImages, but only one. Additionally, I'd suggest using an in-memory bitmap (just a TBitmap). Draw each of the number bitmaps you want to show into the off-screen bitmap and after you finished your drawings blit it to the (only) Image.

Ciao, Mike
0
 

Expert Comment

by:haehnen
ID: 1386888
Hi,
i think it is not so good that your using so many TImage-Components.
Why don't you use Canvas?
bye
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Author Comment

by:ginsonic
ID: 1386889
Hi Lishke ,
Can you tell me more about this ? Maybe some code?
Thanks in advance ,
Ginsonic
0
 
LVL 10

Expert Comment

by:Lischke
ID: 1386890
Oh, this is quite easy.

I assume here that you have your bitmaps stored somewhere you can access them easily. The resource file would be ideal for this. If you need more information about adding bitmaps to a res file then tell me.

When you create your forms then create also 10 small bitmaps each for one number from 0 through 9. These are used to quickly compose your final image. The approach storing the references in an array to use indices to access them is fine.

Now calculate the time as usual and draw (depending on position and number) one of these bitmaps to the Image canvas. Repeat this until you are done.

type
  TForm1 = class(TForm)
    Image1: TImage;
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    NumberBitmaps: array[0..9] of TBitmap;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Initial:Integer;

implementation

{$R *.DFM}

procedure TForm1.Timer1Timer(Sender: TObject);
var Hour, Min, Sec, MSec:Word;
    XOffset, YOffset: Integer;

  begin
    XOffset := 0;
    YOffset := 0;
    DecodeTime(Now, Hour, Min, Sec, MSec);
    Image1.Canvas.Draw(XOffset, YOffset, NumberBitmaps[Hour div 10])
    Inc(XOffset, NumberBitmaps[Hour div 10].Width);
    Image1.Canvas.Draw(XOffset, YOffset, NumberBitmaps[Hour mod 10])
    Inc(XOffset, NumberBitmaps[Hour mod 10].Width);
    Image1.Canvas.Draw(XOffset, YOffset, NumberBitmaps[Min div 10])
    Inc(XOffset, NumberBitmaps[Min div 10].Width);
    Image1.Canvas.Draw(XOffset, YOffset, NumberBitmaps[Min mod 10])
    Inc(XOffset, NumberBitmaps[Min mod 10].Width);
    Image1.Canvas.Draw(XOffset, YOffset, NumberBitmaps[Sec div 10])
    Inc(XOffset, NumberBitmaps[Sec div 10].Width);
    Image1.Canvas.Draw(XOffset, YOffset, NumberBitmaps[Sec mod 10])
    Inc(XOffset, NumberBitmaps[Sec mod 10].Width);
    Image1.Refresh;
  end;

procedure TForm1.FormCreate(Sender: TObject);
var I: Integer;
begin
  for I := 0 to 9 do
  begin
    NumberBitmaps[I] := TBitmap.Create;
    // here I assume your bitmaps are stored with names
    // NUMBER0, NUMBER1 etc.
    NumberBitmaps[I].Handle := LoadBitmap(HInstance, 'NUMBER' + IntToStr(I));
  end;
end;

end.
0
 
LVL 9

Author Comment

by:ginsonic
ID: 1386891
Hi Lishke,
Can you tell me how can I put the bitmaps in res file ?
I try with image editor but I lose quality of bmp .
And for
NumberBitmaps[I].Handle := LoadBitmap(HInstance, 'NUMBER' + IntToStr(I));
I receive an error message : incompatible String with PChar .

Nick aka Ginsonic
0
 
LVL 10

Expert Comment

by:Lischke
ID: 1386892
Well, the image editor isn't actually very good, but you can use it if your images have only 256 colors. If you are working with D4 then you can found a Delphi1 version on the CD which has the Resource Workshop included (browse through the binary image folder). This thing is much better and can produce 32bit resources. If you can't use any of this then you have a problem (Borland should have thought about that...). If you can get hold of a VC++ copy then use this as resource editor (also quite good). If that all doesn't help then send me your bitmaps and I'll put them for you in a res file (public@lischke-online.de).

Sorry for the PChar incompatibility. I forgot that... Solution:procedure TForm1.FormCreate(Sender: TObject);
var I: Integer;
    S: String;
begin
  for I := 0 to 9 do
  begin
    NumberBitmaps[I] := TBitmap.Create;
    // here I assume your bitmaps are stored with names
    // NUMBER0, NUMBER1 etc.
    S :='NUMBER' + IntToStr(I);
    NumberBitmaps[I].Handle := LoadBitmap(HInstance, PChar(S));
  end;
end;


Ciao, Mike
0
 
LVL 9

Author Comment

by:ginsonic
ID: 1386893
Lischke,
Put some words for answer.
Thanks for help.
Nick aka Ginsonic
0
 
LVL 10

Accepted Solution

by:
Lischke earned 200 total points
ID: 1386894
Hi Nick,

glad that I could help you.

Ciao, Mike
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question