Solved

How to align text in a String Grid Control

Posted on 1998-09-18
4
1,036 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo

707 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

15 Experts available now in Live!

Get 1:1 Help Now