Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 03-10-2012, 07:22 PM
ShaunVW ShaunVW is offline
Member
 
Join Date: Feb 2012
Posts: 47
Default Using DrawText for multiline StringGrid

I have been trying to implement a multiline heading in my StringGrid, and have been trying the following code to do it, having changed the code to make it relevant to my StringGrid

Code:
procedure TmainForm.dailyDataStringGridDrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
var
 s: String;
 drawrect : TRect;
begin
 dailyDataStringGrid.Canvas.FillRect(Rect);
 s := dailyDataStringGrid.Cells[ ACol, ARow ];
 if Length(s) > 0 then //is there any text needing to be formatted?
  begin
   drawrect := rect;
   DrawText(dailyDataStringGrid.canvas.handle,  
            Pchar(s), Length(s), drawrect,
            dt_calcrect or dt_wordbreak or dt_left or dt_noprefix ); //how to format the text
   if (drawrect.bottom - drawrect.top) > dailyDataStringGrid.RowHeights[ARow] then //Does the row height need to be adjusted?
    dailyDataStringGrid.RowHeights[ARow] := (drawrect.bottom - drawrect.top)
   else
    begin
     drawrect.Right := rect.right;
     dailyDataStringGrid.canvas.fillrect( drawrect );
     DrawText(dailyDataStringGrid.canvas.handle,
              Pchar(s), Length(s), drawrect,
              dt_wordbreak or dt_left or dt_noprefix);
    end;
  end;
end;
I got this code while looking for solution to my needs, but I need to actually modify it, and need some help.
Originally when doing this for DBGrid, I came across some code that allowed the text to be split onto 2 lines when it encountered a specific symbol e.g. '_' in the text, and it used this to know what to put on the second line, i.e. you could specify exactly what went on the first line, and the second line.

How can I modify the above code to do this, e.g. if I want "Line 1 Line 2" split as
Quote:
Line 1
Line 2
I was thinking that this would be read initially as "Line 1_Line 2".

Also, what is the function of the first call to DrawRect if it then gets called again in the if-then clause?
I'm just trying to understand the code better.
Thanks.
Reply With Quote
  #2  
Old 03-10-2012, 08:24 PM
ShaunVW ShaunVW is offline
Member
 
Join Date: Feb 2012
Posts: 47
Default

I figured it out.
A carriage return also forces a new line, so I just specify my text as

'Line 1'#10'Line 2'

and the #10 forces the new line exactly where I want it.

Now how do I center the lines that fit on one line vertically?
When I use dt_singleline or dt_vcenter, then even the cell where I want double lines reverts back to single line!
Reply With Quote
  #3  
Old 05-28-2012, 07:43 AM
ShaunVW ShaunVW is offline
Member
 
Join Date: Feb 2012
Posts: 47
Default

I have another question w.r.t. this multiline text, it works well in titles using #10 to split the lines. (e.g. in the tile for the column)
In another application though, I am reading the text to be split over multiple lines from a database, and I want the text to be automatically split if needed. However, it doesn't want to split it correctly.

As an example, I make the column narrow enough that very little text will fit on a line. I then place some text in the cell, with a space where I would expect it to split the text over 2 lines, done in FormShow.

Code:
innovationsStringGrid.Cells[4,0] := 'Descri ption'; //split betw. i & p ->space
In OnDrawCell for StringGrid I have the following code:

Code:
procedure TstatusForm.innovationsStringGridDrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
var
 s : String;
 drawRect : TRect;
begin
 if ACol = 4 then
  begin
   innovationsStringGrid.Canvas.FillRect(Rect);
   s := innovationsStringGrid.Cells[ACol, ARow]; //the text in the cell
   if Length(s) > 0 then //is there any text needing to be formatted?
    begin
     drawRect := Rect;
     DrawText(innovationsStringGrid.canvas.handle,  //See http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
              Pchar(s), Length(s), drawRect,
              DT_CALCRECT OR DT_WORDBREAK OR DT_LEFT ); //format the text as required, then use drawRect later as per DT_CALCRECT
     if (drawRect.bottom - drawRect.top) > innovationsStringGrid.RowHeights[ARow] then //Does the row need to be adjusted?
      innovationsStringGrid.RowHeights[ARow] := (drawRect.bottom - drawRect.top) //adjust row height
     else
      begin //fits in one line
       drawRect.Right := Rect.Right;
       innovationsStringGrid.canvas.fillrect(drawRect);
       DrawText(innovationsStringGrid.canvas.handle,
                Pchar(s), Length(s), drawRect,
                DT_LEFT); //how to format the text
      end;
    end;
  end;
end;
The reason for DT_LEFT is because it is a description column, don't want it center alinged

However, this is my result is still the text left on a single line. The column is wide enough that I can see "Descri p" in the cell, the "ion" is not visble, however, I would expect it to be
"Descrip" --> first line
"tion" --> second line

Can someone tell me where I am going wrong please? Or how to do it better?
Thanks.
Reply With Quote
  #4  
Old 05-28-2012, 09:48 AM
ShaunVW ShaunVW is offline
Member
 
Join Date: Feb 2012
Posts: 47
Default

Thanks for anyone thinking about this, eventually found out that the DrawText in the "else" clause also had to include the DT_WORDBREAK flag.
Reply With Quote
Reply

Tags
drawrect, multiline, stringgrid

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 07:29 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2018, vBulletin Solutions, Inc.