Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to align text in a String Grid Control

Posted on 1998-09-18
4
Medium Priority
?
1,271 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 150 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr
Suggested Courses

705 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