Center text in a TStringGrid cell

Is it possible (and how) to center a text in TStringGrid cell ?
LVL 1
LeTayAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bokistCommented:
Try this solution :

procedure TForm1.MyStringGridDrawCell(Sender: TObject; ACol, ARow: Integer;
          Rect: TRect; State: TGridDrawState);
begin
  with (Sender as TStringGrid), Canvas do
     begin
     TextRect(Rect, Rect.Left + 2, Rect.Top + 2, Cells[Acol, Arow]);
     FrameRect(Rect);
     Display(MyStringGrid, MyStringGrid.Cells[ACol, ARow], Rect, taCenter); //taLeftJustify, taRightJustify
  end;
LeTayAuthor Commented:
The Display (..) does not compile (undeclared identifier)
bokistCommented:
Which version of delphi are you using?
Example works with D7.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Geert GOracle dbaCommented:
have you tried a component like devexpress quantumgrid  ?

it might be expensive, but it would allow you to set the text align = alCenter
for a column
LeTayAuthor Commented:
Delphi XE2
LeTayAuthor Commented:
Maybe some unit is missing in my uses clause ?
bokistCommented:
Here is my uses clause :
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, StdCtrls, ExtCtrls, Buttons, Db, DBTables, Printers, ComCtrls;
LeTayAuthor Commented:
I adapted but it does not compile
Can You click on that Display in your environment ?
It will show you the Display' unit
Thanks
bokistCommented:
Hi,
Sorry, I just found this in my project :

procedure Display(StringGrid: TStringGrid; const S: string; Rect: TRect; Alignment: TAlignment);
const
   Formats: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);

   procedure WriteText(StringGrid: TStringGrid; ACanvas: TCanvas; const ARect: TRect;
                      const Text: string; Format: Word);
   var
      DX, DY : integer;
      S : array[0..255] of Char;
   begin
      DX := 2;
      DY := 2;
      with StringGrid, ACanvas, ARect do
           begin
           case DefaultRowHeight of
                    1..19 : DY := 2;
                        20 : DY := 3;
                  21..25 : DY := 4;
           end;
           case Format of
                  DT_LEFT  : ExtTextOut(Handle, Left + DX, Top + DY,
                                     ETO_OPAQUE or ETO_CLIPPED, @ARect, StrPCopy(S, Text), Length(Text), nil);

                  DT_RIGHT : ExtTextOut(Handle, Right - TextWidth(Text) - 3, Top + DY,
                                       ETO_OPAQUE or ETO_CLIPPED, @ARect, StrPCopy(S, Text), Length(Text), nil);

                DT_CENTER: ExtTextOut(Handle, Left + (Right - Left - TextWidth(Text)) div 2,Top + DY,
                                       ETO_OPAQUE or ETO_CLIPPED, @ARect, StrPCopy(S, Text), Length(Text), nil);
            end;
        end;
    end;

begin
    WriteText(StringGrid, StringGrid.Canvas, Rect, S, Formats[Alignment]);
end;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LeTayAuthor Commented:
Many  many thanks
bokistCommented:
You are welcome...
Thank's for points and grading :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.