Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6117587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:20:15+00:00 2026-05-23T15:20:15+00:00

I have a Tree View as a main menu. After program start, I add

  • 0

I have a Tree View as a main menu. After program start, I add new sub items.

Then I do TreeView1.Perform(TVM_SETITEMHEIGHT, 28, 0);

At designtime I set the Form’s Position property to poDesigned. Why does TreeView1.Perform not work if I do Position := poScreenCenter; in runtime?

This is my code:

procedure TForm1.FormCreate(Sender: TObject);
begin
  TreeView1.Items.AddChild(TreeView1.Items.Item[0],'Sub Menu1');
  TreeView1.Items.AddChild(TreeView1.Items.Item[0],'Sub Menu2');
  TreeView1.Items.AddChild(TreeView1.Items.Item[0],'Sub Menu3');
  TreeView1.Items.AddChild(TreeView1.Items.Item[0],'Sub Menu4');
  TreeView1.Perform(TVM_SETITEMHEIGHT, 28, 0);
  Position:=poScreenCenter;
end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:20:17+00:00Added an answer on May 23, 2026 at 3:20 pm

    The issue is that setting the Position property of the Form results in call to RecreateWnd. RecreateWnd means destroying the windows screen object and building it up from scratch. It seems this is needed (or te most easiest way) to fully implement all the effects of changing this property. Recreation of window handles is not uncommon: e.g. changing the BorderStyle of a form, or even that of an Edit control results in a call to RecreateWnd.

    RecreateWnd cascades into recreating all child windowed controls, also your TreeView. Normally, a component knows from its internals (properties, private data) how to recreate itself. For instance: a TreeView saves its nodes to a temporary memory stream prior to handle destruction, and loads it back in after handle recreation.

    So, who is to blame: the Position property of the Form, the TreeView, or neither? In the absence of a ItemHeight property for the TreeView, you are forced to manually send a WinAPI message. And that is a modification to the control which is not registered by the VCL. So far the explanation as why this happens.

    The best solution is to be sure your customization is done every time the TreeView is recreated. Unfortunately there is no event available for that. You would need to override CreateWnd (see update below). But when you leave the Ctl3D and BorderStyle properties intact, it is also possible to control this at the parent’s level. I came down to overriding CM_ShowingChanged, because the TreeView unfortunately isn’t yet completely rebuild after TForm1.CreateWnd:

    TForm1 = class(TForm)
      TreeView1: TTreeView;
      ...
    private
      procedure CMShowingChanged(var Message: TMessage);
        message CM_SHOWINGCHANGED;
    end;
    
    procedure TForm1.CMShowingChanged(var Message: TMessage);
    begin
      inherited;
      TreeView1.Perform(TVM_SETITEMHEIGHT, 28, 0);
    end;
    

    Update:

    As requested in below comment, here is the solution with overriden TTreeView.CreateWnd:

    unit Unit1;
    
    interface
    
    uses
      Windows, Classes, Controls, Forms, StdCtrls, ComCtrls, CommCtrl, XPMan;
    
    type
      TTreeView = class(ComCtrls.TTreeView)
      protected
        procedure CreateWnd; override;
      end;
    
      TForm1 = class(TForm)
        TreeView1: TTreeView;
        XPManifest1: TXPManifest;
        procedure FormCreate(Sender: TObject);
      end;
    
    implementation
    
    {$R *.dfm}
    
    { TTreeView }
    
    procedure TTreeView.CreateWnd;
    begin
      inherited CreateWnd;
      Perform(TVM_SETITEMHEIGHT, 38, 0);
    end;
    
    { TForm1 }
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      TreeView1.Items.AddChild(TreeView1.Items.Item[0],'Sub Menu1');
      TreeView1.Items.AddChild(TreeView1.Items.Item[0],'Sub Menu2');
      TreeView1.Items.AddChild(TreeView1.Items.Item[0],'Sub Menu3');
      TreeView1.Items.AddChild(TreeView1.Items.Item[0],'Sub Menu4');
      Position := poScreenCenter;
    end;
    
    end.
    

    And if you don’t like to subclass TTreeView, then override CreateWnd of the form, but in that case you need to call HandleNeeded as answered by Andreas Rejbrand:

    unit Unit1;
    
    interface
    
    uses
      Windows, Classes, Controls, Forms, StdCtrls, ComCtrls, CommCtrl, XPMan;
    
    type
      TForm1 = class(TForm)
        TreeView1: TTreeView;
        XPManifest1: TXPManifest;
        procedure FormCreate(Sender: TObject);
      protected
        procedure CreateWnd; override;
      end;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      TreeView1.Items.AddChild(TreeView1.Items.Item[0],'Sub Menu1');
      TreeView1.Items.AddChild(TreeView1.Items.Item[0],'Sub Menu2');
      TreeView1.Items.AddChild(TreeView1.Items.Item[0],'Sub Menu3');
      TreeView1.Items.AddChild(TreeView1.Items.Item[0],'Sub Menu4');
      Position := poScreenCenter;
    end;
    
    procedure TForm1.CreateWnd;
    begin
      inherited CreateWnd;
      TreeView1.HandleNeeded;
      TreeView1.Perform(TVM_SETITEMHEIGHT, 38, 0);
    end;
    
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tree view <asp:TreeView ID=TreeView1 runat=server DataSourceID=SiteMapDataSource1 ShowExpandCollapse=False> </asp:TreeView> As you can
I am new to jaspersoft reports .Is it possible to have dynamic tree view
I have this TreeView: Main Node Header=Main Sub Node Header=Sub1 Final Node Header=Item1 Final
I have a WinForms TreeView with one main node and several sub-nodes. How can
I have a tree view defined as follows: <HierarchicalDataTemplate x:Key=ChildTemplate ItemsSource={Binding Children}> <TextBlock Text={Binding
I have a hypothetical tree view that contains this data: RootNode Leaf vein SecondRoot
I have a web application where I am using asp.net tree view control to
I am having a tree-view on my main form with initially some nodes as
I have created an extension that adds a new column to the main message
I'm new to iPhone development and am having problems removing a sub-view from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.