Solved

keeping tree control selection when control loses focus

Posted on 1997-09-22
8
387 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 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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 58
notReplace  challenge 53 102
Annoying "thing" blocks my view 4 51
Thin secure Windows 10 5 49
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

746 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now