-
January 12th, 2021, 12:02 PM
#1
MFC CListCtrl change checkbox color
Hi dear guru!!! I need your help!!! I'm trying to find a way to change background color of checkbox inside CListCtrl, please help me to find a working solution. Maye there is some way to work with CListCtrl subitems inside the OnCtlColor function? I can change the text color and backcolor of CListCtrl, but can't find a way how to change the checkboxes background color...
HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if ( nCtlColor== CTLCOLOR_LISTBOX)
{
pDC->SetTextColor(RGB(255, 0, 0));
pDC->SetBkMode(TRANSPARENT);
pDC->SetBkColor(RGB(0,255,0));
hbr = m_brush;
}
return hbr;
}
-
January 12th, 2021, 01:20 PM
#2
Re: MFC CListCtrl change checkbox color
CListCtrl or CListBox?
The CTLCOLOR_LISTBOX belongs to the CListBox class (ListBox control)
Last edited by VictorN; January 13th, 2021 at 04:20 AM.
Victor Nijegorodov
-
January 12th, 2021, 01:22 PM
#3
Re: MFC CListCtrl change checkbox color
Originally Posted by VictorN
CListCtrl or CListBox?
The CTLCOLOR_LISTBOXw belongs to the CListBox class (ListBox control)
Thank you for reply! I use CListCtrl
-
January 12th, 2021, 02:15 PM
#4
Re: MFC CListCtrl change checkbox color
Originally Posted by al072
Thank you for reply! I use CListCtrl
CListCtrl already has the methods to set the background ( https://docs.microsoft.com/en-us/cpp...160#setbkcolor ) and text colors. So you don't need to handle OnCtlColor message.
However, if it won't work for the item checkboxes than you will need to implement owner or custom draw.
Victor Nijegorodov
-
January 12th, 2021, 09:27 PM
#5
Re: MFC CListCtrl change checkbox color
It would be nice to see a code sample, can anybody share it please?
-
January 13th, 2021, 02:59 AM
#6
Re: MFC CListCtrl change checkbox color
Originally Posted by al072
It would be nice to see a code sample, can anybody share it please?
Did you try using CListCtrl::SetBkColor? Does it work or not?
Victor Nijegorodov
-
January 13th, 2021, 09:20 AM
#7
Re: MFC CListCtrl change checkbox color
Originally Posted by VictorN
Did you try using CListCtrl::SetBkColor? Does it work or not?
Yes i did, this method only change back color of CListCtrl, but not change the color of checkboxes... it doesnt work for me
-
January 13th, 2021, 10:23 AM
#8
Re: MFC CListCtrl change checkbox color
Victor Nijegorodov
-
January 15th, 2021, 01:51 PM
#9
Re: MFC CListCtrl change checkbox color
Originally Posted by VictorN
Hi, VictorN!!! First of all i want to thank you very much for your time and help. Finally i found the almost what i need in this link: https://www.codeproject.com/Articles...n-List-Control
There is a sample of how to draw a checkbox in CHeaderCtrl using a .bmp images.
Here is some code:
BOOL CCheckListCtrl::Init()
{
if (m_blInited)
return TRUE;
CHeaderCtrl* pHeadCtrl = this->GetHeaderCtrl();
ASSERT(pHeadCtrl->GetSafeHwnd());
VERIFY( m_checkHeadCtrl.SubclassWindow(pHeadCtrl->GetSafeHwnd()) );
VERIFY( m_checkImgList.Create(IDB_CHECKBOXES, 16, 3, RGB(255,255,255)) );
int i = m_checkImgList.GetImageCount();
m_checkHeadCtrl.SetImageList(&m_checkImgList);
HDITEM hdItem;
hdItem.mask = HDI_IMAGE | HDI_FORMAT;
VERIFY( m_checkHeadCtrl.GetItem(0, &hdItem) );
hdItem.iImage = 1;
hdItem.fmt |= HDF_IMAGE;
VERIFY( m_checkHeadCtrl.SetItem(0, &hdItem) );
m_blInited = TRUE;
return TRUE;
}
void CCheckListCtrl::OnItemChanged(NMHDR* pNMHDR, LRESULT* pResult)
{
NMLISTVIEW* pNMLV = (NMLISTVIEW*)pNMHDR;
*pResult = 0;
if ( m_blInited && LVIF_STATE == pNMLV->uChanged)
{
BOOL blAllChecked = TRUE;
int nCount = GetItemCount();
for(int nItem = 0; nItem < nCount; nItem++)
{
if ( !ListView_GetCheckState(GetSafeHwnd(), nItem) )
{
blAllChecked = FALSE;
break;
}
}
HDITEM hdItem;
hdItem.mask = HDI_IMAGE;
if (blAllChecked)
hdItem.iImage = 2;
else
hdItem.iImage = 1;
VERIFY( m_checkHeadCtrl.SetItem(0, &hdItem) );
}
}
I change the checkbox.bmp color and it looks like i need, but this code sample demonstrate a chechbox in CheaderCtrl!
Could you help me to undestand and find out how to do the same for CListCtrl checkboxes, i think i should use LVITEM instead of HDITEM, but i dont know how to do it and how to get the instances of checkboxes...
I am on the right way... help me please
-
January 15th, 2021, 01:55 PM
#10
Re: MFC CListCtrl change checkbox color
Attachment 35940
That is what i can achieve, take a look please on the top left corner, there is a custom checkbox, but i need the same inside of the CListCtrl not in the header and i dont know how to do it
-
January 16th, 2021, 05:09 AM
#11
Re: MFC CListCtrl change checkbox color
[Attachment Link is invalid]
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 17th, 2021, 06:26 AM
#12
Re: MFC CListCtrl change checkbox color
I found a solution, how to load checkbox images without custom draw, i am using the flag "LVSIL_STATE"
Code:
m_imglist.Add(hUnChecked);
m_imglist.Add(hChecked);
this->SetImageList(&m_imglist, LVSIL_STATE);
But states are not changed automatically like in CTreeCtrl, states changes only if i pass the SetItemState manually:
Code:
m_FaceExplorerLIST.SetItemState(i, INDEXTOSTATEIMAGEMASK(1), LVIS_STATEIMAGEMASK);
Is there way to make the LVSIL_STATE checkboxes work as native in automatic mode? If there is no way to do it wich is the best practice to handle the LVSIL_STATE checkboxes state? Wich message handler i should use to set states? Thank you
-
January 17th, 2021, 09:08 AM
#13
Re: MFC CListCtrl change checkbox color
I found this way to check and toggle the state of LVSIL_STATE checkbox and it is work perfect:
Code:
void CAlphaListCtrl::OnNMClick(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
int nItem = static_cast<int>(pNMItemActivate->iItem);
LVITEM rItem;
rItem.mask = LVIS_STATEIMAGEMASK;
rItem.iItem = nItem;
this->GetItem(&rItem);
if (this->GetItemState(rItem.iItem, LVIS_STATEIMAGEMASK) == 4096)
{
this->SetItemState(rItem.iItem, INDEXTOSTATEIMAGEMASK(2), LVIS_STATEIMAGEMASK);
}
else
{
this->SetItemState(rItem.iItem, INDEXTOSTATEIMAGEMASK(1), LVIS_STATEIMAGEMASK);
}
*pResult = 0;
}
But i am not sure this is a right way.., i dont know why, but the Uint value is unchecked box is alway 4096 and i use it to check the state...
Could anybody show me the right way of checking and toggling states please and explaint this magic 4096 number? Thank you very much
-
January 17th, 2021, 08:09 PM
#14
Re: MFC CListCtrl change checkbox color
-
January 18th, 2021, 12:49 PM
#15
Re: MFC CListCtrl change checkbox color
Thank you very much for the link, very interesting and helpful information, unfurtunatley didn't find the answer on my question about the magic number))) But the example in really uncommon., i like the dispinfo approach, but finally it didn't work for me
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|