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 Sanjay1982, 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"
public:
CListCtrlEx m_cListCtrl1;
BOOL Your_DialogDlg::OnInitDialog()
{
m_bMultiSelect = TRUE;
m_cListCtrl1.ModifyStyle(LVS_SINGLESEL,NULL,0);
m_cListCtrl1.SetUnFocusColors(RGB(0, 250, 0),
RGB(255, 0, 255));
m_bShowWhenUnfocus = TRUE;
m_cListCtrl1.SetShowSelectedItemUnFocus
(m_bShowWhenUnfocus);
m_bDrawBordure = TRUE;
m_cListCtrl1.SetBordureActive(m_bDrawBordure);
m_iRadio1 = PS_DOT;
m_cListCtrl1.SetPenStyle(m_iRadio1); }
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