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

  • SEARCH
  • Home
  • 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 8690455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:54:24+00:00 2026-06-12T23:54:24+00:00

due to layout customization needs, I extended CTreeCtrl class. I absolutely need to use

  • 0

due to layout customization needs, I extended CTreeCtrl class.

I absolutely need to use CDC::DrawText() function to dinamically (re)write the text of the single nodes, WITHOUT calling SetItemText() function more than once(mandatory requisite).

Then, I wrote my own implementation of OnPaint() method.
I implemented also a DrawItems() method which draws nodes in CTreeCtrl.

Since I don’t want to modify anything else than single labels beside the single nodes, then I need to re-implement most of original CTreeCtrl::OnPaint() code.

I only have two doubts:

  1. How can I show the DEFAULT CTreeCtrl icons? I dont’need/don’t want
    custom icons.
  2. How can I restore the default layout of selection mode for individual nodes?

Simply, currently selected nodes should be appear highlighted.

Some pieces of simplified, auto-explanatory code below:

void MyDlg::OnPaint()
{
    CPaintDC dc(this);
    CDC dc_ff;  
    CBitmap bm_ff;
    CBitmap *bm_old;
    CFont *font;
    CFont *old_font;
    CFont fontDC;
    int old_mode;
    GetClientRect(&m_Rect);

    dc_ff.CreateCompatibleDC( &dc );
    bm_ff.CreateCompatibleBitmap( &dc, m_Rect.Width(), m_Rect.Height() ); 
    dc_ff.SelectObject( &bm_ff ); 
    font = GetFont();
    old_font = dc_ff.SelectObject( font );

    // Could / Should be called here?
    CWnd::DefWindowProc(WM_PAINT, (WPARAM)dc.m_hDC, 0);

    old_mode = dc_ff.SetBkMode(TRANSPARENT);

    dc_ff.FillSolidRect(m_Rect, dc_ff.GetBkColor());

    DrawItems( &dc_ff ); // DrawItems() member function draws single nodes of CTreeCtrl

    dc.BitBlt( m_Rect.left, m_Rect.top, m_Rect.Width(), m_Rect.Height(), &dc_ff, 0, 0, SRCCOPY);

    dc_ff.SelectObject( old_font );
    dc_ff.SetBkMode( old_mode );
    dc_ff.SelectObject( bm_old ); 
}

void MyDlg::DrawItems( CDC *pDC )
{
    // draw items
    HTREEITEM show_item, parent;
    CRect rc_item;
    CString name;
    DWORD tree_style;
    int count = 0;
    int state;
    bool selected;
    bool has_children;

    show_item = GetFirstVisibleItem();
    if ( show_item == NULL )
        return;
    color = pDC->GetTextColor();
    tree_style = ::GetWindowLong( m_hWnd, GWL_STYLE ); 

    do
    {
        state = GetItemState( show_item, TVIF_STATE );
        parent = GetParentItem( show_item );
        has_children = ItemHasChildren( show_item ) || parent == NULL;
        selected = (state & TVIS_SELECTED) && ((this == GetFocus()) || 
                (tree_style & TVS_SHOWSELALWAYS));

        if ( GetItemRect( show_item, rc_item, TRUE ) )
        {
            if ( has_children  || selected )
            {
                if ( selected )
                {
                    // HERE i need to 
                }
                else

                // do some stuff...

                if ( has_children )
                {
                    HICON icon;
                    // HERE I need to load CTreeCtrl nodes _DEFAULT_icon
                    icon = LoadIcon(NULL, IDI_ASTERISK);
                    if ( icon != NULL )
                        DrawIconEx( pDC->m_hDC, rc_item.left - 18, rc_item.top, icon, 16, 16,0,0, DI_NORMAL ); 
                }
            }
            if ( !has_children )
            {
                HICON icon;
                *// HERE I need to load CTreeCtrl nodes _DEFAULT_icon*
                icon = LoadIcon(NULL, IDI_ASTERISK);
                if ( icon != NULL )
                    DrawIconEx( pDC->m_hDC, rc_item.left - 18, rc_item.top, icon, 16, 16,0,0, DI_NORMAL ); 
            }
            name = GetItemText( show_item );
            // ...
            if ( selected )
            {
                pDC->DrawText( "Temp", rc_item, DT_LEFT );
            }
            else
            {
                pDC->DrawText( "Temp", rc_item, DT_LEFT );
            }
            //if ( state & TVIS_BOLD )
            //  pDC->SelectObject( font );
        }
    } while ( (show_item = GetNextVisibleItem( show_item )) != NULL );
}

All I need, is source code of an almost-standard CTreeCtrl::OnPaint() implementation.
Any suggestion/help is appreciated. 🙂

Thanks

IT.

  • 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-06-12T23:54:26+00:00Added an answer on June 12, 2026 at 11:54 pm

    You don’t need to overload onPaint. If you set a tree items text as LPSTR_TEXTCALLBACK, the CtreeCtrl will fire the message TVN_GETDISPINFO to retrieve new text every time that item is displayed. Regeister a message handler using ON_NOTIFY if it’s in a parent window or ON_NOTIFY_REFLECT if you are subclassing CTreeCtrl. This message handler can assign the text you want but allow the treeCtrl to continue drawing as normal.

    TVN_GETDISPINFO Documentation

    If you went the parent Cwnd route, youd need

    1. So you’d need to assign the message handler in the cpp file:

      BEGIN_MESSAGE_MAP(MyCWnd, CWnd)

      ON_NOTIFY(TVN_GETDISPINFO, tree_ctl_id, CustomTreeControl::OnGetdispinfo)
      END_MESSAGE_MAP()

    2. Function prototype in header

      afx_msg void OnGetdispinfo( NMHDR* pNMHDR, LRESULT* pResult );

    3. This at the end of your class definition

      DECLARE_MESSAGE_MAP()

    4. And the actual function to handle the request

      void ColumnTreeControl::OnGetdispinfo(NMHDR* pNMHDR, LRESULT* pResult)
      {
      NMTVDISPINFO * pDispInfo = (NMTVDISPINFO )pNMHDR;
      TVITEM
      item = &pDispInfo->item;

      if(item->mask & TVIF_TEXT )
      {
               item->pszText " YOUR CODE HERE";
      }
      *pResult = 0;
      

      }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've built an app but due to layout issues in tablets i decided not
In bash, due to the layout of my keyboard, I often do cd ..<tab>
let me start by saying that due to the complicated design of the layout
First to describe the problem: During a re-layout due to a change in width
My View doesn't use an XML layout, I just reference a View subclass defined
I am getting null pointer exception, due to failed to recognize layout parameter in
Is there any way to access a layout's view from a non-Activity-derived class? I'm
I have this layout, and it's scaled weirdly (probably due to inadviseable layout nesting)
Due to a sad situation I receive HTML like this: <p>Perform the following commands:
Due to legacy reasons a lot of our data is stored encoded in standard

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.