Change CListCtrl, CHeaderCtrl height, font, color, and background

Source: Internet
Author: User
Summary

CListCtrl is more convenient to display data. Sometimes we need to mark the background and font color of a column or a cell, or need to change the row height and font size. CListCtrl is not very convenient to change these. This article will introduce how to derive a class to change the height, font size, column background color, cell background color, column font color, cell font color of CListCtrl and its header.

Keywords: self-painting subclassing color CListCtrl CHeaderCtrl

1. Implementation process

1. Table header modification

Create a new MFC class CHeaderCtrlCl, the base class is CHeaderCtrl, implement self-painting in response to the OnPaint message, see the source code for the implementation code (since the code is more space, so I will not post it, sorry), define the function in the header file

LRESULT OnLayout (WPARAM wParam, LPARAM lParam), then manually add a message response ON_MESSAGE (HDM_LAYOUT, OnLayout), change the height in the message response, the implementation code is as follows:
LRESULT CHeaderCtrlCl :: OnLayout (WPARAM wParam, LPARAM lParam)
{
        LRESULT lResult = CHeaderCtrl :: DefWindowProc (HDM_LAYOUT, 0, lParam);
        HD_LAYOUT & hdl = * (HD_LAYOUT *) lParam;
        RECT * prc = hdl.prc;
        WINDOWPOS * pwpos = hdl.pwpos;
        int nHeight = (int) (pwpos-> cy * m_Height); // Change the height, m_Height is a multiple
        pwpos-> cy = nHeight;
        prc-> top = nHeight;
        return lResult;
}

2. Modification of the table

Create a new MFC class CListCtrlCl, the base class is CListCtrl, define a member variable m_Header of CHeaderCtrlCl, overload PreSubclassWindow (), modify the control type to self-draw mode in the function, and then subclass the table header, the code is as follows:
void CListCtrlCl :: PreSubclassWindow ()
{
        // TODO: add special code and / or call base class here
        ModifyStyle (0, LVS_OWNERDRAWFIXED);
        CListCtrl :: PreSubclassWindow ();
        CHeaderCtrl * pHeader = GetHeaderCtrl ();
        m_Header.SubclassWindow (pHeader-> GetSafeHwnd ());
}

Add member variables to save some basic information.
CPtrList m_ptrListCol; // Save column color
CPtrList m_ptrListItem; // Save the item color table
CPtrList m_colTextColor; // Save column font color
CPtrList m_ItemTextColor; // Save cell font color

Overload DrawItem () to achieve self-drawing, please refer to the code for implementation code. Manually add the message macro ON_WM_MEASUREITEM_REFLECT () and add the following function to modify the row height.
void CListCtrlCl :: MeasureItem (LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
        if (m_nRowHeight> 0)
        {
                lpMeasureItemStruct-> itemHeight = m_nRowHeight;
        }
}

Add message handling
OnMeasureItem () void CListCtrlCl :: OnMeasureItem (int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
        // TODO: add message handler code and / or call default value here
        CListCtrl :: OnMeasureItem (nIDCtl, lpMeasureItemStruct);
}

Override the base class InsertColumn to save column information
int CListCtrlCl :: InsertColumn (int nCol, LPCTSTR lpszColumnHeading,
                        int nFormat / * = LVCFMT_LEFT * /,
                        int nWidth / * = -1 * /,
                        int nSubItem / * = -1 * /)
{
        m_Header.m_HChar.Add (lpszColumnHeading);
        if (nFormat == LVCFMT_LEFT)
        {
                m_Header.m_Format = m_Header.m_Format + "0";
        }
        else if (nFormat == LVCFMT_CENTER)
        {
                m_Header.m_Format = m_Header.m_Format + "1";
        }
        else if (nFormat == LVCFMT_RIGHT)
        {
                m_Header.m_Format = m_Header.m_Format + "2";
        }
        else
        {
                m_Header.m_Format = m_Header.m_Format + "1";
        }
        return CListCtrl :: InsertColumn (nCol, lpszColumnHeading, nFormat, nWidth, nSubItem);
}

3. How to use

Add HeaderCtrlCl.h, HeaderCtrlCl.cpp, ListCtrlCl.h, ListCtrlCl.cpp to your project, drag a CListCtrl control to the dialog box, add member variables, and then #include "ListCtrlCl.h" in the header file, Change the type of the member variable just added to CListCtrlCl, and add the initialization code in OnInitDialog () of the dialog box:
m_ListCtrl.SetColColor (0, RGB (10,150,20)); // Set column background color
m_ListCtrl.SetColColor (2, RGB (30,100,90)); // Set column background color
m_ListCtrl.SetBkColor (RGB (50,10,10)); // Set the background color
m_ListCtrl.SetItemColor (1,1, RGB (100,100,10)); // Set the background color of the specified unit
m_ListCtrl.SetRowHeigt (25); // Set row height
m_ListCtrl.SetHeaderHeight (1.5); // Set the head height
m_ListCtrl.SetHeaderFontHW (16,0); // Set the head font height, and width, 0 means the default, adaptive
m_ListCtrl.SetHeaderTextColor (RGB (255,200,100)); // Set the head font color
m_ListCtrl.SetTextColor (RGB (0,255,255)); // Set text color
m_ListCtrl.SetHeaderBKColor (100,255,100,8); // Set the head background color
m_ListCtrl.SetFontHW (15,0); // Set font height and width, 0 means default width
m_ListCtrl.SetColTextColor (2, RGB (255,255,100)); // Set column text color
m_ListCtrl.SetItemTextColor (3,1, RGB (255,0,0)); // Set cell font color

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.