Hello to all! Check out my TTreeViewEx derived code from TTreeView:

 function TTreeViewEx.CustomDrawItem(Node: TTreeNode; State: TCustomDrawState; Stage: TCustomDrawStage; var PaintImages: Boolean): Boolean; var NodeRect: TRect; procedure DrawSelection; begin if FSelectionBold then Canvas.Font.Style := Canvas.Font.Style + [fsBold]; if FFocused then Canvas.Brush.Color := GetShadowColor(clHighlight, 60) else Canvas.Brush.Color := GetShadowColor($F0F0F0, -10); end; begin Result := True; inherited CustomDrawItem(Node, State, Stage, PaintImages); if Result then begin if (Stage in [cdPrePaint]) then begin Canvas.Font.Color := clBlack; if FSelectionBold then Canvas.Font.Style := Canvas.Font.Style - [fsBold]; Canvas.Brush.Color := clWhite; if cdsHot in State then begin if Node Selected then Canvas.Brush.Color := ApproximateColor(clBtnHighlight, clHighlight, 256 / 9) else DrawSelection; end else if cdsSelected in State then DrawSelection; NodeRect := Node.DisplayRect(not RowSelect); if RowSelect then begin NodeRect.Right := BoundsRect.Right; NodeRect.Left := BoundsRect.Left; end; Canvas.FillRect(NodeRect); end; end; end; 

Now how can you draw a frame around the selected as in the picture Windows explorer

    3 answers 3

    It all started from setting plus and minus to Button:

     procedure TTreeViewEx.CreateWnd; begin inherited CreateWnd; SetWindowTheme(Handle, nil, nil); end;
    procedure TTreeViewEx.CreateWnd; begin inherited CreateWnd; SetWindowTheme(Handle, nil, nil); end; 

    And make a RowSelect

      Why do you have to draw everything with your own resources? Usually Custom Draw is used to change some details (for example, a font, or to add an icon, or something like that). Generally, if you want to draw everything yourself, then there is nothing special in Custom Draw - it's easier to do Owner Draw. These are general considerations, if without reference to Delphi, perhaps there are some special features.

      Well, in any case, Windows draws it through Theme API. DrawThemeBackground and similar. Here's a similar question: https://stackoverflow.com/questions/10930695/how-to-draw-ttreeviews-styled-selection-rectangle-during-advancedcustomdrawitem

        I decided everything.

         if (cdsSelected in State) and Focused then DrawFocusRect(Canvas.Handle, NodeRect); 

        enter image description here

        • To format the code, use an indent of four spaces (select the code and press Ctrl + K). - Athari