Solved

How to align text in a String Grid Control

Posted on 1998-09-18
4
1,205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to
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
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https
Suggested Courses
Course of the Month10 days, 9 hours left to enroll

632 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