Solved

keeping tree control selection when control loses focus

Posted on 1997-09-22
8
395 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 100 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

623 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