Create objects in Runtime faster

Asked

Viewed 667 times

1

I’m developing a Unit for a biometric reader. When giving CREATE, Unit creates several Runtime objects within Tabsheet. Everything is running perfectly, but when loading the objects you can see flashing/drawing on the screen. Did anyone have a similar problem? Any idea how to do this more elegantly?

The Code:

constructor TFingerprint.Create (ATab : TTabSheet; AInix, AIniy : Integer);
begin
  VTab := ATab;
  // Create image
  ComImage := TImage.Create(ATab);
  Finix := AInix;
  Finiy := AIniy;
  L1 := TStringList.Create;
  L2 := TStringList.Create;
  L1.Clear;
  L2.Clear;
  FlagSync := False;
  with ComImage do
  begin
    Parent := ATab;
    Left := (VTab.ClientWidth-128*4-24-217) div 2;
    Top := 8;
    Height := 128;
    Width := 128;
    Picture.LoadFromFile('conn_off.png');
  end;
  FP_IDS := TStringList.Create;
  // ComPort
  ComPortA := TComPort.Create(ATab);
  with ComPortA do
  begin
    BaudRate := br115200;
    Port := 'COM5';
    Buffer.InputSize := 2048;
    Buffer.OutputSize := 2048;
    OnAfterOpen := ComPortAAfterOpen;
    OnBeforeOpen := ComPortABeforeClose;
    OnError := ComPortAError;
    //OnRxChar := ComPortARxChar;
  end;
  // ComDataPacket
  ComDataPacketA := TComDataPacket.Create(ATab);
  with ComDataPacketA do
  begin
    ComPort := ComPortA;
    StartString := MD_STX;
    StopString := MD_ETX;
    OnPacket := COmDataPacket1Packet;
  end;

  // Buttons
  btnCaptureFP := TBitBtn.Create(ATab);
  with btnCaptureFP do
  begin
    Parent := ATab;
    Caption := '   Capturar   '#13#10'Digital';
    Width := 217;
    Height := 97;
    Left := VTab.ClientWidth-Width-8;
    Top := 16;
    Font.Height := -23;
    Font.Name := 'Tahoma';
    NumGlyphs := 2;
    Glyph.LoadFromFile('glyph-fingerprint.bmp');
    OnClick := OnClickCaptureFP;
    Enabled := False;
  end;
  btnDeleteFP := TBitBtn.Create(ATab);
  with btnDeleteFP do
  begin
    Parent := ATab;
    Caption := '    Apagar    '#13#10'Digital';
    Width := 217;
    Height := 97;
    Left := VTab.ClientWidth-Width-8;
    Top := 129;
    Font.Height := -23;
    Font.Name := 'Tahoma';
    NumGlyphs := 2;
    Glyph.LoadFromFile('glyph-fingerprint-delete.bmp');
    OnClick := OnClickDeleteFP;
  end;
  btnSyncFP := TBitBtn.Create(ATab);
  with btnSyncFP do
  begin
    Parent := ATab;
    Caption := ' Sincronizar '#13#10'Digital';
    Width := 217;
    Height := 97;
    Left := VTab.ClientWidth-Width-8;
    Top := 242;
    Font.Height := -23;
    Font.Name := 'Tahoma';
    NumGlyphs := 2;
    Glyph.LoadFromFile('glyph-sync.bmp');
    OnClick := OnClickSyncFP;
    Enabled := False;
  end;
  btnExitFP := TBitBtn.Create(ATab);
  with btnExitFP do
  begin
    Parent := ATab;
    Caption := '      Sair      ';
    Width := 217;
    Height := 97;
    Left := VTab.ClientWidth-Width-8;
    Top := 355;
    Font.Height := -23;
    Font.Name := 'Tahoma';
    NumGlyphs := 2;
    Glyph.LoadFromFile('exit.bmp');
  end;


 // StringGrid
  FPGrid := TStringGrid.Create(ATab);
  with FPGrid do
  begin
    Parent := ATab;
    FixedCols := 0;
    Font.Name := 'Tahoma';
    Font.Size := 12;
    ColCount := 4;
    Left := 8;
    Top := 144;
    Width := ATab.ClientWidth-24-217;
    Height := (VTab.ClientHeight-ComImage.Top-ComImage.Height-16);
    Cells[0,0] := 'Nome';
    Cells[1,0] := 'ID';
    Cells[2,0] := 'Grupo';
    Cells[3,0] := 'Leitor ID';
    Cells[4,0] := 'Level';
    Cells[5,0] := 'Status';
    ColWidths[0] := 210;
    ColWidths[1] := 100;
    ColWidths[2] := 190;
    ColWidths[3] := 90;
    ScrollBars := ssVertical;
    Options := Options + [goRowSelect];
    OnClick := OnClickFPGrid;
  end;
  // ProgressBar
  prgBar := TProgressBar.Create(ATab);
  with prgBar do
  begin
    Parent := ATab;
    Width := 217;
    Height := 16;
    Left := VTab.ClientWidth-Width-8;
    Top := 468;
    Position := 0;
  end;
  SendMessage(prgBar.Handle, PBM_SETBARCOLOR,0,clLime);
  // Label
  lblBar := TLabel.Create(ATab);
  with lblBar do
  begin
    Parent := ATab;
    Left := prgBar.Left;
    Top := prgBar.Top + prgBar.Height + 8;
    Font.Name := 'Tahoma';
    Font.Color := clWhite;
  end;

  // Load Users
  LoadUsers2;

  // Check first empty FP
  FPGrid.Row := 1;
  while not(FPGrid.Cells[3,FPGrid.Row] = '') and (FPGrid.Row < FPGrid.RowCount-1) do
    FPGrid.Row := FPGrid.Row + 1;

  ComPortA.Open;
  // GetUsers
  SendTelegram(GET_USERS);
  ComImage.Visible := True;
end;

In the middle is the loading of the Glyphs which has between 17K to 20K.

  • 4

    You could hide Tabsheet, or start with it hidden and make it visible only when all components have been created. In addition to hiding unwanted effects, it makes creation faster because some events will not be called.

  • Your tip was very good, I did this and when it becomes visible creates a delay and then starts flashing everything to appear.

3 answers

1

You can set the form Doublebuffered Property to True and still lock the canvas:

Canvas.Lock; try ... criação dos componentes ... finnaly Canvas.Unlock; end;

  • Thank you very much.... improved... I’m almost there.... still missing something.

1

It’s like Ricardo Alves phallus, matter of buffer.

procedure TForm1.FormCreate(Sender: TObject);
begin
  DoubleBuffered := true;
end;

That solves it. I used to make small games in Delphi.

0

  • The worst that continues a blinker.... the code above is in a Unit and the objects are being created inside a Tabsheet

Browser other questions tagged

You are not signed in. Login or sign up in order to post.