Click here to Skip to main content
11,490,973 members (67,157 online)
Click here to Skip to main content

Get Highlight Selected Items on CListCtrl when there is no Focus

, 17 Oct 2013 CPOL 9.1K 831 10
Rate this:
Please Sign up or sign in to vote.
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 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

History

  • 2013-10-17 : First published

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

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 17 Oct 2013
Article Copyright 2013 by MaxMax14
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid