• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1341
  • Last Modified:

TreeView with checkboxes had draw problem

Hello,

I use the standard D7 TreeView and if I enable checkboxes

procedure TMyCustomTreeView.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);

    Params.Style := Params.Style or TVS_CHECKBOXES;
end;


a HScrollbar is shown, but the Treeview has no nodes!

If I add the first node the behavoir is normal and HScrollbar is only shown if is it neccesary.

How can I have a normal behavoir from the begin?

regards

William
0
SirWinston8
Asked:
SirWinston8
  • 4
  • 3
1 Solution
 
JaccoCommented:
Here is how I got it working.

Regards Jacco

uses
  CommCtrl;

procedure SetComCtlStyle(Ctl: TWinControl; Value: Integer; UseStyle: Boolean);
var
  Style: Integer;
begin
  if Ctl.HandleAllocated then
  begin
    Style := GetWindowLong(Ctl.Handle, GWL_STYLE);
    if not UseStyle then Style := Style and not Value
    else Style := Style or Value;
    SetWindowLong(Ctl.Handle, GWL_STYLE, Style);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetComCtlStyle(Treeview1, TVS_CHECKBOXES, True);  
end;
0
 
SirWinston8Author Commented:
Good, one step closer.

But how can I refresh the window if I had nodes in it and toogle the style in runtime?

Regards William
0
 
JaccoCommented:
Add a call to the protected method RecreateWnd after disabling the checkboxes.

You can call the protected method like this:

type
  TMyTreeView = class(TTreeView);

procedure TForm1.Button3Click(Sender: TObject);
begin
  SetComCtlStyle(Treeview1, TVS_CHECKBOXES, False);
  TMyTreeView(Treeview1).RecreateWnd;
end;

When enabling/disabling you will loose the checks that are set. Look at the source of TListView to see how Delphi saves their state.

Regards Jacco
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SirWinston8Author Commented:
Hi,

your solution does work on a form, but not inside a derived class.

The problem with the HScrollbar appears also if you insert a node:

 - insert a node with too long text put a HScrollbar in the window, this is OK!
 - edit the same node to a shorter text will NOT remove the HScrollbar!!!!!!

 - after you insert a new node the HScrollBar will remove!?!?!

Do you have an explanition for this?

Regards William
0
 
SirWinston8Author Commented:
I forget!

it seems to me that TTreeview-class does not correct syncronize with the underlaying Windows TreeView?!?!?!
0
 
SirWinston8Author Commented:
Hi,

I have done any tests.

Following forces, after edit any node, the TreeView to adjust it's ScrollBars.
It works!

procedure TMyCustomTreeView.Edit(const Item: TTVItem);
begin
  inherited Edit(Item);

  if HandleAllocated then
  begin
    Style := GetWindowLong(Handle, GWL_STYLE);
    Style := Style or TVS_NOSCROLL;
    SetWindowLong(Handle, GWL_STYLE, Style);
    Style := Style and not TVS_NOSCROLL;
    SetWindowLong(Handle, GWL_STYLE, Style);
  end;
end;


The same code in this procedure or CreateHandle or CreateWnd does NOTHING!

procedure TMyCustomTreeView.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);

  if FShowCheckBoxes then
    Params.Style := Params.Style or TVS_CHECKBOXES
  else
    Params.Style := Params.Style and not TVS_CHECKBOXES;

  if HandleAllocated then
  begin
    Style := GetWindowLong(Handle, GWL_STYLE);
    Style := Style or TVS_NOSCROLL;
    SetWindowLong(Handle, GWL_STYLE, Style);
    Style := Style and not TVS_NOSCROLL;
    SetWindowLong(Handle, GWL_STYLE, Style);
  end;

end;

Do you have any idea?

Regards William

0
 
JaccoCommented:
CreateParams is only call once when creating the control (and when a RecreateWnd is issued). Edit is called every time a label is edited.

Can't figure out why the scrollbar stays and when it disappears...

Regards Jacco
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now