Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

keeping tree control selection when control loses focus

Posted on 1997-09-22
8
Medium Priority
?
397 Views
Last Modified: 2013-11-19
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?
0
Comment
Question by:zelda071497
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1306703
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.

0
 

Accepted Solution

by:
Dmitry091997 earned 200 total points
ID: 1306704
The TreeCtrl work really so. When control loses focus, the selection highlighted in COLOR_INACTIVECAPTION. We resolved such problems doing owner draw Tree Control

void CYourTreeCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
  if(lpDrawItemStruct->itemID == -1)
      return;
  CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  BOOL bSelect = (lpDrawItemStruct->itemState & ODS_SELECTED) == ODS_SELECTED;
  COLORREF clrSave;
    ...
   if(bSelect)
      clrSave = pDC->SetBkColor(COLOR_HIGHLIGHT);
   ...
   // restore color
   pDC->SetBkColor(clrSave);
}
0
 

Author Comment

by:zelda071497
ID: 1306705
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?
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 

Author Comment

by:zelda071497
ID: 1306706
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?
0
 

Expert Comment

by:Dmitry091997
ID: 1306707
Sorry, zelda, I was wrong... One should do it on WM_PAINT( OnPaint() ) but programming is enough difficult
0
 

Expert Comment

by:Dmitry091997
ID: 1306708
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);
  }
}

0
 

Author Comment

by:zelda071497
ID: 1306709
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.
0
 

Expert Comment

by:Dmitry091997
ID: 1306710
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
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question