Get Highlight Selected Items on CListCtrl when there is no Focus
Keep the items highlighted when focus is on another control
Introduction
I created an MDI application with CListCtrl
and needed to see the selected items when focus was on another control. Standard behavior of CListCtrl
changes the highlighted color to a no visible color when focus is not on the CListCtrl
. I found nothing on the web giving a quick solution. So here is my own solution.
Background
As I use most of the time CListCtrlEx
from , 24 Jul 2008 (see his article "An Extended MFC CListCtrl to edit individual cells, sort headers, and more"), I modified his code with additional options.
So this tip will treat only the additional parts for highlight. For all other features, see Sanjay's article.
Using the Code
Add the following files in your project:
- ListCtrlEx.h & .cpp
- MsgHook.h & .cpp
Here is the main settings parts with comments:
//
//Your_DialogDlg.h
//
#pragma once
#include "ListCtrlEx.h"
public:
CListCtrlEx m_cListCtrl1;
//
//
//Your_DialogDlg.cpp
BOOL Your_DialogDlg::OnInitDialog()
{
//........
//Set Multi-Select at start
m_bMultiSelect = TRUE;
m_cListCtrl1.ModifyStyle(LVS_SINGLESEL,NULL,0);
//Change Backcolor and Textcolor when unfocus :
//Default is ::GetSysColor(COLOR_HIGHLIGHT) and ::GetSysColor(COLOR_HIGHLIGHTTEXT)
m_cListCtrl1.SetUnFocusColors(RGB(0, 250, 0),
RGB(255, 0, 255)); //COLORREF clrBackUnfocus, COLORREF clrTextUnfocus
//Activate colors when unfocus
m_bShowWhenUnfocus = TRUE;
m_cListCtrl1.SetShowSelectedItemUnFocus
(m_bShowWhenUnfocus); //True: Show selected item without focus
//Activate border
m_bDrawBordure = TRUE;
m_cListCtrl1.SetBordureActive(m_bDrawBordure);
//Define type of border (default is PS_DOT : not necessary to setup if you need PS_DOT)
m_iRadio1 = PS_DOT;
m_cListCtrl1.SetPenStyle(m_iRadio1); //PS_SOLID PS_DASH PS_DOT
//
//.....
}
Functions
void SetUnFocusColors
( COLORREF clrBackUnfocus, COLORREF clrTextUnfocus)
|
Define the color of selected items when SetShowSelectedItemUnFocus(TRUE) activate the function; Back color and text color is used when Default back color is stored in m_clrDefTextUnfocus |
void SetRowColors
(int nItem, COLORREF clrBk,
COLORREF clrText, COLORREF clrBackUnfocus,
COLORREF clrTextUnfocus)
|
It is an extension of original Default back color is store in m_clrDefTextUnfocus |
void SetRowColorsUnfocus
(int nItem, COLORREF clrBackUnfocus, COLORREF clrTextUnfocus
|
Define the back color and text color for the row defined by Default back color is stored in Default text color is stored in |
void SetShowSelectedItemUnFocus (BOOL bEtat)
|
TRUE : Activate the highlighted when unfocus FALSE : Deactivate it. Variable is |
BOOL GetShowSelectedItemUnFocus ()
|
Return variable is m_bShowSelectedItemUnFocus |
void SetPenStyle (int iStyle)
|
Defined style of border. Example : Variable is |
void SetPenStyleColor (COLORREF clrPenStyle)
|
Define border color. Default is white. Focus item is therefore drawn in red. Variable is |
void SetBordureActive (BOOL bBordure)
|
TRUE : Activate border drawing on the highlighted rectangle. FALSE : Deactivate border drawing. Border color is defined by Focus item is therefore drawn in red. Style of border is defined by Variable is |
History
- 2016-02-02 :Filling correction as proposed by Alessandro Di Martino
- 2013-10-17 : First published