C++ Builder TStringGrid row colors - c++builder

The TStringGrid component I'm using in a C++ Builder project seems to alternate row colors by defaults. Even rows have a gray background while odd rows have a white background. How can I disable this alternate coloring so that all rows have the same background color?
Here's what it looks like:
And here is the corresponding entry in the DFM:
object StringGrid1: TStringGrid [0]
Left = 0
Top = 0
Width = 744
Height = 300
Align = alClient
FixedCols = 0
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
Options = [goFixedHorzLine, goVertLine, goHorzLine, goColSizing, goRowSelect]
ParentFont = False
TabOrder = 0
OnSelectCell = StringGrid1SelectCell
ColWidths = (
64
64
64
64
64)
end

I don't know which version of VCL you are using, but back in time, when I had been using great and very useful TStringGrid component, I did exactly the same thing, related to custom coloring cells, as you want to do. It is change related to behavior, not defined as property, I think.
You need to:
overload TStringGrid's virtual function for drawing cells in your particular derived class from TStringGrid
or,
on your TForm instance containing TStringGrid you want to modify, change behaviour of that particular TStringGrid.
So, basically you have choice, to make new class derived from TStringGrid and change behavior by overriding function for cell drawing, or to change existing, particular instance of TStringGrid by overriding just that grid's behavior with a help of great ObjectInspector, if it is still called like that.
It is better of course, to make new class and derive it from TStringGrid, and use it on Form(Parent container) directly which you can reuse later, whenever you want,
but it contains some additional work. Maybe, as start point, it is better to change behavior of particular TStringGrid instance you already dropped on ParetContainer(probably TForm) with using object inspector. If these directions are not enough for you, I can place here, some code samples that will be more helpful for you.

You should use event OnDrawCell. Here is working example:
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
if( ( ARow % 2 ) == 0 )
{
if( ARow == 0 )
StringGrid1->Canvas->Brush->Color = clBlue;
else
StringGrid1->Canvas->Brush->Color = clGray;
StringGrid1->Canvas->FillRect( Rect );
}
}

Related

Why the child panel do not assume its color?

Now I am struggling to set the color of a child panel placed on another one.
The goal is to represent fractions of time with certain properties over the whole period, represented by a TPanel on the background(Lime Green).
I do not understand, the code I am using has already worked, but now the child panel keeps the same color of the parent panel.
int IntSize = SecondsBetween( CurrInterrup->TerminoInt, CurrInterrup->InicioInt );
int dura = SecondsBetween( Indisp->DtHoraFim, Indisp->DtHoraIni);
int left = SecondsBetween( Indisp->DtHoraIni, CurrInterrup->InicioInt);
int width = RoundTo(((double)dura /(double) IntSize) *(double) PnGreen->Width, 0);
left = RoundTo(((double)left/(double)IntSize) * (double)PnGreen->Width, 0);
TPanel *pn = new TPanel(this);
pn->ParentColor = false;
pn->BorderStyle = bsSingle;
pn->BevelKind = bkSoft;
pn->Color = clRed;
pn->Left = left;
pn->Width = width;
pn->Height = PnGreen->Height -5;
pn->Parent = PnGreen; // A Panel instantiated at design time of color clLime
pn->Update();
The panel "pn" is shown with the correct properties of left, height and width and position, but the color is clLime instead of clRed.
Is there any error on the code?
Thank you very much.
Kind Regards.
Try setting the panel's ParentBackground property to false.
If ParentBackground is True, the control uses the parent's theme background to draw its own background.
If ParentBackground is False, the control uses its own properties, such as Color, to draw its background.

Delphi style that doesn't look old

I'm creating a VCL application and I want to style it exactly like Delphi itself is styled. For example, the tree view nodes when selected render really nicely (full width blue bar), see below
Yet mine looks like it's from Windows 98:
I'm hoping there's an easy way to get this to look nicer. Would love any ideas or pointers in the right direction. I've never done any custom/owner drawing before so this is new to me.
For a starter, you could just experiment a little with the available standard property settings. The following is the looks of the standard TTreeView on Windows 10, with just a few properties changed.
From the TextView of the form:
object TreeView1: TTreeView
Left = 8
Top = 8
Width = 225
Height = 283
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'Tahoma'
Font.Style = []
HideSelection = False
HotTrack = True
Indent = 19
ParentFont = False
RowSelect = True
ShowLines = False
TabOrder = 0
end
Note the Hottrack = true, Rowselect = true and Showlines = False

TListView - How to control the rect item size in Viewstyle = vsIcon?

I need to draw custom 120 x 120 thmbnails on a TListView in Viewstyle = vsIcon. without using LargeImages (I will paint on "OnCustomDrawItem").
The only way I could make this work is to specify a "dummy" TImageList for LargeImages with width/heigh = 120.
otherwise the item rect is undefined (also with OwnerDraw=True)
How to do this?

Delphi TToolBar Superfluous Vertical Bar in tbsSeparator [duplicate]

This question already has answers here:
Can Delphi themed toolbars have dividers that are centred between their tool buttons?
(3 answers)
Closed 8 years ago.
I'm using Delphi XE6 on top of Windows 2012 R2, and trying to create applications with toolbars. I'm however experiencing problems with the compiled application having a vertical superfluous bar at the left hand side of any Separators I might add.
This problem is described in both This Question and This Other Question, however I am not trying to use any other fancy controls - I am solely using the "New Button" and "New Separator" to add items. I have nonetheless tried the suggested fixes to no avail.
My sample application has been created as follows:
New VCL Forms Application
Add TToolBar
Select ToolBar1
Right Click, Select "New Button"
Right Click, Select "New Button"
Right Click, Select "New Separator"
Right Click, Select "New Button"
Run
This is what the application looks like
I have tried changing every potentially applicable property of the TToolBar itself (including setting Transparent to False), as well as the ToolBar3 (which is set to tbsSeparator), but I cannot seem to get rid of this vertical bar. I have also tried to add a TXPManifest to the form, but this made no difference.
The only thing that does seem to make a difference is if I set the Separator Visible property to False - the line disappears, but so does the Separator, so this isn't any help.
The issue I'm experiencing is also described in This thread, and I can assure the readers that I've not made any changes to the width of the Separator - it's all default.
Does anybody have any suggestions on how I can fix this?
For what it's worth - my Delphi Form is like this - you can see that I'm not using any fancy controls other than the supported ones:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 112
ClientWidth = 382
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object ToolBar1: TToolBar
Left = 0
Top = 0
Width = 382
Height = 29
Caption = 'ToolBar1'
TabOrder = 0
Transparent = False
object ToolButton1: TToolButton
Left = 0
Top = 0
Caption = 'ToolButton1'
ImageIndex = 0
end
object ToolButton2: TToolButton
Left = 23
Top = 0
Caption = 'ToolButton2'
ImageIndex = 1
end
object ToolButton3: TToolButton
Left = 46
Top = 0
Width = 8
Caption = 'ToolButton3'
ImageIndex = 2
Style = tbsSeparator
end
object ToolButton4: TToolButton
Left = 54
Top = 0
Caption = 'ToolButton4'
ImageIndex = 3
end
end
object XPManifest1: TXPManifest
Left = 40
Top = 48
end
end
As the TToolbar is a wrapper of Microsoft Windows Rebar32 Control, it looks exactly the same as a normal Windows application does. Why not remove these separators, so that you will not see them anymore.

List view control drawn incorrectly when groups are present

I was experimenting with list view groups, and it turns out the control displays incorrectly when the application starts.
As soon as I resize the form it displays correctly:
I do not understand what is happening. Can anyone explain?
It's a bug in the Delphi control which I have reproduced. I'm not sure yet what causes the bug. I have submitted this to Quality Central as QC#101104.
I found a simple workaround by adding the following code to the form's OnShow event.
ListView1.Align := alNone;
ListView1.Align := alClient;
The following DFM file is enough to demonstrate the problem:
object MyForm: TMyForm
Left = 0
Top = 0
ClientHeight = 300
ClientWidth = 635
object ListView1: TListView
Left = 200
Top = 96
Width = 250
Height = 150
Align = alClient
Columns = <
item
Caption = 'Column'
end>
Groups = <
item
Header = 'Group header'
GroupID = 0
end>
Items.ItemData = {
052A0000000100000000000000FFFFFFFFFFFFFFFF0000000000000000000000
000854006800650020006900740065006D00}
GroupView = True
ViewStyle = vsReport
end
end
It turns out that another way to resolve the problem is to move the ViewStyle entry in the DFM file so that it appears before the Items entry. So yet another workaround for the problem would be to add the items at runtime. In fact this probably explains why this bug has not been found since I bet that the overwhelming majority of list view code adds the items at runtime.

Resources