TTreeView - Change Expand and Collapse Image? - delphi

Is it possible using the standard TTreeView to change the Expand and Collapse Image?
I don't mean Node images, I mean the little arrows next to Nodes that have children, like so:
Ideally I would like the arrows to show as + and - Symbols, like the Delphi component structure tree:
If it is possible to change this, how would I go about doing it?
Working Demo based on David's Answer
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, Themes, uxTheme;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TMyTreeView = class(TTreeView)
protected
procedure CreateWnd; override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TMyTreeView }
procedure TMyTreeView.CreateWnd;
begin
inherited;
if ThemeServices.Enabled and CheckWin32Version(6, 0) then
SetWindowTheme(Handle, nil, nil);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
MyTree: TMyTreeView;
Node: TTreeNode;
begin
MyTree := TMyTreeView.Create(nil);
with MyTree do
begin
Parent := Self;
Height := 100;
Width := 100;
Left := 30;
Top := 30;
Node := Items.Add(nil, 'Item');
Items.AddChild(Node, 'Item');
Node := Items.AddChild(Node, 'Item');
Items.AddChild(Node, 'Item');
end;
end;
end.
The Result:

Tree views in post-Vista Windows have two alternative themes. The theme that you are wanting to avoid is known as the explorer theme. You want to use the standard theme. A control has to opt-in to get the explorer theme. It does so via the SetWindowTheme API. The VCL tree view control calls this to opt-in. It does so at the end of its CreateWnd method.
You can revert to the standard theme by undoing the change like this:
type
TMyTreeView = class(TTreeView)
protected
procedure CreateWnd; override;
end;
procedure TMyTreeView.CreateWnd;
begin
inherited;
if StyleServices.Enabled and TOSVersion.Check(6) and StyleServices.IsSystemStyle then
SetWindowTheme(Handle, nil, nil);
end;
This code is written for XE2. If you have an earlier Delphi then I think you want it like this:
if ThemeServices.Enabled and CheckWin32Version(6, 0) then
SetWindowTheme(Handle, nil, nil);

I addition to Davids answer. Put the following code in some extra unit and add it in the uses after the ComCtrls unit. That way you can use the standard TTreeView and change the theme whenever you like. Or register it in your own package if you like.
type
TTreeView = class(ComCtrls.TTreeView)
private
procedure SetExplorerTheme(const Value: Boolean);
public
property ExplorerTheme: Boolean write SetExplorerTheme;
end;
procedure TTreeView.SetExplorerTheme(const Value: Boolean);
begin
if ThemeServices.ThemesEnabled and CheckWin32Version(6, 0) then
if Value then
SetWindowTheme(Handle, 'Explorer', nil)
else
SetWindowTheme(Handle, nil, nil);
end;
In never Delphi versions you could also use a class helper to avoid the extra inheritance.

Related

How to correctly stream a TCollection property of a subcomponent, e.g. the Columns property of an embedded TDBGrid

I've been trying to boil down to an MCVE some code the author of another q sent me
to illustrate a problem with a custom component.
The component is simply a TPanel descendant which includes an embedded TDBGrid.
My version of its source, and a test project are below.
The problem is that if the embedded DBGrid has been created with persistent columns,
when its test project is re-opened in the IDE, an exception is raised
Error reading TColumn.Grid.Expanded. Property Griddoes not exist.
Executing the Stream method of the test project shows how this problem arises:
For comparison purposes, I also have a normal TDBGrid, DBGrid1, on my form. Whereas the Columns of this DBGrid1 are streamed as
Columns = <
item
Expanded = False
FieldName = 'ID'
Visible = True
end
[...]
the embedded grid's columns are streamed like this
Grid.Columns = <
item
Grid.Expanded = False
Grid.FieldName = 'ID'
Grid.Visible = True
end
[...]
It's obviously the Grid prefix of Grid.Expanded and the other column properties which is causing the problem.
I imagine that the problem is something to do with the fact that DBGridColumns
is a TCollection descendant and that the embedded grid isn't the top-level object in
the DFM.
My question is: How should the code of TMyPanel be modified so that the grid's
columns get correctly streamed?
Component source:
unit MAGridu;
interface
uses
Windows, SysUtils, Classes, Controls, ExtCtrls, DBGrids;
type
TMyPanel = class(TPanel)
private
FGrid : TDBGrid;
public
constructor Create(AOwner : TComponent); override;
published
property Grid : TDBGrid read FGrid;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Standard', [TMyPanel]);
end;
constructor TMyPanel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FGrid := TDBGrid.Create(Self);
FGrid.SetSubcomponent(True);
FGrid.Parent := Self;
end;
end.
Test project source:
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
CDS1: TClientDataSet;
DataSource1: TDataSource;
MyPanel1: TMyPanel;
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure Stream;
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Stream;
end;
procedure TForm1.Stream;
// This method is included as an easy way of getting at the contents of the project's
// DFM. It saves the form to a stream, and loads it into a memo on the form.
var
SS : TStringStream;
MS : TMemoryStream;
Writer : TWriter;
begin
SS := TStringStream.Create('');
MS := TMemoryStream.Create;
Writer := TWriter.Create(MS, 4096);
try
Writer.Root := Self;
Writer.WriteSignature;
Writer.WriteComponent(Self);
Writer.FlushBuffer;
MS.Position := 0;
ObjectBinaryToText(MS, SS);
Memo1.Lines.Text := SS.DataString;
finally
Writer.Free;
MS.Free;
SS.Free;
end;
end;
end.
procedure TForm1.FormCreate(Sender: TObject);
var
Field : TField;
begin
Field := TIntegerField.Create(Self);
Field.FieldName := 'ID';
Field.FieldKind := fkData;
Field.DataSet := CDS1;
Field := TStringField.Create(Self);
Field.FieldName := 'Name';
Field.Size := 20;
Field.FieldKind := fkData;
Field.DataSet := CDS1;
CDS1.CreateDataSet;
CDS1.InsertRecord([1, 'One']);
end;
end.
Seems there is not much you can do about it. When you look into procedure WriteCollectionProp (local to TWriter.WriteProperties) you see that FPropPath is cleared before the call to WriteCollection.
The problem with TDBGrid, or better TCustomDBGrid, is that the collection is marked as stored false and the streaming is delegated to DefineProperties, which uses TCustomDBGrid.WriteColumns to do the work.
Inspecting that method reveals that, although it also calls WriteCollection, the content of FPropPath is not cleared before. This is somewhat expected as FPropPath is a private field.
The reason why it nonetheless works in the standard use case is that at the moment of writing FPropPath is just empty.
As even Delphi 10.1 Berlin behaves the same as Delphi 7, I suggest filing a QP report together with just this example.
The solution would involve the embedded grid not having the form that owns the panel as the streaming root, but the panel itself. This will prevent the grid's properties being qualified by 'Grid', which, in effect, will eliminate column properties being wrongly qualified by the same. That is to say, the below is a workaround for faulty behavior.
To achieve the above, remove the SetSubComponent call,
constructor TMyPanel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FGrid := TDBGrid.Create(Self);
// FGrid.SetSubcomponent(True);
FGrid.Parent := Self;
end;
The csSubComponent style being removed, now the grid is not streamed at all.
Then override GetChildren for the panel to stream the grid through the panel. GetChildren, as documented, is used to determine which child controls are saved (streamed) of a control. Since we have only one control (the grid) we don't need to make a distinction and instead can call the inherited handler modifying the root.
type
TMyPanel = class(TPanel)
private
FGrid : TDBGrid;
public
constructor Create(AOwner : TComponent); override;
procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
published
property Grid : TDBGrid read FGrid;
end;
...
procedure TMyPanel.GetChildren(Proc: TGetChildProc; Root: TComponent);
begin
inherited GetChildren(Proc, Self);
end;
Then remains resolving subcomponent complications. Complication here was a second grid being created sitting in front of the panel which assumes streamed properties. Very much like in this unanswered question. Note that this problem is not related to the solution provided above. The original code displays the same problem.
Having read the question mentioned above, and this one, and this one, and this one, and still not being able to resolve with the help of the code, clues, advices in them, I traced the streaming system and came up with my solution as below.
I'm not claiming it is how it is supposed to be. It is just how I could make this to work. Main modifications are, the sub-grid is now writable (which would require a setter in production code), the conditional creation of the grid, and the overriden GetChildOwner of the panel. Below is the entire unit having TMyPanel2 (TMyPanel couldn't make it... ).
unit TestPanel2;
interface
uses
Windows, SysUtils, Classes, Controls, ExtCtrls, DBGrids;
type
TMyPanel2 = class(TPanel)
private
FGrid : TDBGrid;
protected
function GetChildOwner: TComponent; override;
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
published
property Grid : TDBGrid read FGrid write FGrid;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Test', [TMyPanel2]);
end;
constructor TMyPanel2.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
if not (csReading in AOwner.ComponentState) then begin
FGrid := TDBGrid.Create(Self);
FGrid.Name := 'InternalDBGrid';
FGrid.Parent := Self;
end else
RegisterClass(TDBGrid);
end;
destructor TMyPanel2.Destroy;
begin
FGrid.Free;
inherited;
end;
function TMyPanel2.GetChildOwner: TComponent;
begin
Result := Self;
end;
procedure TMyPanel2.GetChildren(Proc: TGetChildProc; Root: TComponent);
begin
Proc(Grid);
end;
end.

How to get a tree view item by the hit test when RTL layout is used?

Description:
Having a tree view in right-to-left reading mode (RTL), how to get node that was clicked knowing just the click coordinates ? Here is an interposed class, that makes the tree view to use the RTL display and that contains a click handler in which you can see the problem:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, CommCtrl;
type
TTreeView = class(ComCtrls.TTreeView)
protected
procedure CNNotify(var Msg: TWMNotify); message CN_NOTIFY;
procedure CreateParams(var Params: TCreateParams); override;
end;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TTreeView }
procedure TTreeView.CNNotify(var Msg: TWMNotify);
var
Node: TTreeNode;
Point: TPoint;
begin
inherited;
if Msg.NMHdr.code = NM_CLICK then
begin
Point := ScreenToClient(Mouse.CursorPos);
Node := GetNodeAt(Point.X, Point.Y);
if Assigned(Node) then
ShowMessage('This message never shows...');
end;
end;
procedure TTreeView.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or TVS_RTLREADING;
Params.ExStyle := Params.ExStyle or WS_EX_LAYOUTRTL or WS_EX_RIGHT;
end;
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
var
Node: TTreeNode;
begin
Node := TreeView1.Items.AddChild(nil, 'Item 1');
TreeView1.Items.AddChild(Node, 'SubItem 1');
end;
end.
The problem with this code (or better to say with such tree view in RTL mode) is, that when you click the node (or wherever), the GetNodeAt method never returns a valid node (always nil). For those, who don't have Delphi, the GetNodeAt method internally calls the TreeView_HitTest macro which when the tree view is in RTL mode, returns NULL like there won't be any item. I am passing to that macro the coordinates obtained through the GetCursorPos function calculated relatively to the control by the ScreenToClient function.
Question:
My question is, how to get the clicked node knowing just the mouse coordinates ? How to make a hit test with the tree view in RTL mode ? Should I for instance calculate the mouse horizontal position from right, and if so, how ?
From ScreenToClient documentation:
Do not use ScreenToClient when in a mirroring situation, that is, when
changing from left-to-right layout to right-to-left layout. Instead,
use MapWindowPoints. For more information, see "Window Layout and
Mirroring" in Window Features.
The corrected code could be like:
..
Point := Mouse.CursorPos;
MapWindowPoints(0, Handle, Point, 1);
Node := GetNodeAt(Point.X, Point.Y);
..
Also see: Window Layout and Mirroring

Delphi throbber

What is the best solution to show that the application is doing something?
I tried showing a progress indicator, but it did not work.
UPDATE: -------------
A progress bar works fine, but isn't what I want.
I want to show a throbber, like what Web browsers use, so as long as something is being updated it keeps turning.
Cursor can also be in crHourGlass mode.
Try this:
AnimateUnit
unit AnimateUnit;
interface
uses
Windows, Classes;
type
TFrameProc = procedure(const theFrame: ShortInt) of object;
TFrameThread = class(TThread)
private
{ Private declarations }
FFrameProc: TFrameProc;
FFrameValue: ShortInt;
procedure SynchedFrame();
protected
{ Protected declarations }
procedure Frame(const theFrame: ShortInt); virtual;
public
{ Public declarations }
constructor Create(theFrameProc: TFrameProc; CreateSuspended: Boolean = False); reintroduce; virtual;
end;
TAnimateThread = class(TFrameThread)
private
{ Private declarations }
protected
{ Protected declarations }
procedure Execute(); override;
public
{ Public declarations }
end;
var
AnimateThread: TAnimateThread;
implementation
{ TFrameThread }
constructor TFrameThread.Create(theFrameProc: TFrameProc; CreateSuspended: Boolean = False);
begin
inherited Create(CreateSuspended);
FreeOnTerminate := True;
FFrameProc := theFrameProc;
end;
procedure TFrameThread.SynchedFrame();
begin
if Assigned(FFrameProc) then FFrameProc(FFrameValue);
end;
procedure TFrameThread.Frame(const theFrame: ShortInt);
begin
FFrameValue := theFrame;
try
Sleep(0);
finally
Synchronize(SynchedFrame);
end;
end;
{ TAnimateThread }
procedure TAnimateThread.Execute();
var
I: ShortInt;
begin
while (not Self.Terminated) do
begin
Frame(0);
for I := 1 to 8 do
begin
if (not Self.Terminated) then
begin
Sleep(120);
Frame(I);
end;
end;
Frame(0);
end;
end;
end.
Unit1
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ImgList;
type
TForm1 = class(TForm)
ImageList1: TImageList;
Image1: TImage;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure UpdateFrame(const theFrame: ShortInt);
end;
var
Form1: TForm1;
implementation
uses
AnimateUnit;
{$R *.DFM}
procedure TForm1.UpdateFrame(const theFrame: ShortInt);
begin
Image1.Picture.Bitmap.Handle := 0;
try
ImageList1.GetBitmap(theFrame, Image1.Picture.Bitmap);
finally
Image1.Update();
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
AnimateThread := TAnimateThread.Create(UpdateFrame);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
AnimateThread.Terminate();
end;
end.
The Images
You are probably running your time consuming task in the main thread.
One option is to move it to a background thread which will allow your message queue to be serviced. You need it to be serviced in order for your progress bar, and indeed any UI, to work.
Answer to the updated question:
generate an animated gif e.g. here
add a GIF library to your environment (JEDI JVCL+JCL)
insert a TImage and load the generated gif
make it visible if you need it
A indicator is OK. You have to call Application.ProcessMessages after changing it.
"What is the best solution to show that that application is doing something?" - set mouse cursor to crHourGlass? or to create another form/frame/etc which attentions the user that the application is 'doing' something, and he needs to wait.
From your lengthy task, you can occasionally update a visual indicator, like a progress bar or anything else. However, you need to redraw the changes immediately by calling Update on the control that provides the feedback.
Don't use Application.ProcessMessages as this will introduce possible reentrancy issues.

How to impliment a stringlist property in a custom delphi component?

I am creating my first custom Delphi component. Its basically a custom Tpanel with header and lines text displayed on it.
I want to be able to add multiple lines text using a stringlist.
When testing the component I cannot get the text lines to display on the panel when adding lines: NewLinesText.add('line1 text')
It does however work when I create and populate a new stringlist at runtime and then assign it to my control : controlPanelitem.NewLinesText = MyNewStringlist
I want to be able to add lines like this: NewLinesText.add('line1 text')
I am using Delphi 7 professional on WinXP. See code below.
Any help would be appreciated!
unit ControlPanelItem;
interface
uses
SysUtils, Classes, Controls, ExtCtrls, Graphics, AdvPanel, StdCtrls,
Windows,Forms,Dialogs;
type
tControlPanelItem = class(TAdvPanel)
private
fLinesText : TStrings;
procedure SetLinesText(const Value: TStrings);
procedure SetText;
protected
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
published
property NewLinesText : TStrings read FLinesText write SetLinesText;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [tControlPanelItem]);
end;
constructor tControlPanelItem.Create(AOwner: TComponent);
begin
inherited;
fLinesText := TStringList.Create;
end;
destructor tControlPanelItem.Destroy;
begin
fLinesText.Free;
inherited;
end;
procedure tControlPanelItem.SetLinesText(const Value: TStrings);
begin
fLinesText.Assign(value);
SetText;
end;
procedure tControlPanelItem.SetText;
var
count : Integer;
begin
for count := 0 to fLinesText.Count - 1 do
ShowMessage(fLinesText.strings[count]);
end;
end.
You should do
procedure SetLines(Lines: TStrings);
begin
FLinesText.Assign(Lines);
// Repaint, update or whatever you need to do.
end;
You may also need to set the OnChange property of the FLines (do this in the constructor of your custom control, as soon as you have created it). Set it to any TNofifyEvent-compatible (private or protected, I guess) procedure of your component. In this procedure, you can do the repainting, updating etc. you need.
That is, do
constructor TControlPanelItem.Create(AOwner: TComponent);
begin
inherited;
FLinesText := TStringList.Create;
TStringList(FLinesText).OnChange := LinesChanged;
end;
procedure TControlPanelItem.LinesChanged(Sender: TObject);
begin
// Repaint, update or whatever you need to do.
end;

How to create an array of controls?

I have to create an array and place all controls there in order to access them.Here's a short example:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
const Test:Array[0..2] of TButton = (Button1,Button2,Button3);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
end.
Undeclarated idenitifier 'Button1' at the line where I declarated my array.But it's declarated three lines above.
Where's the problem,how to put all controls in an array?
EDIT:
Thank you for your answers,but I've got problems:
var TestA:TObjectList<TButton>;
var index:TComponent;
begin
TestA := TObjectList<TButton>.Create(false);
for index in Form7 do
if pos(index.name, 'Button') = 1 then
TestA.add(TButton(index));
TestA[0].Caption := 'Test'; //Exception out of range.
Ben's right. You can't set up a control array in the form designer. But if you have 110 images, for this specific case you can put them into a TImageList component and treat its collection of images as an array.
If you've got a bunch of more normal controls, like buttons, you'll have to create an array and load them into it in code. There are two ways to do this. The simple way, for small arrays at least, is Ben's answer. For large control sets, or ones that change frequently, (where your design is not finished, for example,) as long as you make sure to give them all serial names (Button1, Button2, Button3...), you can try something like this:
var
index: TComponent;
list: TObjectList;
begin
list := TObjectList.Create(false); //DO NOT take ownership
for index in frmMyForm do
if pos('Button', index.name) = 1 then
list.add(index);
//do more stuff once the list is built
end;
(Use a TObjectList<TComponent>, or something even more specific, if you're using D2009.) Build the list, based on the code above, then write a sorting function callback that will sort them based on name and use it to sort the list, and you've got your "array."
You may not be able to reference public properties of your form in an array constant like that. Try doing it in your form constructor/OnCreate event instead.
procedure TForm1.FormCreate(Sender: TObject);
begin
Test[0] := Button1;
Test[1] := Button2;
Test[2] := Button3;
end;
This function will iterate over all the controls on a specified container, like a particular TPanel or even the entire form, and populate a specified TObjectList with your TImage controls.
procedure TForm1.AddImageControlsToList(AParent: TWinControl; AList: TObjectList; Recursive: boolean);
var
Index: integer;
AChild: TControl;
begin
for Index := 0 to AParent.ControlCount - 1 do
begin
AChild := AParent.Controls[Index];
if AChild is TImage then // Or whatever test you want to use
AList.Add(AChild)
else if Recursive and (AChild is TWinControl) then
AddImageControlsToList(TWinControl(AChild), AList, True);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
// Call like this or similar to get your list of images
// (assumes MyImageList is declared in Form)
MyImageList := TObjectList.Create(False);
AddImageControlsToList(Self, MyImageList, True);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
// Destroy the list
FreeAndNil(MyImageList);
end;
How about this?
procedure TForm1.FormCreate(Sender: TObject);
begin
for b := 1 to 110 do
Test[b] := FindComponent('Button' + IntToStr(b)) as TButton;
end;
You'll have to declare the array as a variable rather than a constant and it will have to go from 1 to 110 rather than 0 to 109 but that's no problem.
I use this all the time - it is simple and fast (despite Mr Wheeler's comment)- declare the maxbuttons as a constant
var
Form1: TForm1;
pbutton:array[1..maxbuttons] of ^tbutton;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
(* Exit *)
var k:integer;
begin
for k:=1 to maxbuttons do dispose(pbutton[k]);
close;
end;
procedure TForm1.FormActivate(Sender: TObject);
var k:integer;
begin
(*note the buttons must be Button1, Button2 etc in sequence or you need to
allocate them manually eg pbutton[1]^:=exitbtn etc *)
for k:=1 to maxbuttons do
begin
new(pbutton[k]);
pbutton[k]^:= tbutton(FindComponent('Button'+IntToStr(k)));
end;
end;
procedure TForm1.ButtonMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var k:integer;
b:boolean;
begin
b:=false;
k:=1;
while (k<= maxbuttons) and (not b) do
begin
if pbutton[k]^ = sender then (Note sender indicates which button has been clicked)
begin
{ found it so do something}
b:=true;
end;
k:=k+1;
end;
end;
Try this
var
TestA:TObjectList;
index:TComponent;
begin
TestA := TObjectList<TButton>.Create(false);
try
for index in Form7 do
if (pos is TButton) OR {or/and} (pos.tag and 8=8) then
TestA.add(TButton(index));
if TestA.Count>0 then //Fix:Exception out of range.
TestA[0].Caption := 'Test';
finally
TestA.Free;
end;
end;

Resources