Click here to Skip to main content
15,311,151 members
Articles / Web Development / HTML
Posted 17 Oct 2013


22 bookmarked

Get Highlight Selected Items on CListCtrl when there is no Focus

Rate me:
Please Sign up or sign in to vote.
4.79/5 (11 votes)
2 Feb 2016CPOL2 min read
Keep the items highlighted when focus is on another control

Image 1


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.


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:

#pragma once
#include "ListCtrlEx.h"
    CListCtrlEx m_cListCtrl1;

BOOL Your_DialogDlg::OnInitDialog()

    //Set Multi-Select at start
    m_bMultiSelect = TRUE;

    //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_bShowWhenUnfocus);    //True: Show selected item without focus

    //Activate border
    m_bDrawBordure = TRUE;

    //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


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 ClistCtrl is focused and unfocused (by default, it is ::GetSysColor(COLOR_HIGHLIGHT) for back color and ::GetSysColor(COLOR_HIGHLIGHTTEXT) for text color.

Default back color is stored in m_clrDefBackUnfocus

Default text color is stored in m_clrDefTextUnfocus
void SetRowColors
(int nItem, COLORREF clrBk, 
COLORREF clrText, COLORREF clrBackUnfocus, 
COLORREF clrTextUnfocus)

It is an extension of original SetRowColors() with additional parameters : COLORREF clrBackUnfocus, COLORREF clrTextUnfocus.

Default back color is store in m_clrDefBackUnfocus

Default text 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 nItem when SetShowSelectedItemUnFocus(TRUE) activates the function.  

Default back color is stored in m_clrDefBackUnfocus. Default is ::GetSysColor(COLOR_HIGHLIGHT).

Default text color is stored in m_clrDefTextUnfocus. Default is ::GetSysColor(COLOR_HIGHLIGHTTEXT).

void SetShowSelectedItemUnFocus (BOOL bEtat)

TRUE : Activate the highlighted when unfocus

FALSE : Deactivate it.

Variable is m_bShowSelectedItemUnFocus

BOOL GetShowSelectedItemUnFocus ()
Return variable is m_bShowSelectedItemUnFocus
void SetPenStyle (int iStyle)

Defined style of border. Example : PS_SOLID, PS_DASH, PS_DOT,...

Variable is m_iPenStyles

void SetPenStyleColor (COLORREF clrPenStyle)

Define border color. Default is white.

Focus item is therefore drawn in red.

Variable is m_clrPenStyle

void SetBordureActive (BOOL bBordure)

TRUE : Activate border drawing on the highlighted rectangle.

FALSE : Deactivate border drawing.

Border color is defined by SetPenStyleColor(), default is white.

Focus item is therefore drawn in red.

Style of border is defined by SetPenStyle()

Variable is m_bBordure



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

France France
No Biography provided

Comments and Discussions

QuestionThe arrows for HDF_SORTDOWN HDF_SORTUP are not visible Pin
ChrisKir28-Oct-19 14:51
MemberChrisKir28-Oct-19 14:51 
SuggestionTAB forces scroll to last item Pin
Rob_Cattral27-Feb-19 7:03
MemberRob_Cattral27-Feb-19 7:03 
QuestionNecessary? Pin
David Crow8-Feb-16 6:25
MemberDavid Crow8-Feb-16 6:25 
AnswerRe: Necessary? Pin
Damir Valiulin17-Feb-16 8:52
MemberDamir Valiulin17-Feb-16 8:52 
QuestionFilling Correction Pin
_sigfrid_20-Nov-15 7:20
Member_sigfrid_20-Nov-15 7:20 
AnswerRe: Filling Correction Pin
MaxMax142-Feb-16 8:37
MemberMaxMax142-Feb-16 8:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.