Post by StarkPost by StarkPost by BRobertsPost by StarkIn a DBGrid I show the memo field of my dBase file using the followin
� �DrawText(Canvas.Handle, PChar(Column.Field.AsString),
� � � �Length(Column.Field.AsString), R,
� � � �DT_WORDBREAK OR DT_NOPREFIX);
What I would like is to know if my string fills the space of the DBGrid
column. I it doesn't, may be I can do something about it.
But how do I calculate the space occupied by the string, given that a
font or another can be used, etc.
It must be over complicated, so I tried something and gave up after a
while. Can anyone help?
Take a look at the various flags available to DrawText. There is one that
will, rather than paint the text, return the text width - DT_CalcRect. It
is also that it's possible to instruct DrawText to include ellipsis if
the text exceeds the given rectangle - DT_End_Ellipsis. Check the Win32
SDK help or at msdn.com.
If different fonts are being used set the appropriate Canvas.Font
properties immediately before calling DrawText. If memory serves I
generally do something like
� �var � �svFont : tFont;
� �. . .
� �svFont := tFont.Create;
� �try
� � � �svFont.Assign (aCanvas.Font);
� � � �aCanvas.Font.Name := drawFontName;
� � � �aCanvas.Font.Size := drawFontSize;
� � � �aCanvas.Font.Style := drawFontStyle;
� � � �// set up draw text parameters and then execute DrawText
� �finally
� � � �aCanvas.Font.Assign (svFont);
� � � �svFont.Free;
� � � �end;
OK, I used the DrawText function with the DT_CALCRECT flag that retuned a
value. Now I want to know if this value will fit or not the space I have
available in this column of the DBGrid. How do I know the total available
space ?
I tried several tests with DT_CALCRECT modifying the string and I think I
found values that look very strange to me. The string "a word" returns a
value 13 and the string "a word xxxxxxxxx" still gives a value 13. On the
other hand, a shorter strin contained in another memo field returns a bigger
value of 26.
I also tried to use the expression
"Canvas.TextWidth(Column.Field.AsString)"
in parallel with the above to compare results and these are totally
different...
I am on the point of giving up understanding...- Hide quoted text -
Post a copy of some of your code, particularly the DrawText call, but
also any other associated with your attempt to measure the width or
rect.
Have you looked up DrawText on an MSDN (Mictosoft Development Network)
Library. There are a lot of flags associated with DrawText which may
affect you.
Alan Lloyd
The Draw routine is very long, so I am posting the part that deals with the
memo field. Here it is, supplemented with the debug code I have used to
compare sizes:
..........................................................
if ((Column.Field is TMemoField)) then
begin
Canvas.FillRect(Rect);
if Column.Field.AsString <>'' then
begin
R:=Rect;
Canvas.FillRect(R);
Inc(R.Top,2);
Inc(R.Left,2);
H:= DrawText(Canvas.Handle, PChar(Column.Field.AsString),
Length(Column.Field.AsString), R,
DT_CALCRECT);
// Debug code
ListBox1.Items.Add(Column.Field.AsString);
ListBox1.Items.Add('H='+inttostr(H);
ListBox1.Items.Add('L='+inttostr(Length(Column.Field.AsString));
ListBox1.Items.Add('C='+inttostr(Canvas.TextWidth(Column.Field.AsString));
DrawText(Canvas.Handle, PChar(Column.Field.AsString),
Length(Column.Field.AsString), R,
DT_NOCLIP OR DT_WORDBREAK);
end;
end else
// OTHER COLUMNS_ CENTER TEXT IN THE HEIGHT
begin
Txt := Column.Field.Text;
H := Dbgrid1.Canvas.TextHeight(Txt); //senza H e W non è
perfett.centrato
W := Dbgrid1.Canvas.TextWidth(Txt);
X := Rect.Left;
Y := (Rect.Bottom + Rect.Top - H) DIV 2;
//
Canvas.TextRect(Rect, X, Y, Txt );
end;
end;
end; //with Sender as TDBGrid