delphi dbgrid drawing cell image black background - delphi

Problem using image from a TImage list to draw a glyph on to a data cell in DBGrid:
I am putting a bmp image of a "checkmark" in place of the text "Done" in a particular data cell. It works, but there is always black color in the parts of the cell not covered by the image. I have tried enlarging the pixel size of the bmp image to match the cell size, but it always seems to resize the image for me. Using Delphi 10.2, was not problem in D7?
Have tried many combos of setting background colors, pen and brush colors, etc. Here is a simple example of one code attempt:
procedure TFUpRepWS.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
with Column do begin
if ((FieldName = 'Done') and (Field.AsString = 'x')) then begin
//below shows black outside of check mark image in the cell
ImageList1.Draw(DBGrid1.Canvas,Rect.Left,Rect.Top,0)
end
else DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
end;

Do the default cell painting DefaultDrawColumnCell always. That will ensure the cell will look like the others. Then draw the image. Try this:
procedure TFUpRepWS.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
with Column do
begin
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
if ((FieldName = 'Done') and (Field.AsString = 'x')) then
ImageList1.Draw(DBGrid1.Canvas, Rect.Left, Rect.Top, 0);
end;
end;
I guess that what you described happens because there is no code that paints the cell background.

Related

Why DevExpress TCxGrid AViewInfo.EditViewInfo.Paint(ACanvas); neglects assigned font color?

I want to format DevExpress TcxGrid (DBTableView) in such manner that selected special cells contain blue text and the top border in red color. I have arrived at the following code:
procedure TTestFrame.ClipsGridCClipsViewCustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var TmpBounds: TRect;
begin
if IsSpecialCell(...) then begin
ACanvas.Font.Color:=clBlue;
AViewInfo.EditViewInfo.Paint(ACanvas);
TmpBounds:=AViewInfo.Bounds;
TmpBounds.Bottom:=TmpBounds.Top+2;
ACanvas.FillRect(TmpBounds, clRed);
ADone:=True;
end;
end;
Unfortunately this code neglects the blue font color and draws the cell in default formatting and only adds red top border. I guess that my problem would be solved if AViewInfo.EditViewInfo.Paint(ACanvas); had taken into account the assigned color. Unfortunately, this procedure uses default formatting.
So - how to solve my problem? And, generally, is it possible to draw some content in OnCustomDraw (e.g. inner border of the cell with FillRec) and to draw some other content in default drawing procedure (e.g. usual content of the cell)? At present it seems to me that I have to take choice - whether to draw everything in OnCustomDrawCell or let Grid draws everything and I can make only small configuration of some drawing parameters (font color, brush color) in OnCustomDrawCell.
For access to private function
TcxPainterAccess = class(TcxGridTableDataCellPainter);
procedure TTestFrame.ClipsGridCClipsViewCustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var APainter : TcxPainterAccess ;
begin
APainter := TcxPainterAccess(TcxViewInfoAcess(AViewInfo).GetPainterClass.Create(ACanvas, AViewInfo));
try
with Apainter do
DrawContent;
DrawBorders;
your code....
finally
Adone := True;
Free;
end;
end;

delphi 7: How to update StringGrid cells color dependence on a state of boolean variable

I would like to have: if a boolean variable will set to true, should a cell (error message) in StringGrid be red. It isn't works automatically with OnDrawCell.
How can I achieve it? Thanks in advance.
In the OnDrawCell event, check the state of the boolean variable and if the correct cell is about to be drawn, set the colour to red.
See delphi : how can I change color of a cell in string grid.
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
if (myBooleanState) and (ACol = 3) and (ARow = 2) then
with TStringGrid(Sender) do
begin
//paint the background red
Canvas.Brush.Color := clRed;
Canvas.FillRect(Rect);
Canvas.TextOut(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]);
end;
end;
When the state of the boolean is changed, just call MyStringGrid.Repaint or MyStringGrid.Invalidate.

change colors of some rows in dbgrid delphi 5

I have a problem, I have an TStringList object with some numbers and his status in some kind of json text I created, like: {'8987436','Sin documentar.', '0','1'}, {...},...
This file can contain a lot of groups of data. This works really fine, this information I displayed in a DBGrid, only the numbers.
The problem is that when I try to change the color of the rows, only draw the color of the last number added to the dbgrid, and I need that each row with some type of number draw in yellow, other kind in red, other kind in green and all the other let it white. The other problem is that when I click on one row it refresh the dbgrid and paint it again in white.
I created this procedure:
procedure TEmbarqueGeneracionEscaneoFRM.DBValidasDrawColumnCell(
Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
inherited;
if Pinta then
begin
with (Sender As TDBGrid).Canvas do
begin
brush.Color:=GridColor;
FillRect(Rect);
TextOut(Rect.Left, Rect.Top, Column.Field.AsString);
end;
TDBGrid(sender).DefaultDrawColumnCell(Rect, Datacol, Column, State);
end;
Pinta := false;
end;
Where Pinta is a variable that tell if the number was painted, and GridColor is a TColor variable with the color that will be drawn.
Did you have any idea?
Regards

How to change the color of TStringGrid grid-lines?

I've just finished migrating from D7 to XE2 and I've noticed that the default grid-lines are extremely faint (it doesn't help that I like to set the contrast on my monitor to high), as you can see in the screenshot below:
This was my attempt to re-color the lines darker by setting the TStringGrid's OnDrawCell event:
procedure TfrmBaseRamEditor.DrawStrGrid(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
sgrSenden.Canvas.Pen.Color := clDkGray;
// "Set the Style property to bsClear to eliminate flicker when the object
// repaints" (I don't know if this helps).
sgrSenden.Canvas.Brush.Style := bsClear;
// Draw a line from the cell's top-right to its bottom-right:
sgrSenden.Canvas.MoveTo(Rect.Right, Rect.Top);
sgrSenden.Canvas.LineTo(Rect.Right, Rect.Bottom);
// Make the horizontal line.
sgrSenden.Canvas.LineTo(Rect.Left, Rect.Bottom);
// The other vertical line.
sgrSenden.Canvas.LineTo(Rect.Left, Rect.Top);
end;
But this produces a result even less desirable, notice especially the border of the active cell:
Is there any way to make these grid-lines darker or thicker in a way that doesn't look as ugly as my attempt?
As per the answer to this question, I simply set DrawingStyle property of the TStringGrid to gdsClassic.

Draw TBitmaps in DrawGrid in Delphi

I have an 8 x 16 DrawGrid in Delphi XE5 that I would like to randomly fill with nine images I've stored in C:\Users\Sean Ewing\Documents\My Documents\Delphi Tutorials\Other\Math-O-Sphere\Win32\Debug\img. I'm currently trying to get one image to load to make sure I'm doing it correctly. Here is the code I've used to do this:
procedure TForm1.grdPlayFieldDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
spherePlus: TBitmap;
begin
spherePlus.LoadFromFile(ExtractFilePath(Application.ExeName) + '\img\Sphere +1.bmp');
grdPlayField.Canvas.Draw(0, 0, spherePlus);
end;
The code compiles fine, and based on what I've read in the Embarcadero wiki this is correct, but I get an error at runtime when it's time to load the DrawGgrid. Where did I go wrong?
You need to first create the bitmap before you can use it:
procedure TForm1.grdPlayFieldDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
spherePlus: TBitmap;
begin
spherePlus := TBitmap.Create;
try
spherePlus.LoadFromFile(ExtractFilePath(Application.ExeName) +
'\img\Sphere +1.bmp');
grdPlayField.Canvas.Draw(0, 0, spherePlus);
finally
spherePlus.Free;
end;
end;
The other thing you should be aware of is that the Rect parameter you receive in the event is the area that needs to be painted, so you'll want to use Canvas.StretchDraw and pass it that rectangle. It won't help with the current issue, but you'll need it when you move to the next step. You can identify the exact cell that's being drawn with the ACol and ARow parameters, so you can use that information to load a specific image for a column, for instance, or to output text for a column or row.
// Load specific image for the cell passed in ACol and ARow,
// and then draw it to the appropriate area using the Rect provided.
grdPlayField.Canvas.StretchDraw(Rect, spherePlus);

Resources