Click here to Skip to main content
15,168,423 members
Articles / Web Development / HTML
Tip/Trick
Posted 17 Oct 2013

Stats

31.4K views
2.5K downloads
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

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:

C++
//
//Your_DialogDlg.h
//
#pragma once
#include "ListCtrlEx.h"
public:
    CListCtrlEx m_cListCtrl1;
//
C++
//
//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

C++
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
C++
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
C++
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).

C++
void SetShowSelectedItemUnFocus (BOOL bEtat)

TRUE : Activate the highlighted when unfocus

FALSE : Deactivate it.

Variable is m_bShowSelectedItemUnFocus

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

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

Variable is m_iPenStyles

C++
void SetPenStyleColor (COLORREF clrPenStyle)

Define border color. Default is white.

Focus item is therefore drawn in red.

Variable is m_clrPenStyle

C++
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

History

License

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

Share

About the Author

MaxMax14
France France
No Biography provided

Comments and Discussions

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