Recent

Author Topic: mcGrid: Alignement of cells  (Read 787 times)

Jvan

  • Full Member
  • ***
  • Posts: 181
mcGrid: Alignement of cells
« on: August 04, 2020, 02:26:10 am »
I want to center the content of the cells, so I used this code:

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TfrmMain.DrawCellTextHandler(Sender: TObject; ACol, ARow: Integer;
  3.   ARect: TRect; AState: TGridDrawState; AText: String; var Handled: Boolean);
  4. var
  5.   ts: TTextStyle;
  6.   x, y: Integer;
  7.   bmp: TBitmap;
  8. begin
  9.   Handled := True;
  10.   if (ACol in [1..2]) and (ARow = 0) then
  11.   begin
  12.     // Centered text
  13.     ts := grdAstCont.Canvas.TextStyle;
  14.     ts.Alignment := taCenter;
  15.     ts.Layout := tlCenter;
  16.     x := (ARect.Left + ARect.Right) div 2;
  17.     y := (ARect.Top + ARect.Bottom) div 2;
  18.     grdAstCont.Canvas.TextRect(ARect, x, y, AText, ts);
  19.   end else if (ACol in [3..4]) and (ARow = 0) then begin
  20.     // Centered text
  21.     ts := grdAstCont.Canvas.TextStyle;
  22.     ts.Alignment := taCenter;
  23.     ts.Layout := tlCenter;
  24.     x := (ARect.Left + ARect.Right) div 2;
  25.     y := (ARect.Top + ARect.Bottom) div 2;
  26.     grdAstCont.Canvas.TextRect(ARect, x, y, AText, ts);
  27.   end;
  28.   Handled := False;
  29. end;
  30.  

But the result is the image.

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: mcGrid: Alignement of cells
« Reply #1 on: August 04, 2020, 11:07:35 am »
I think the last "Handled" is wrong. This parameter tells the grid whether it should continue painting the specified cell. Since you provided an OnDrawCellText handler for the merged cells "Handled" should be true for these, but for the other cells "Handled" should be false. Therefore, there must be an "else" before the "Handled := false.

Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.DrawCellTextHandler(Sender: TObject; ACol, ARow: Integer;
  2.   ARect: TRect; AState: TGridDrawState; AText: String; var Handled: Boolean);
  3. var
  4.   ts: TTextStyle;
  5.   x, y: Integer;
  6.   bmp: TBitmap;
  7. begin
  8.   Handled := True;
  9.   if (ACol in [1..2]) and (ARow = 0) then
  10.   begin
  11.     // Centered text
  12.     // ...
  13.   end else if (ACol in [3..4]) and (ARow = 0) then begin
  14.     // Centered text
  15.     // ...
  16.   end
  17.   else   // <----------- ADDED
  18.     Handled := False;
  19. end;

See also attached demo which is simpler than the example in the Lazarus folder.

Jvan

  • Full Member
  • ***
  • Posts: 181
Re: mcGrid: Alignement of cells
« Reply #2 on: August 04, 2020, 04:10:57 pm »
It works. Thanks.

 

TinyPortal © 2005-2018