Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to align text in a String Grid Control

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi. One of the more specific questions is how to show progress of the thread. Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the
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
Weve all felt that sense of false security beforelocking down external access to a database or component and feeling like weve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ

971 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