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

Source: Internet
Author: User
Technorati Tags: VC, Listctrl

Original address: http://www.vckbase.com/document/viewdoc/?id=1855

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

Author: Effortsboyszyb

Download source code

Summary

Using CListCtrl to display data is convenient, sometimes we need to label a column or a cell background and font color, or need to change the row height and font size, CListCtrl to change this is not very convenient. This article describes 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 headers.

Keywords: self-painted sub-class color CListCtrl CHeaderCtrl

First, the realization process

1. Table Header Modification
Create a new MFC class CHEADERCTRLCL, its base class is CHeaderCtrl, the response OnPaint message implementation self-painting, implementation code see source code (because the code is more space, so do not paste it up, sorry), in the header file to define the function
LRESULT onlayout (WPARAM WPARAM, LPARAM LPARAM), after manually adding the message response On_message (hdm_layout, onlayout), change the height in the message response, implementing the code as follows:

LRESULT cheaderctrlcl::onlayout (WPARAM WPARAM, LPARAM LPARAM)
{
    
    
    
    
    int nheight = (int) (PWPOS->CY * m_height); Change height, m_height to multiples
    
    
    
}
2. Changes to the table
Create a new MFC class CLISTCTRLCL, whose base class is CListCtrl, define a CHEADERCTRLCL member variable M_header, Overload Presubclasswindow (), modify the control type in the function to self-paint mode, and then subclass the table header , the code is as follows:
void CLISTCTRLCL::P Resubclasswindow ()
{
    TODO: Add private code here and/or call base class
    ModifyStyle (0,lvs_ownerdrawfixed);
    CListCtrl::P Resubclasswindow ();
    CHeaderCtrl *pheader = GetHeaderCtrl ();
    M_header.subclasswindow (Pheader->getsafehwnd ());
}
Add member variables to save some basic information.
CPtrList M_ptrlistcol;  Save column Colors
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 implement the self-painting, the implementation code please refer to the code. Add the Message Macro On_wm_measureitem_reflect () manually, adding the following function, which implements modifying the row height.
void Clistctrlcl::measureitem (Lpmeasureitemstruct lpmeasureitemstruct)
{
    if (m_nrowheight>0)
    {
        Lpmeasureitemstruct->itemheight = M_nrowheight;
    }
}
Add message processing OnMeasureItem ()
void Clistctrlcl::onmeasureitem (int nidctl, lpmeasureitemstruct lpmeasureitemstruct)
{
    TODO: Add Message Handler code and/or call default values here
    Clistctrl::onmeasureitem (Nidctl, lpmeasureitemstruct);
}
Overwrite base class InsertColumn to save column information
            
            
            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);
}
Second, the operation effect diagram is as follows:

Iii. Methods of Use

Add HeaderCtrlCl.h, HeaderCtrlCl.cpp, ListCtrlCl.h, ListCtrlCl.cpp to your project, drag a CListCtrl control to the dialog box, add the member variable, and then in the header file # Include "ListCtrlCl.h", change the member variable type you just added to CLISTCTRLCL, and add the initialization code in the dialog box OnInitDialog ():

M_listctrl.setcolcolor (0,rgb (10,150,20)); Set Column background color
m_listctrl.setcolcolor (2,rgb (30,100,90)); 

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.