Draw title in Stringgrid rotated (top to buttom) lazarus / 31-LCL ---------------------------------------------------------------------- MoellerCLaus · 2022-03-11 14:31 I been looking for an example on how to rotate the title text in a columnheader in a stringgridcomponent. In my application I need a lot of checkboxes in the grid and to minimize the columns width this would come in handy. I am porting to Lazarus from Delphi. See image. Thanks for any hint ---------------------------------------------------------------------- winni · 2022-03-11 16:07 Hi! In the OI for the StringGrid1 set DefaultDrawing to false. Create this procedure for the onDrawCell event: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;  aRect: TRect; aState: TGridDrawState);  var x,y : integer;    s: String;begin  if  aRow = 0 then  begin    x := (aRect.Right-aRect.Left -12) div 2 + aRect.left;    y := aRect.Top+ 3;    StringGrid1.Canvas.Brush.color := StringGrid1.FixedColor;    StringGrid1.canvas.FillRect(aRect);    StringGrid1.Canvas.Font.Orientation := 2700;    StringGrid1.Canvas.Textout(x,y,'Column '+IntToStr(aCol));  end else  begin   x := aRect.Left +2;   y := aRect.Top +2;   if odd (aRow) then StringGrid1.Canvas.Brush.color := StringGrid1.Color else        StringGrid1.Canvas.Brush.color := StringGrid1.AlternateColor;   if gdSelected in aState then StringGrid1.Canvas.Brush.color := clRed;   StringGrid1.canvas.FillRect(aRect);   s := stringGrid1.cells[aCol,aRow];   StringGrid1.Canvas.Font.Orientation := 0;   StringGrid1.Canvas.TextOut(x,y,s);  end;end;  Winni ---------------------------------------------------------------------- MoellerCLaus · 2022-03-11 19:13 Thanks Winni That did it for me. ---------------------------------------------------------------------- wp · 2022-03-13 15:41 I modified cell painting in the Lazarus grids so that the Font.Orientation is handled correctly. Now you only must set the Orientation of the Column.Title.Font to -900 to get vertical text running downward; use Column.Title.Alignment = taCenter to center the text horizontally within the cell, and Column.Title.Layout = tlTop to move it to the top of the cell. In case of a checkbox column, there is no need any more to paint the checkboxes manually now. All this is in trunk/main. Since it is a new feature it will not be ported to the fixes branch and thus not be included in Laz v2.2.2. If you want to have the feature in your current Lazarus version you can do the following changes manually (of course, make a backup of the modified files because I cannot guarantee that the new code is compatible with your Laz version): * Open unit graphmath.pas (in the components/lazutils folder of the Lazarus installation) and add the following code. Add the declaration of the RotateRect function to the interface part of the unit: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {---------- ---------------------------------------------------------------------  Method:  RotatePoint  Params:  APoint, AAngle  Returns: TPoint after rotation    Rotates a point around the origin (0,0) by the angle AAngle. The angle is  in radians and positive for counter-clockwise rotation.  Note that y points downwards.------------------------------ -------------------------------------------------}function RotatePoint(const APoint: TPoint; AAngle: Double): TPoint;var  sa, ca: Double;begin  sa := sin(AAngle);  ca := cos(AAngle);  Result.X := Round( ca * APoint.X + sa * APoint.Y);  Result.Y := Round(-sa * APoint.X + ca * APoint.Y);end; procedure GetMinMax(x: Integer; var min, max: Integer);begin  if x < min then min := x;  if x > max then max := x;end; {------------------------------------------------------- ------------------------  Method:   RotateRect  Params:   AWidth, AHeight, AAngle  Returns:  smallest TRect containing the rotated rectangle.    Rotates the rectangle (0, 0, AWidth, AHeight) around its top-left corner (0,0)  by the angle AAngle (in radians).  Note that y points downwards.----------------------------------------------------- --------------------------}function RotateRect(AWidth, AHeight: Integer; AAngle: Double): TRect;var  P1, P2, P3: TPoint;begin  if AAngle = 0 then    Result := Rect(0, 0, AWidth, AHeight)  else  begin    P1 := RotatePoint(Point(AWidth, 0), AAngle);    P2 := RotatePoint(Point(0, AHeight), AAngle);    P3 := P1 + P2;     Result := Rect(0, 0, 0, 0);    GetMinMax(P1.X, Result.Left, Result.Right);  GetMinMax(P2.X, Result.Left, Result.Right);    GetMinMax(P3.X, Result.Left, Result.Right);    GetMinMax(P1.Y, Result.Top, Result.Bottom);    GetMinMax(P2.Y, Result.Top, Result.Bottom);    GetMinMax(P3.Y, Result.Top, Result.Bottom);  end;end; * Open unit grids.pas (in the lcl folder of the Lazarus installation) and add GraphMath to its implementation "uses" clause. * In the implementation part of the unit, replace the method TCustomGrid.DrawCellText by the following code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TCustomGrid.DrawCellText(aCol, aRow: Integer; aRect: TRect;  aState: TGridDrawState; aText: String);var  Rtxt, Rrot, R: TRect;  angle: Double;  ts: TTextStyle;begin  ts := Canvas.TextStyle;   if Canvas.Font.Orientation = 0 then  begin    dec(ARect.Right, varCellPadding);    case Canvas.TextStyle.Alignment of      Classes.taLeftJustify: Inc(ARect.Left, varCellPadding);      Classes.taRightJustify: Dec(ARect.Right, 1);    end;    case Canvas.TextStyle.Layout of      tlTop: Inc(ARect.Top, varCellPadding);      tlBottom: Dec(ARect.Bottom, varCellPadding);    end;  end else  begin    angle := Canvas.Font.Orientation * pi / 1800;    Rtxt.TopLeft := Point(0, 0);    Rtxt.BottomRight := TPoint(Canvas.TextExtent(aText));    Rrot := RotateRect(Rtxt.Width, Rtxt.Height, angle);    R := Rrot;    case Canvas.TextStyle.Alignment of      taLeftJustify: OffsetRect(R, -Rrot.Left + varCellPadding, 0);      taCenter: OffsetRect(R, (ARect.Width - Rrot.Width) div 2 - Rrot.Left, 0);      taRightJustify: OffsetRect(R, ARect.Width - Rrot.Right - varCellPadding, 0);    end;    case Canvas.TextStyle.Layout of      tlTop: OffsetRect(R, 0, -Rrot.Top + varCellPadding);      tlCenter: OffsetRect(R, 0, (ARect.Height - Rrot.Height) div 2 - Rrot.Top);      tlBottom: OffsetRect(R, 0, ARect.Height - Rrot.Bottom - varCellPadding);    end;    OffsetRect(R, -Rrot.Left, -Rrot.Top);    OffsetRect(R, ARect.Left, ARect.Top);    ARect := R;    ts.Clipping := false;    ts.Layout := tlTop;    ts.Alignment := taLeftJustify;  end;     if ARect.RightARect.Right then    ARect.Left:=ARect.Right;  if ARect.BottomARect.Bottom then    ARect.Top:=ARect.Bottom;   if (ARect.Left<>ARect.Right) and (ARect.Top<>ARect.Bottom) then    Canvas.TextRect(aRect,ARect.Left,ARect.Top, aText, ts);end; ---------------------------------------------------------------------- wp · 2022-03-13 18:18 Don't ask me... I only wrote the part with Font.Orientation <> 0. ---------------------------------------------------------------------- MoellerCLaus · 2022-03-14 13:56 Hi Winni and others As you could see from the above I need to draw checkboxes in some of the cells. I tried   to use Grid.DefaultDrawing:=True; in procedure TMarkListForm.MarksGridDrawCell(Sender: TObject; aCol, aRow: Integer;   aRect: TRect; aState: TGridDrawState); It seems to work for me. An alternative is perhaps to use    procedure DrawGridCheckboxBitmaps(const aCol,aRow: Integer; const aRect: TRect;                                         const aState: TCheckboxState); virtual;  but this as standard protected. Any hints - thanks ---------------------------------------------------------------------- wp · 2022-03-14 16:12 I guess you did not read my message about the fix... (Reply #3 on March 13, 2022) [EDIT] The attached demo project is created after this modification. All the changes to achieve vertical headers can be made at designtime. ---------------------------------------------------------------------- MoellerCLaus · 2022-03-14 20:09 Whau WP Thanks No I did not try it - I will. I wote for that your improvement to stringgrid will be part of the next release