Solved

How to align text in a String Grid Control

Posted on 1998-09-18
4
1,056 Views
Last Modified: 2010-05-18
Hi!! I need to align the text of the row in a grid control in the middle of the row... how can I accomplish that?

Thanks in advance
Jaime
0
Comment
Question by:skel
  • 2
4 Comments
 
LVL 4

Expert Comment

by:erajoj
ID: 1340202
0
 

Author Comment

by:skel
ID: 1340203
Thanks.. I've downloaded one of those controls. but I forgot to tell you that I need to align the text in the middle of the cell, but vertically. That control only aligns it hortizontally.
Can you explain me how can I use ownerdraw? Other fact I have observed with that control in torry site, that is visible slower that the StringGrid coming with Delphi 3. This is bad for my program, so I need to try another solution.

Thanks in advance
Jaime
0
 

Expert Comment

by:sakya
ID: 1340204
Hello.

this is a example.
--------------------

procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
  Rect: TRect; State: TGridDrawState);
var
   xpos,
   Mode:  Integer;
begin
   if Col = 1 then begin       //right side
      Mode := DT_RIGHT or TA_TOP;
      xpos := Rect.Right - 2;
   end
   else if Col = 2 then begin  //left side
      Mode := DT_LEFT or TA_TOP;
      xpos := Rect.Left + 2;
   end
   else begin                  //middle
      Mode := TA_CENTER or TA_TOP;
      xpos := (Rect.Left + Rect.Right) div 2;
   end;
   SetTextAlign(StringGrid1.Canvas.Handle, Mode);
   ExtTextOut(StringGrid1.Canvas.Handle, xpos, Rect.Top + 5,
          ETO_CLIPPED or ETO_OPAQUE, @Rect,
          PChar(StringGrid1.Cells[Col, Row]),
          Length(StringGrid1.Cells[Col, Row]), nil);

end;

if you want to display characters in center vertically, you can change 'Rect.Top + 5' to a proper value.

Sakya
0
 

Accepted Solution

by:
sakya earned 50 total points
ID: 1340205
Hello.

I changed my code in order to display in the middle of cells vertically and horizontally.

Please test my next code.
--------------------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, StdCtrls;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    procedure StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Button1Click(Sender: TObject);
  private
    { Private 宣言 }
  public
    { Public 宣言 }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
  Rect: TRect; State: TGridDrawState);
var
   FontSize,
   xpos,
   Mode:  Integer;
begin
   if Col = 1 then begin       //right side
      Mode := DT_RIGHT or TA_TOP;
      xpos := Rect.Right - 2;
      FontSize := 9
   end
   else if Col = 2 then begin  //left side
      Mode := DT_LEFT or TA_TOP;
      xpos := Rect.Left + 2;
      FontSize := 15;
   end
   else if Col = 3 then begin                  //center
      Mode := TA_CENTER;
      xpos := (Rect.Left + Rect.Right) shr 1;
      FontSize := 11;
   end
   else begin                  //center
      Mode := TA_CENTER;
      xpos := (Rect.Left + Rect.Right) shr 1;
      FontSize := 14;
   end;

   SetTextAlign(StringGrid1.Canvas.Handle, Mode);
   StringGrid1.Canvas.Font.Size := FontSize;

   ExtTextOut(StringGrid1.Canvas.Handle, xpos, (Rect.Top + ((Rect.Bottom - Rect.Top) - FontSize ) shr 1),
          ETO_CLIPPED or ETO_OPAQUE, @Rect,
          PChar(StringGrid1.Cells[Col, Row]),
          Length(StringGrid1.Cells[Col, Row]), nil);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     StringGrid1.Cells[3,3] := 'Hello';
     StringGrid1.Cells[1,1] := 'Hello';
     StringGrid1.Cells[2,2] := 'Hello';
     StringGrid1.Cells[4,1] := 'Good Bye';
     StringGrid1.Cells[3,1] := 'YAHOO';
end;

------------------------------------------

sakya

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this months webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now