Center text in a TStringGrid cell

Is it possible (and how) to center a text in TStringGrid cell ?
LVL 1
LeTayAsked:
Who is Participating?
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;
0
LeTayAuthor Commented:
The Display (..) does not compile (undeclared identifier)
0
bokistCommented:
Which version of delphi are you using?
Example works with D7.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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
0
LeTayAuthor Commented:
Delphi XE2
0
LeTayAuthor Commented:
Maybe some unit is missing in my uses clause ?
0
bokistCommented:
Here is my uses clause :
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, StdCtrls, ExtCtrls, Buttons, Db, DBTables, Printers, ComCtrls;
0
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
0
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;
0

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
0
bokistCommented:
You are welcome...
Thank's for points and grading :)
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.