Delphi - Is it possible to draw on the canvas a selected cell in a grid of rows?

I want to draw a cell canvas in a grid of rows. This will be on top of the image previously loaded into the row grid.

What I have

Currently, instead of drawing on top of the image, I load a second transparent image and then draw on top of the cell. This is the code I use and it works.

procedure TfrmCavern.StringGridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var index : integer; I : integer; begin // Calculate the corresponding linear index index := LinearIndexOf(ARow, ACol); //Draw image referenced to cell StringGrid.Canvas.StretchDraw(Rect, CellDetails[index].Images.Picture.Graphic); //if player present draw corresponding player image for I := 0 to frmWelcome.NoofPlayers - 1 do begin if index = Players[I].pIndex then StringGrid.Canvas.StretchDraw(Rect,Players[I].UserImage.Picture.Graphic); end; end; end; 

First, the procedure draws an image related to the cell. If a “player” is present, he will then draw the top of the player. Since the “part of the play” image is a transparent .PNG image, the image below it is still visible.

What I want

The rollback of this method is that the "part of the player" is in a predetermined position inside the cell due to the constant image. I want to be able to draw the “player part” in a different position inside the cell, depending on the selected cell. I have about 200 cells, so I don’t want to manually create a lot of images with different positions.

What i tried

I tried to draw directly on the stringgrid canvas in the drawcell procedure, but it seemed to refer to the entire stringgrid line, not the current cell that was drawn.

 StringGrid.Canvas.ellipse(10,10,50,50); 

I looked, but I can't seem to reference the canvas of the current cell - I suppose it doesn't exist?

The next thing I tried is to draw a temporary image and then draw the image in a cell.

 TempImage.Canvas.Ellipse(10,10,50,50); StringGrid.Canvas.StretchDraw(Rect, TempImage.Picture.Graphic); 

This worked to some extent, it drew the image in the cell, however, the image had an opaque background / canvas, so the cell was white with a circle on it, the image below was not visible. I learned a little, but could not find a way to make the image canvas transparent.

The last thing I can come up with is to write an algorithm to find the top left point of the current cell and then draw directly on the canvas from there, but it can be sloggy and cause problems when redrawing the stringgrid.

Can anyone see a way for my problem?

Thanks in advance, Josh

+7
source share
2 answers

The Rect parameter for OnDrawCell indicates the boundaries of a particular cell relative to the grid client coordinate system. Thus, you need to draw the coordinates lying inside this rectangle. Try something like this in an event handler:

 StringGrid.Canvas.Pen.Color := clBlack; StringGrid.Canvas.Brush.Style := bsClear; StringGrid.Canvas.Ellipse( Rect.Left+5, Rect.Top+5, Rect.Left+15, Rect.Top+15 ); 
+3
source

There is only one canvas, control canvas. Each cell is simply a rectangle inside this canvas. It is trivial to find the position of the current cell in the canvas. In fact, this is a Rect parameter. Rect.Left is the x coordinate of the cell, and Rect.Top is the y coordinate of the cell.

Or am I misinterpreting your question?

+4
source

All Articles