sorry, the first reply went into my bulk folder... didn't see this until now.
I was under the mistaken belief that Sender in this event gave one the individual cell properties.. I was wrong, so the only advantage to Sender as TStringGrid is if you want to use the same code in multiple stringGrids (whoopee).
But I did stumble across some rather complete code for custom drawing a stringGrid cell and nowhere does he use a pointer or pointer reference and I don't found the need for doing so in my code. So here it is (compliments of Pete Below I would guess since I got it from borland.public.delphi.vcl.components.using):
procedure DrawSGCell(Sender : TObject; C, R : integer; Rect : TRect;
Style : TFontStyles; Wrap : boolean; Just : TAlignment;
CanEdit : boolean);
{ draws formatted contents in string grid cell at col C, row R;
Style is a set of fsBold, fsItalic, fsUnderline and fsStrikeOut;
Wrap invokes word wrap for the cell's text; Just is taLeftJustify,
taRightJustify or taCenter; if CanEdit false, cell will be given
the background color of fixed cells; call this routine from
grid's DrawCell event }
var
S : string;
DrawRect : TRect;
begin
with (Sender as tStringGrid), Canvas do begin
{ erase earlier contents from default drawing }
if (R >= FixedRows) and (C >= FixedCols) and CanEdit then
Brush.Color:= Color
else
Brush.Color:= FixedColor;
FillRect(Rect);
{ get cell contents }
S:= Cells[C, R];
if length(S) > 0 then begin
case Just of
taLeftJustify : S:= ' ' + S;
taRightJustify : S:= S + ' ';
end;
{ set font style }
Font.Style:= Style;
{ copy of cell rectangle for text sizing }
DrawRect:= Rect;
if Wrap then begin
{ get size of text rectangle in DrawRect, with word wrap }
DrawText(Handle, PChar(S), length(S), DrawRect,
dt_calcrect or dt_wordbreak or dt_center);
if (DrawRect.Bottom - DrawRect.Top) > RowHeights[R] then begin
{ cell word-wraps; increase row height }
RowHeights[R]:= DrawRect.Bottom - DrawRect.Top;
SetGridHeight(Sender as tStringGrid);
end
else begin
{ cell doesn't word-wrap }
DrawRect.Right:= Rect.Right;
FillRect(DrawRect);
case Just of
taLeftJustify : DrawText(Handle, PChar(S), length(S), DrawRect,
dt_wordbreak or dt_left);
taCenter : DrawText(Handle, PChar(S), length(S), DrawRect,
dt_wordbreak or dt_center);
taRightJustify : DrawText(Handle, PChar(S), length(S), DrawRect,
dt_wordbreak or dt_right);
end;
end
end
else
{ no word wrap }
case Just of
taLeftJustify : DrawText(Handle, PChar(S), length(S), DrawRect,
dt_singleline or dt_vcenter or dt_left);
taCenter : DrawText(Handle, PChar(S), length(S), DrawRect,
dt_singleline or dt_vcenter or dt_center);
taRightJustify : DrawText(Handle, PChar(S), length(S), DrawRect,
dt_singleline or dt_vcenter or dt_right);
end;
{ restore no font styles }
Font.Style:= [];
end;
end;
end;
- Hide quoted text -
- Show quoted text -
Dave Murray < irongut%40paranoia.clara.co.uk">irongut
paranoia.clara.co.uk> wrote:
Sender is the component calling your event handler. For example it's
not uncommon to use the same method for more than one button click
event when they perform very similar tasks. You then write code like:
if (Sender as TButton).Name = 'btnPrint' then
begin
// do stuff
end
else (Sender as TButton).Name = 'btnTestPrint' then
begin
// do slightly different stuff
end;
// do stuff common to both print buttons
You need to cast the Sender object to whatever type you expect it to
be because it is of type TObject which doesn't have properties like
Canvas or Handle. So if you're using Sender instead of StringGrid1
you'll have some code like: (Sender as TStringGrid).Canvas.Handle
Hope this helps,
Dave.
Dave Murray
Glasgow, UK
--- In delphi-en%40yahoogroups.com">delphi-en
yahoogroups.com, "Nesler, Thomas J" <tnesler
...> wrote:
>
> Hi David!
>
> Thanks for the tip...However I can't get it to work...:-(
>
> Can you suggest how I would incorporate Sender into my code?
>
> Thanks in advance!
>
> Tom Nesler
>
>
> -----Original Message-----
> From: delphi-en%40yahoogroups.com">delphi-en
yahoogroups.com [mailto: delphi-en%40yahoogroups.com">delphi-en
yahoogroups.com] On
> Behalf Of David Smith
> Sent: Wednesday, March 19, 2008 10:45 AM
> To: delphi-en%40yahoogroups.com">delphi-en
yahoogroups.com
> Subject: RE: [delphi-en] Working with ExtTextOut
>
>
> You should use Sender instead of StringGrid1. That's what it's for.
>
> "Nesler, Thomas J" <tnesler
...> wrote:
> Hi Rob!
>
> Your observation combined with Theo's code cleanup fixed the problem.
>
> Here is the final code for those who may need to do this in the future:
>
> // *** This procedure Centers all cells in the grid ****
>
> procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow:
> Integer;
> Rect: TRect; State: TGridDrawState);
> var
> Text : string;
> Begin
> Text := StringGrid1.Cells[Acol,ARow];
> ExtTextOut(StringGrid1.Canvas.Handle,
> Rect.Left + (Rect.Right - Rect.Left -
> StringGrid1.Canvas.TextWidth(Text)) div 2,
> Rect.Top + 2,
> ETO_OPAQUE or ETO_CLIPPED,
>
Rect,
> PChar(Text),
> Length(Text),
> nil);
> end;
>
> //************** End of Code ***********
>
> Thanks for your input, Rob
>
> Tom Nesler
> Live long!... Code Well!... and Prosper!... V
>
> -----Original Message-----
> From: delphi-en%40yahoogroups.com">delphi-en
yahoogroups.com [mailto: delphi-en%40yahoogroups.com">delphi-en
yahoogroups.com] On
> Behalf Of Rob Kennedy
> Sent: Tuesday, March 18, 2008 8:07 PM
> To: delphi-en%40yahoogroups.com">delphi-en
yahoogroups.com
> Subject: Re: [delphi-en] Working with ExtTextOut
>
> ....
> > ExtTextOut(Handle,
> > // HDC
> > Rect.Left + (Rect.Right - Rect.Left -
> > TextWidth(Text)) div 2, // X Position
> > Top + DY,
>
> Do you mean Rect.Top?
>
>
>
>
>
>
> ---------------------------------
> Never miss a thing. Make Yahoo your homepage.
>
> [Non-text portions of this message have been removed]
>
>
> ------------------------------------
>
> -----------------------------------------------------
> Home page: http://groups.yahoo.com/group/delphi-en/
> To unsubscribe: delphi-en-unsubscribe
...! Groups Links
>
---------------------------------