Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

keeping tree control selection when control loses focus

Posted on 1997-09-22
8
Medium Priority
?
401 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

885 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