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

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;

Related

How to change background color of an TListview item?

How can I customize my listview to display different background colors like in the picture below ?
My listview is bound to a datasource (Livebindng). I want to use the color field to set my backgroud color.
I've customized my view this way :
3 Text items (Designation,Date and Resume)
1 Bitmap item (Couleur)
Text items are bound to datasource but there is no way to bind my Bitmap to my "color" field.
I've filled the listview ActivesUpdateObjects event but this is not enough as bitmap is not changed when datasource record is updated!
procedure TfrmMain.lvTachesActivesUpdateObjects(const Sender: TObject;
const AItem: TListViewItem);
begin
SetItemColor(AItem);
end;
procedure TfrmMain.SetItemColor(const AItem: TListViewItem; const UpdateColor:
Boolean = False);
var
LObject: TListItemImage;
VC: TColor;
begin
LObject := AItem.Objects.FindObjectT<TListItemImage>('Couleur');
VC:= dtmMain.qrTaches.FieldByName('couleur').AsInteger;
if LObject.Bitmap = nil then
begin
LObject.Bitmap := FMX.Graphics.TBitmap.Create(10,240);
LObject.Bitmap.Clear(VC);
end else if UpdateColor then LObject.Bitmap.Clear(VC);
end;
Is there a better way to proceed? I was also looking to use style but it appears (or I didn't find) that itemlistview can apply styles!
Ps : Firemonkey / Windows / Delphi Berlin XE10.1
I'm using Delphi 7 so take this with a grain of salt.
You may have to write your own CustomDrawItem method on your TreeView to handle this stuff
This is mine (I edited out some code because it has some lengthy logic behind). Also, I don't draw icons so the DrawImage part is commented.
procedure TVentanaVisorComponentes.TreeView1CustomDrawItem(
Sender: TCustomTreeView; Node: TTreeNode; State: TCustomDrawState;
var DefaultDraw: Boolean);
var
NodeRect: TRect;
EsSeleccion, EsDespejado: boolean;
begin
with TreeView1.Canvas do
begin
//If DefaultDraw it is true, any of the node's font properties can be
//changed. Note also that when DefaultDraw = True, Windows draws the
//buttons and ignores our font background colors, using instead the
//TreeView's Color property.
DefaultDraw := False;
//DefaultDraw = False means you have to handle all the item drawing yourself,
//including the buttons, lines, images, and text.
if not DefaultDraw then
begin
Brush.Color := clMenuHighLight;
Font.Color := clWhite;
NodeRect := Node.DisplayRect(True);
FillRect(NodeRect);
// ...
NodeRect := Node.DisplayRect(False);
// ...
FillRect(NodeRect);
NodeRect.Left := NodeRect.Left + (Node.Level * TreeView1.Indent);
//NodeRect.Left now represents the left-most portion of the expand button
DrawButton(NodeRect, Node);
NodeRect.Left := NodeRect.Left + TreeView1.Indent;
//NodeRect.Left is now the leftmost portion of the image.
//DrawImage(NodeRect, Node.ImageIndex);
// NodeRect.Left := NodeRect.Left + ImageList.Width;
//Now we are finally in a position to draw the text.
TextOut(NodeRect.Left, NodeRect.Top, (Node as TNodoArbolComponentes).Texto);
end;
end;
end;

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 color of tcxgrid row

I want to show some rows from tcxgrid in different color (Depend upon column value ).
I did changes for it but its not getting reflected on grid after running my project.
procedure TfrmMessaging.cxGrid1DBTableView1CustomDrawCell(..);
Var
i : Integer;
begin
For i := 0 To cxGrid1DBTableView1.ViewData.RowCount - 1 Do
Begin
If cxGrid1DBTableView1.ViewData.Rows[i].Values[4] = '1' Then
Begin
cxGrid1.Canvas.Brush.Color := clRed;
End;
End;
end;
In above code I have used cxGrid1DBTableView1CustomDrawCell event of tcxgrid.
Thanks in advance.
If you are using a data-aware view (as it seems) you need to use the DataController instead of the ViewData to get to the records.
As stated in DevExpress help for TcxGridDBTableView (bold format is mine):
The TcxGridDBTableView object represents the data-aware version of the grid Table View. It inherits all functionality from its ancestor, except for data binding settings. The DataController.DataSource property of the TcxGridDBTableView provides the connection between the View and a TDataSet or its descendant.
Besides that, the OnCustomDrawCell event fires for every cell, so you do not need to iterate the rows.
Following code should help you:
procedure TfrmMessaging.cxGrid1DBTableView1CustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
if Sender.DataController.GetValue(AViewInfo.GridRecord.RecordIndex, 4) = '1' then
ACanvas.Brush.Color := clRed;
end;
Normally the easiest path for stuff like that are cxStyles. Drop a style repository on the form, add one or more styles to it and assign them in the object inspector or in an event handler (OnGetContentStyle etc.).
One advantage over custom drawing is that styles are considered for various calculations while owner drawn cells aren't handled specially and sometimes aren't autosized correctly etc.
How I change the color of the grid
procedure TfrmNewOffer.GrdOffDetailViewRemarkCustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
backgroundColorCode: Variant;
textColorCode: Variant;
begin
inherited;
if assigned(AViewInfo) and assigned(AViewInfo.GridRecord) then
begin
backgroundColorCode := AViewInfo.GridRecord.Values[GrdOffDetailViewBackColorCode.Index];
textColorCode := AViewInfo.GridRecord.Values[GrdOffDetailViewTextColorCode.Index];
if not VarIsNull(backgroundColorCode) then
begin
ACanvas.Brush.Color := backgroundColorCode;
end;
if not VarIsNull(textColorCode) then
begin
ACanvas.Font.Color := textColorCode;
end;
end;
end;

Delphi TDBGrid How to change selected color when style is gdsGradient

I'm just trying to use delphi XE, before that I've been a big fan of Delphi7.
I see the new dbgrid allows to use themed and gradient styles.
I'm using gradient and set rowselect, it has a property for gradient-start and -end for the column header.
But where is the property to set the selected color ?
It's strange because the color doesn't match, selected color is always a blue gradient.
I can do it with customdraw, I just want to know if there is anyway to change it without custom drawing.
The selected color comes from the OS.
There it's coded as clHighlight.
You cannot change it as such, but you can subclass the dbgrid and override the DrawCell method.
Or even easier add a onDrawCell eventhandler.
procedure TForm1.DBGrid1DrawCell(Sender: TObject, const Rect: TRect; Field: TField; State: TGridDrawState);
var
index: Integer;
begin
if not(gdSelected in State) then DefaultDrawCell(Rect, Field, State)
else begin
index := ARow * DBGrid1.ColCount + ACol;
DBGrid1.Canvas.Brush.Color := clYellow; <<-- some color
DBGrid1.Canvas.FillRect(Rect);
if (gdFocused in State) then begin
DBGrid1.Canvas.DrawFocusRect(Rect);
end;
ImageList1.Draw(DBGrid1.Canvas,Rect.Left,Rect.Top,index, True);
end;

How would you change the colour of the current selection in cxGrid?

How would you change the colour of the current selection in cxGrid?
Thank you.
You could assign a style to the selection by expanding the grid's view's "Styles" property and creating a new style in (a new) style repository for the "Selection" style. Double-click on the style repository component that gets created on your form to set the style's properties.
For more control you can implement the grid's view's "OnCustomDrawCell" event and set the colors there.
procedure TForm1.cxGrid1DBTableView1CustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
if(AViewInfo.Selected) and (Screen.ActiveControl = Sender.Site) then begin
ACanvas.Brush.Color := clGreen;
ACanvas.Font.Color := clFuchsia;
end;
end;

Resources