IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants VCL Delphi Discussion :

TTreeview activer un node


Sujet :

Composants VCL Delphi

  1. #1
    Membre émérite

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2007
    Messages
    3 387
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 62
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2007
    Messages : 3 387
    Points : 2 999
    Points
    2 999
    Par défaut TTreeview activer un node
    Bonjour

    J'essaie toutes les solutions possibles mais rien à faire.
    J'ai un treeview dans lequel je fais une recherche pour trouver un node contenant un texte précis.
    Je veux qu'il soit sélectionné et pas seulement visible mais "highlited" (cette propriété n'existe pas).
    Les solutions selected, focused, makevisible, treeview.setfocus, treeview.repaint, etc. ne fonctionnent pas.

    Finalement, il y a une solution ou pas ?

  2. #2
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 042
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 042
    Points : 40 952
    Points
    40 952
    Billets dans le blog
    62
    Par défaut
    Bonjour,

    Cela fait un bail que je n'ai pas touché à des TTreeview. Il a fallu que je "déterre" des programmes D3 (toujours utilisés en production donc, ça date du siècle dernier ou presque)

    pour "activer" le node je fais par exemple pour sélectionner le noeud suivant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    Anode : TTreeNode;
    begin
    Anode:=Treeview1.Selected.GetPrevVisible;
    if Anode<>nil then TreeView1.Selected:=Anode;
    end;
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

  3. #3
    Expert éminent sénior
    Avatar de ShaiLeTroll
    Homme Profil pro
    Développeur C++\Delphi
    Inscrit en
    Juillet 2006
    Messages
    13 455
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Développeur C++\Delphi
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2006
    Messages : 13 455
    Points : 24 867
    Points
    24 867
    Par défaut
    Vcl.ComCtrls.TCustomTreeView.Select avec un TShiftState particulier peut-il donner cet effet highlighted ? même si pour un TTreeNode j'ignore ce que signifie l'état highlight non présent dans Tree-View Control Item States

    Passer HideSelection à False.

    Il faudrait une image pour montrer à quoi ressemble un highlighted node selon toi.

    Si tu veux qu'un mot dans le text du noeud soit colorié, il faut le dessiner dans OnAdvancedCustomDrawItem ou OnCustomDrawItem (voir ce cas)
    Aide via F1 - FAQ - Guide du développeur Delphi devant un problème - Pensez-y !
    Attention Troll Méchant !
    "Quand un homme a faim, mieux vaut lui apprendre à pêcher que de lui donner un poisson" Confucius
    Mieux vaut se taire et paraître idiot, Que l'ouvrir et de le confirmer !
    L'ignorance n'excuse pas la médiocrité !

    L'expérience, c'est le nom que chacun donne à ses erreurs. (Oscar Wilde)
    Il faut avoir le courage de se tromper et d'apprendre de ses erreurs

  4. #4
    Membre émérite

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2007
    Messages
    3 387
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 62
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2007
    Messages : 3 387
    Points : 2 999
    Points
    2 999
    Par défaut
    Rien à faire quelle que soit la solution.

    Serge, je ne vois pas comment utiliser ta solution. Je trouve le node en faisant une recherche sur le texte. Pourquoi je devrais chercher le prevvisible ?

    Pour le TShiftState, j'en ai testé plusieurs mais rien à faire.

    Avec mon code, le node sélectionné se trouve en bas de l'affichage mais toujours pas sur fond bleu.
    Même des repaint, rien ne change.


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
     
    var
      Nde, Anode: TTreeNode;
      I:   Integer;
      B: Boolean;
    begin
      B := False;
      Cursor  := crHourGlass;
      Enabled := False;
      try
        Nde := Tv.Items.GetFirstNode.getFirstChild;
        while Assigned(Nde) do
        begin
          if Nde.Text.Contains('PROBLÈME') then
          begin
            Tv.SetFocus;
            B := True;
            Break;
          end;
          Nde := Nde.getNextSibling;
        end;
      finally
        Cursor  := crDefault;
        Enabled := True;
        if B then
        begin
          Nde.MakeVisible;
          Tv.Select(Nde, [ssShift]);
          Tv.Selected := Nde;
          Nde.Expanded := True;
        end;
      end;
    end;

  5. #5
    Membre émérite

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2007
    Messages
    3 387
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 62
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2007
    Messages : 3 387
    Points : 2 999
    Points
    2 999
    Par défaut
    le hideselection à false a un effet supplémentaire : un entourage avec une ligne très fine autour du node.

    Ce que j'appelle highlited, c'est avoir le node bien visible sur fond bleu comme lorsqu'on clique dessus.

  6. #6
    Membre émérite

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2007
    Messages
    3 387
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 62
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2007
    Messages : 3 387
    Points : 2 999
    Points
    2 999
    Par défaut
    Le OnCustomDraw fait le boulot finalement.
    Mais je trouve quand même bizarre de devoir en passer par là.

    Merci à vous deux.

  7. #7
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 042
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 042
    Points : 40 952
    Points
    40 952
    Billets dans le blog
    62
    Par défaut
    Citation Envoyé par Papy214 Voir le message
    Serge, je ne vois pas comment utiliser ta solution. Je trouve le node en faisant une recherche sur le texte. Pourquoi je devrais chercher le prevvisible ?
    ce n'était qu'un exemple, ne voulant pas écrire un code de recherche (j'en ai un mais je ne sais plus dans quelle unité) je me suis rabattu sur le code le plus simple

    j'aurais pu simplement écrire if Anode<>nil then TreeView1.Selected:=Anode;dans ton cas cela donnerait
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    ... 
         if Nde.Text.Contains('PROBLÈME') then
          begin
            Tv.Selected:=Nde;
            Tv.SetFocus;
            B := True;
            Break;
          end;
    ...
    reste que si tu veux que ce node soit en haut de liste (ou au milieu pourquoi pas) est un autre truc dont je ne me souviens plus (j'ai refermé D3)
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

  8. #8
    Membre émérite

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2007
    Messages
    3 387
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 62
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2007
    Messages : 3 387
    Points : 2 999
    Points
    2 999
    Par défaut
    Qu'il soit en haut de liste serait un plus mais ça ira bien comme ça.
    Je chercherai ça plus tard si c'est une demande formelle.

    Merci.

  9. #9
    Expert éminent sénior
    Avatar de ShaiLeTroll
    Homme Profil pro
    Développeur C++\Delphi
    Inscrit en
    Juillet 2006
    Messages
    13 455
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Développeur C++\Delphi
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2006
    Messages : 13 455
    Points : 24 867
    Points
    24 867
    Par défaut
    Etrange, je n'ai pas ce problème, Delphi 10 Seatle, Thème Windows, cela apparait en bleu ciel si je clique sur un Panel lié à PanelAllClick

    Déjà, ce break et finally, c'est pas du tout élégant.


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    unit TreeView_MainForm;
     
    interface
     
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.ExtCtrls;
     
    type
      TForm1 = class(TForm)
        TreeView1: TTreeView;
        Panel1: TPanel;
        Panel2: TPanel;
        Panel3: TPanel;
        Panel4: TPanel;
        Panel5: TPanel;
        Panel6: TPanel;
        Panel7: TPanel;
        Panel8: TPanel;
        procedure TreeView1DblClick(Sender: TObject);
        procedure PanelAllClick(Sender: TObject);
      private
        procedure ShowTreeControls(AControl: TWinControl; Node: TTreeNode);
        procedure FindNode(ATree: TTreeView; const AText: string); overload;
        function FindNode(ANode: TTreeNode; const AText: string): Boolean; overload;
      public
        { Déclarations publiques }
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    {$R *.dfm}
     
    procedure TForm1.FindNode(ATree: TTreeView; const AText: string);
    var
      Node: TTreeNode;
    begin
      Node := ATree.Items.GetFirstNode();
      while Assigned(Node) do
      begin
        if FindNode(Node, AText) then
          Exit;
     
        Node := Node.getNextSibling();
      end;
    end;
     
    function TForm1.FindNode(ANode: TTreeNode; const AText: string): Boolean;
    begin
      Result := ANode.Text.Contains(AText);
      if Result then
      begin
        ANode.MakeVisible();
        ANode.Selected := True;
      end
      else
      begin
        ANode := ANode.getFirstChild();
        while Assigned(ANode) do
        begin
          if FindNode(ANode, AText) then
            Exit;
     
          ANode := ANode.getNextSibling();
        end;
      end;
    end;
     
    procedure TForm1.PanelAllClick(Sender: TObject);
    begin
      FindNode(TreeView1, TControl(Sender).Name);
     
    end;
     
    procedure TForm1.ShowTreeControls(AControl: TWinControl; Node: TTreeNode);
    var
       I: Integer;
       ChildNode: TTreeNode;
    begin
      For I := 0 to AControl.ControlCount - 1 do
      begin
        ChildNode := TTreeView(Node.TreeView).Items.AddChild(Node, Format('%s: %s - Parent Node %d', [AControl.Controls[I].Name, AControl.Controls[I].ClassName, Node.AbsoluteIndex]));
        if AControl.Controls[I] is TWinControl then
          ShowTreeControls(AControl.Controls[I] as TWinControl, ChildNode);
      end;
    end;
     
    procedure TForm1.TreeView1DblClick(Sender: TObject);
    begin
      ShowTreeControls(Self, TreeView1.Items.Add(nil, Self.ClassName));
    end;
     
    end.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    object Form1: TForm1
      Left = 0
      Top = 0
      Caption = 'Form1'
      ClientHeight = 299
      ClientWidth = 635
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'Tahoma'
      Font.Style = []
      OldCreateOrder = False
      PixelsPerInch = 96
      TextHeight = 13
      object TreeView1: TTreeView
        Left = 8
        Top = 8
        Width = 426
        Height = 290
        Indent = 19
        TabOrder = 0
        OnDblClick = TreeView1DblClick
      end
      object Panel1: TPanel
        Left = 442
        Top = 8
        Width = 185
        Height = 290
        Caption = 'Panel1'
        TabOrder = 1
        OnClick = PanelAllClick
        object Panel2: TPanel
          Left = 10
          Top = 10
          Width = 168
          Height = 50
          Caption = 'Panel2'
          TabOrder = 0
          OnClick = PanelAllClick
          object Panel3: TPanel
            Left = 8
            Top = 4
            Width = 54
            Height = 41
            Caption = 'Panel3'
            TabOrder = 0
            OnClick = PanelAllClick
          end
          object Panel7: TPanel
            Left = 109
            Top = 4
            Width = 49
            Height = 41
            Caption = 'Panel7'
            TabOrder = 1
            OnClick = PanelAllClick
          end
        end
        object Panel4: TPanel
          Left = 15
          Top = 150
          Width = 161
          Height = 126
          Caption = 'Panel4'
          TabOrder = 1
          OnClick = PanelAllClick
          object Panel5: TPanel
            Left = 14
            Top = 5
            Width = 138
            Height = 41
            Caption = 'Panel5'
            TabOrder = 0
            OnClick = PanelAllClick
            object Panel8: TPanel
              Left = 89
              Top = 7
              Width = 43
              Height = 26
              Caption = 'Panel8'
              TabOrder = 0
              OnClick = PanelAllClick
            end
          end
          object Panel6: TPanel
            Left = 7
            Top = 75
            Width = 129
            Height = 40
            Caption = 'Panel6'
            TabOrder = 1
            OnClick = PanelAllClick
          end
        end
      end
    end
    Aide via F1 - FAQ - Guide du développeur Delphi devant un problème - Pensez-y !
    Attention Troll Méchant !
    "Quand un homme a faim, mieux vaut lui apprendre à pêcher que de lui donner un poisson" Confucius
    Mieux vaut se taire et paraître idiot, Que l'ouvrir et de le confirmer !
    L'ignorance n'excuse pas la médiocrité !

    L'expérience, c'est le nom que chacun donne à ses erreurs. (Oscar Wilde)
    Il faut avoir le courage de se tromper et d'apprendre de ses erreurs

  10. #10
    Membre émérite

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2007
    Messages
    3 387
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 62
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2007
    Messages : 3 387
    Points : 2 999
    Points
    2 999
    Par défaut
    alors à vrai dire, j'ai pas vraiment compris le pourquoi du comment.
    J'ai sorti la recherche du node de l'évènement et maintenant ça fonctionne.
    PAr contre j'ai viré mon try-finally et le Enabled := False.
    J'en arrive à me demander si ça ne venait pas de là.
    Maintenant ça fonctionne parfaitement sans onDrawItem.
    J'ai également viré le break, tu as raison

  11. #11
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 042
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 042
    Points : 40 952
    Points
    40 952
    Billets dans le blog
    62
    Par défaut
    Bonjour au cas où tu repasserais dans le coin.
    Citation Envoyé par Papy214 Voir le message
    Qu'il soit en haut de liste serait un plus mais ça ira bien comme ça.
    Aujourd'hui je voulais retrouver toutes les classes de TGraphicControl et du coup je me suis fait un petit programme m'affichant toutes les classes connues via RTTI
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
     
    // https://theroadtodelphi.com/2010/10/04/fun-with-delphi-rtti-building-a-treeview-with-all-your-classes/
    uses
    Rtti;
     
    procedure FillTreeClasses(TreeViewClasses:TTreeView);
     
            //function to get the node wich match with the TRttiType
            function FindTRttiType(lType:TRttiType):TTreeNode;
            var
              i        : integer;
              Node     : TTreeNode;
            begin
               Result:=nil;
                 for i:=0 to TreeViewClasses.Items.Count-1 do
                 begin
                    Node:=TreeViewClasses.Items.Item[i];
                    if Assigned(Node.Data) then
                     if lType=TRttiType(Node.Data) then
                     begin
                      Result:=Node;
                      exit;
                     end;
                 end;
            end;
     
            //function to get the node wich not match with the BaseType of the Parent
            function FindFirstTRttiTypeOrphan:TTreeNode;
            var
              i        : integer;
              Node     : TTreeNode;
              lType    : TRttiType;
            begin
               Result:=nil;
                 for i:=0 to TreeViewClasses.Items.Count-1 do
                 begin
                     Node :=TreeViewClasses.Items[i];
                     lType:=TRttiType(Node.Data);
     
                    if not Assigned(lType.BaseType) then Continue;
     
                    if lType.BaseType<>TRttiType(Node.Parent.Data) then
                    begin
                       Result:=Node;
                       break;
                     end;
                 end;
            end;
     
    var
      ctx      : TRttiContext;
      TypeList : TArray<TRttiType>;
      lType    : TRttiType;
      PNode    : TTreeNode;
      Node     : TTreeNode;
    begin
      ctx := TRttiContext.Create;
      TreeViewClasses.Items.BeginUpdate;
      try
        TreeViewClasses.Items.Clear;
     
          //Add Root, TObject
          lType:=ctx.GetType(TObject);
          Node:=TreeViewClasses.Items.AddObject(nil,lType.Name,lType);
     
          //Fill the tree with all the classes
          TypeList:= ctx.GetTypes;
          for lType in TypeList do
            if lType.IsInstance then
            begin
                 if Assigned(lType.BaseType) then
                 TreeViewClasses.Items.AddChildObject(Node,lType.Name,lType);
            end;
     
          //Sort the classes
          Repeat
             Node:=FindFirstTRttiTypeOrphan;
             if Node=nil then break;
             //get the location of the node containing the BaseType
             PNode:=FindTRttiType(TRttiType(Node.Data).BaseType);
             //Move the node to the new location
             Node.MoveTo(PNode,naAddChild);
          Until 1<>1;
     
      finally
        TreeViewClasses.Items.EndUpdate;
        ctx.Free;
      end;
      // ajout perso 
      TreeViewClasses.FullExpand;
      TreeViewClasses.Items[0].MakeVisible;
    end;
    Du coup, j'ai utilisé une recherche (différente de la tienne mais peu importe)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    function SearchNode(TV : TTreeView; fromI : Integer; text : String) : TTreeNode;
    var i : integer;
    begin
     if (TV = nil) or (text.IsEmpty) then Exit(nil);
     for i := fromI to TV.Items.Count-1 do
       begin
         if TV.Items[i].Text.ToLower.Contains(text.ToLower) then exit(TV.Items[i]);
       end;
     result:=nil;
    end;
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    // remplissage de départ 
    procedure TuMain.btnFillClick(Sender: TObject);
    begin
    SearchFrom:=0;  // variable privée mémorise le dernier index absolu
    FillTreeClasses(Treeview1);
    end;
    la procédure intéressante ici est le positionnement dans le "centre vertical" pour le node trouvé
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    // uses  winapi.CommCtrl
    procedure TuMain.SpeedButton1Click(Sender: TObject);
    var node : TTreeNode;
        ri : integer;
        i  : Integer;
        DR, CR: TRect;
        ScrollPx, ScrollLines : Integer;
    begin
    if Treeview1.Items.Count=0 then exit;
    
    if not Sametext(LastSearch,edtSearch.Text)  // si différent on recommence au début
       then begin
         SearchFrom:=0;
         Treeview1.Items[0].MakeVisible;
         LastSearch:=edtSearch.Text;  // pour recherche incrémentale 
       end;
    node:=SearchNode(TreeView1,SearchFrom,edtSearch.Text);
    if assigned(node) then
     begin
       TreeView1.Selected:=Node;
       SearchFrom:=Node.AbsoluteIndex+1;
    // calculate how many *pixels* should we scroll
        DR := TreeView1.Selected.DisplayRect(False);
        CR := TreeView1.ClientRect;
        ScrollPx := - Round((CR.Bottom / 2) - DR.Top - ((DR.Bottom - DR.Top) / 2));
    
        // how many lines does it correspond to
        ScrollLines := ScrollPx div TreeView_GetItemHeight(TreeView1.Handle);
    
        // scroll that many lines
        if ScrollLines > 0 then
          for i := 1 to ScrollLines do
            TreeView1.Perform(WM_VSCROLL, MakeWParam(SB_LINEDOWN, 0), 0)
        else if ScrollLines < 0 then
          for i := ScrollLines to -1 do
            TreeView1.Perform(WM_VSCROLL, MakeWParam(SB_LINEUP, 0), 0);
     end
     else begin
        SearchFrom:=0;
     end;
    end;
    et voilà le résultat
    Nom : Capture.PNG
Affichages : 170
Taille : 23,9 Ko

    [Edit] Pour avoir le noeud sélectionné en haut c'est même plus simple puisqu'il s'agit juste d'utiliser DR.Top et de diviser par la hauteur de ligne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
        DR := TreeView1.Selected.DisplayRect(False);
        Scrollpx:=  DR.Top;
        // how many lines does it correspond to
        ScrollLines := ScrollPx div TreeView_GetItemHeight(TreeView1.Handle);
        // scroll that many lines
        if ScrollLines > 0 then
          for i := 1 to ScrollLines do
            TreeView1.Perform(WM_VSCROLL, MakeWParam(SB_LINEDOWN, 0), 0)
        else if ScrollLines < 0 then
          for i := ScrollLines to -1 do
            TreeView1.Perform(WM_VSCROLL, MakeWParam(SB_LINEUP, 0), 0);
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Nodes activate et deactivate sur unreal 4.20
    Par Phylogénus dans le forum Unreal Engine
    Réponses: 0
    Dernier message: 25/04/2020, 10h25
  2. TTreeView + 4 CheckBox sur chaque Node
    Par smartland dans le forum Delphi
    Réponses: 1
    Dernier message: 17/06/2006, 02h00
  3. Comment sauver un TTreeView et ses nodes data ?
    Par faimo dans le forum Composants VCL
    Réponses: 6
    Dernier message: 19/04/2005, 13h48
  4. Tester connexion Internet active sous Windows
    Par Altau dans le forum Développement
    Réponses: 3
    Dernier message: 12/08/2002, 12h43
  5. [Kylix] Clé d'activation
    Par Anonymous dans le forum EDI
    Réponses: 1
    Dernier message: 27/03/2002, 23h19

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo