Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to Show images in the cells of a TStringGrid Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
04-Nov-02
Category
VCL-General
Language
Delphi 2.x
Views
108
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

How to show images in the cells of a TStringGrid

Answer:

Solve 1:

The following example uses a TStringGrid to display the bitmaps from the filename 
strings stored in each cell. In the TStringGrid, I set the DefaultRowHeight to 96 
and the DefaultColWidth to 128 (with ColCount = 1).

Here's the OnDrawCell that does all the work:
1   
2   procedure TForm1.StringGridImageSourceDrawCell(Sender: TObject;
3     Col, Row: Integer; Rect: TRect; State: TGridDrawState);
4   var
5     Bitmap: TBitmap;
6     Filename: string;
7   begin
8     Filename := (Sender as TStringGrid).Cells[Col, Row];
9     if Filename <> NoImagesLoaded then {special "kludge" case}
10    begin
11      Bitmap := TBitmap.Create;
12      try
13        Bitmap.LoadFromFile(Filename);
14        (Sender as TStringGrid).Canvas.StretchDraw(Rect, Bitmap);
15      finally
16        Bitmap.Free
17      end;
18      {Draw blue outline around selected row}
19      if Row = (Sender as TStringGrid).Row then
20      begin
21        with (Sender as TStringGrid).Canvas do
22        begin
23          Pen.Color := clBlue;
24          Pen.Width := 5;
25          MoveTo(Rect.Left + 2, Rect.Top + 2);
26          LineTo(Rect.Right - 2, Rect.Top + 2);
27          LineTo(Rect.Right - 2, Rect.Bottom - 2);
28          LineTo(Rect.Left + 2, Rect.Bottom - 2);
29          LineTo(Rect.Left + 2, Rect.Top + 2)
30        end;
31      end;
32    end;
33  end;



Solve 2:

In your StringGrid's OnDrawCell event handler, place some code that resembles: 

34  with (Sender as TStringGrid) do
35    with Canvas do
36    begin
37      {...}
38      Draw(Rect.Left, Rect.Top, Image1.Picture.Graphic);
39      {...}
40    end;


Using the Draw() or StretchDraw() method of TCanvas should do the trick. BTW, Image1 above is a TImage with a bitmap already loaded into it.

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC