CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: MFC CListCtrl change checkbox color

  1. #1
    Join Date
    Jan 2021
    Posts
    16

    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;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,937

    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

  3. #3
    Join Date
    Jan 2021
    Posts
    16

    Re: MFC CListCtrl change checkbox color

    Quote Originally Posted by VictorN View Post
    CListCtrl or CListBox?
    The CTLCOLOR_LISTBOXw belongs to the CListBox class (ListBox control)
    Thank you for reply! I use CListCtrl

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,937

    Re: MFC CListCtrl change checkbox color

    Quote Originally Posted by al072 View Post
    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

  5. #5
    Join Date
    Jan 2021
    Posts
    16

    Re: MFC CListCtrl change checkbox color

    It would be nice to see a code sample, can anybody share it please?

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,937

    Re: MFC CListCtrl change checkbox color

    Quote Originally Posted by al072 View Post
    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

  7. #7
    Join Date
    Jan 2021
    Posts
    16

    Re: MFC CListCtrl change checkbox color

    Quote Originally Posted by VictorN View Post
    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

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,937

    Re: MFC CListCtrl change checkbox color

    Then you nave to implement owner or custom drawing!
    some examples:
    http://codexpert.ro/blog/2013/02/22/...draw-controls/
    https://www.codeproject.com/KB/list/#Custom+Draw
    Victor Nijegorodov

  9. #9
    Join Date
    Jan 2021
    Posts
    16

    Re: MFC CListCtrl change checkbox color

    Quote Originally Posted by VictorN View Post
    Then you nave to implement owner or custom drawing!
    some examples:
    http://codexpert.ro/blog/2013/02/22/...draw-controls/
    https://www.codeproject.com/KB/list/#Custom+Draw
    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

  10. #10
    Join Date
    Jan 2021
    Posts
    16

    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

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,432

    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++20 Compiler: Microsoft VS2019 (16.8.6)

  12. #12
    Join Date
    Jan 2021
    Posts
    16

    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

  13. #13
    Join Date
    Jan 2021
    Posts
    16

    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

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,457

    Re: MFC CListCtrl change checkbox color

    Take a look at the lvselstate sample here: https://forums.codeguru.com/showthre...00#post2238400

  15. #15
    Join Date
    Jan 2021
    Posts
    16

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)