Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Images in DBGrid

262 views
Skip to first unread message

FrancoJommi

unread,
Mar 19, 2010, 1:45:37 PM3/19/10
to
I use the folllowing proc to paint an image in a DBGrid field. I have two
problems
1-the image is correctly painted, but I can still see part of the field
value in the DBGrid column
2-I need to discern on a field value to paint or not to paint, but I don't
know hoe to test the boolean field value (I am using dBase)

This is the proc I am using to paint (from DelphiAbout..):

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer;Column: TColumn;
State: TGridDrawState);
var
bitmap : TBitmap;
fixRect : TRect;
bmpWidth : integer;
begin
fixRect := Rect;
if Column.Field = Table1Alarm then
begin
bitmap := TBitmap.Create;
try
ImageList1.GetBitmap(0,bitmap);
bmpWidth := (Rect.Bottom - Rect.Top);
fixRect.Right := Rect.Left + bmpWidth;
//draw the bitmap
DBGrid1.Canvas.StretchDraw(fixRect,bitmap);
finally
bitmap.Free;
end;
fixRect := Rect;
fixRect.Left := fixRect.Left + bmpWidth;
end;
DBGrid1.DefaultDrawColumnCell( fixRect, DataCol, Column,State);
end;

Jamie

unread,
Mar 20, 2010, 10:53:59 AM3/20/10
to
FrancoJommi wrote:

Do this prior to your painting.
You only need to directly paint you bmp afterwards, you don't need
to stretch it, unless you have a desire to do so.

DBGrid1.Canvas.FillRect(Rect);

This will use the current brush assigned to the canvas..

Also, you should be testing the "state" property incase you need to
perform a graying effect or select or unless effect..

Selecting usually involves changing the background colors or some
something different like an underline of check mark, ect..

FrancoJommi

unread,
Mar 20, 2010, 2:47:47 PM3/20/10
to
Thanks, I tried, but I can still see part of the field value aside the
image!

"Jamie" <jamie_ka1lpa_not_v...@charter.net> ha scritto nel
messaggio news:6s4pn.66268$gF5...@newsfe13.iad...

alang...@aol.com

unread,
Mar 20, 2010, 3:07:30 PM3/20/10
to
On 20 Mar, 18:47, "FrancoJommi" <franco.jo...@tin.it> wrote:
> Thanks, I tried, but I can still see part of the field value aside the
> image!
>
Look up DefaultDrawColumnCell in Delphi Help. It tells you what it
does. That shoild be enough to tell you why you're seeing the data
value.

Alan Lloyd

FrancoJommi

unread,
Mar 20, 2010, 4:11:28 PM3/20/10
to
I had already tried to look at the help, but it doesn't say anything to me
(as far as my problem is concerned). It says:
"Call DefaultDrawColumnCell from an OnDrawColumnCell event handler to lookup
up the text representation of a field and write it to the cell.
DefaultDrawColumnCell does exactly what the grid does when the
DefaultDrawing property is true, except it never draws a focus rectangle on
selected cells. The Rect parameter is the position of the cell on the
canvas. The DataCol parameter is the index of the column in the Columns
array. The Column parameter is the TColumn object that describes the display
attributes and field binding for the cell. The State parameter indicates
whether the cell has input focus, whether the cell is selected, and whether
the cell is a fixed (non-data) cell such as a column header. "
I tried using Rect to fill the cell with Canvas.FillRect(Rect), but with the
same result.


<alang...@aol.com> ha scritto nel messaggio
news:37a1d1a9-14d3-4815...@30g2000yqi.googlegroups.com...

Jamie

unread,
Mar 20, 2010, 8:25:50 PM3/20/10
to
FrancoJommi wrote:

Please do not top post...

Makes it hard to read the thread.
---
When calculating the width/height from a RECT object, you need to add
1 to each results . Ex. Width := RECT.Right-Rect.Left+1;

The RECT only tells you where the area starts and ends. So if you
were to subtract lets say a horizontal Line RECT, you'd end up with 0
for height. Because the TOp and Bottom would have the same value.

Stark

unread,
Mar 21, 2010, 12:14:22 PM3/21/10
to
It doesn't work! I tried everything and I am wasting so much time (and
wasting other people time) just to know why this happens.
Can you bear with me two minutes more ?
Going back to my routine, let me explain my statements:

fixRect := Rect; // ( actual values: Top=0, B=17, L=372 R=406)


if Column.Field = Table1Alarm then
begin
bitmap := TBitmap.Create;
try
ImageList1.GetBitmap(0,bitmap);

bmpWidth := (Rect.Bottom - Rect.Top); // Here i get the height of my
cell ( 17-0=17 )
fixRect.Right := Rect.Left + bmpWidth; // and adopt it as teh width
of mybitmap (fixRect.R= 372+17=389)
// now I draw the bitmap (Top=0, B=17, L=372 R=389)
DBGrid1.Canvas.StretchDraw(fixRect,bitmap);
finally
bitmap.Free;
end;
fixRect := Rect; // here I reset
fixRect.Left := fixRect.Left + bmpWidth; // here I add space forn the
bitmap


end;
DBGrid1.DefaultDrawColumnCell( fixRect, DataCol, Column,State);
end;

I tested the routine REMOVING the StretchDraw stetement and found that the
DBGrid column is painted left to right like this: "falFalse" (the field is a
boolean field), i.e. the cell is painted with the bitmap + its value.
Obviously I am making a mistake, but I can't see where I am mistaken !

"Jamie" <jamie_ka1lpa_not_v...@charter.net> ha scritto nel

messaggio news:_Pcpn.35535$NH1...@newsfe14.iad...

Jamie

unread,
Mar 21, 2010, 1:28:39 PM3/21/10
to
Stark wrote:

why are you calling the defaultDrawColumecell at the end, it just over
writes what you have.

Also, you still don't seem to understand or not reading what I am
telling you. You need to add "1" to the results of a RECT calculations
when determining the size of an element..

Also, I stated to stop, top posting!.

With that, I'll say no more until you reform yourself.!


Jamie

unread,
Mar 21, 2010, 2:20:12 PM3/21/10
to
Stark wrote:

FrancoJommi

unread,
Mar 21, 2010, 2:18:32 PM3/21/10
to
I didn't tell you. I changed Rect.Bottom - Rect.Top to Rect.Bottom -
Rect.Top + 1, but nothing had changed.
I will continue to "top posting", unless someone explains what that means or
explain waht I am supposed not to do. I normally use the "answer to
userGroup" menuitem to respond, strting from the line I wan to to respond
to.
Can you try to explain without using "topposting" which I do not understand
?

"Jamie" <jamie_ka1lpa_not_v...@charter.net> ha scritto nel

messaggio news:3zspn.52605$2r7....@newsfe05.iad...

Maarten Wiltink

unread,
Mar 21, 2010, 4:06:13 PM3/21/10
to
"FrancoJommi" <franco...@tin.it> wrote in message
news:4ba662eb$0$824$4faf...@reader5.news.tin.it...
[...]

> I will continue to "top posting", unless someone explains what that
> means or explain waht I am supposed not to do.

You are supposed to write your responses _below_ the text you are
replying to. Because it is the direction in which people read. From
top to bottom.

Groetjes,
Maarten Wiltink


alang...@aol.com

unread,
Mar 21, 2010, 4:21:26 PM3/21/10
to
On 20 Mar, 20:11, "FrancoJommi" <franco.jo...@tin.it> wrote:
> I had already tried to look at the help, but it doesn't say anything to me
> (as far as my problem is concerned). It says:
> "Call DefaultDrawColumnCell from an OnDrawColumnCell event handler to lookup
> up the text representation of a field and write it to the cell.
<snip>

In other words it writes the data to the cell. But you call it _after_
you have written the bitmap to the cell, thus overwriting the bitmap.

If DefaultDraw is true then the DefaultDrawColumnCell is called before
the OnDrawColumnCell event is called (see OnDrawColumnCell in Help).

Alan Lloyd

Jamie

unread,
Mar 21, 2010, 6:34:47 PM3/21/10
to
FrancoJommi wrote:

Please continue top posting. Have fun getting help.


0 new messages