Link to home
Start Free TrialLog in
Avatar of zelda071497
zelda071497

asked on

keeping tree control selection when control loses focus

I have a tree control.  I have set the TVS_SHOWSELALWAYS style.  When I select an item
in the tree control, it is highlighted in blue.  If the control
loses focus, the selection is highlighted in grey.  How
do I force it to always highlight the selection in blue, even
when the control loses focus?
Avatar of RONSLOW
RONSLOW

Are you sure you have properly set the TVS_SHOWSELALWAYS?

The best way is to do it in PreSubclassWindow - if you do it in OnCreate etc then it will not work unless you are creating the tree control yourself because OnCreate (and earlier) messages have already been send by the time you subclass a control.

ASKER CERTIFIED SOLUTION
Avatar of Dmitry091997
Dmitry091997

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zelda071497

ASKER

I looked at the MFC document.  The tree control does not have a DrawItem function (things like list boxes and combo boxes do).
Do you just override the handler for WM_DRAWITEM that is
defined on CWnd?
I added a handler for WM_DRAWITEM as I mentioned above.  But this handler never gets called.  From what I read, it sounds like you cannot have an owner draw tree control.  So, how do you do this?
Sorry, zelda, I was wrong... One should do it on WM_PAINT( OnPaint() ) but programming is enough difficult
Hi, zelda! Try it:

// MyTree.cpp : implementation file
//

#include "stdafx.h"
#include "MyTree.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyTree

CMyTree::CMyTree()
{
}

CMyTree::~CMyTree()
{
}


BEGIN_MESSAGE_MAP(CMyTree, CTreeCtrl)
      //{{AFX_MSG_MAP(CMyTree)
      ON_WM_PAINT()
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyTree message handlers

BOOL CMyTree::PreCreateWindow(CREATESTRUCT& cs)
{
  cs.style |= TVS_SHOWSELALWAYS;
  return CTreeCtrl::PreCreateWindow(cs);
}


// It work anyhow, but you must do something else
void CMyTree::OnPaint()
{
  HTREEITEM htItem;
  CRect rect;
  COLORREF clrBk, clrFore;
  CString strText;

  CPaintDC dc(this); // device context for painting
      
  htItem = GetNextItem(NULL, TVGN_FIRSTVISIBLE);
  clrBk = dc.GetBkColor();
  clrFore = dc.GetTextColor();
  while(htItem != NULL)
  {
      BOOL bSel = GetItemState(htItem, TVIS_SELECTED) & TVIS_SELECTED;

      GetItemRect(htItem, rect, TRUE);
      strText = GetItemText(htItem);
      dc.SetBkColor(bSel ? ::GetSysColor(COLOR_HIGHLIGHT) : clrBk);
      dc.SetTextColor(bSel ? ::GetSysColor(COLOR_HIGHLIGHTTEXT) : clrFore);
      dc.FillSolidRect( rect, dc.GetBkColor() );
      dc.DrawText( strText, rect, DT_LEFT | DT_VCENTER);
      htItem = GetNextItem(htItem, TVGN_NEXTVISIBLE);
  }
}

I tried this.  It shows my tree entries in a different font.  In addition, the text is cut off and the tree control "+" and "-"'s are missing.  If I do a CTreeCtrl::OnPaint at the beginning of the function.  It does not work at all.
I know, zelda, that it works so. That's only a template... Everything else you have to programm yourself including font settings and icons drawing... I said, that programming is enough difficult