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

Thread: How to change the color of header in a list box control?

  1. #1
    Join Date
    Jan 2004
    Posts
    143

    How to change the color of header in a list box control?

    Hello !


    I just want to change the background color of a header in the list box control.I have seen so many posts regarding this and found ways to change text color, list control's background color etc. but didn't find the exact one for the header of a list control.

    I have a class CHeaderCustom :header file

    Code:
    class  CHeaderCustom : public  CHeaderCtrl
    {
    // Construction
    public:
    	CHeaderCustom();
    
    // Generated message map functions
    protected:
    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
    
    }
    Definition
    Code:
    void CHeaderCustom::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    {
    
     //what should i do to change the color of a header of a list box control ?
        
    }
    I having class CMyFrame derived from CFormView, which is having a list control.

    Code:
    void CMyFrame::OnInitialUpdate() 
    {
       CHeaderCtrl* pHeader = NULL;
       pHeader=my_list.GetHeaderCtrl();
    
      //set style for list control and inserted rows and columns for it
    }
    Please suggest me any straight way to do this.

    Thanks.

  2. #2
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,917

    Re: How to change the color of header in a list box control?

    Owner drawn header control.
    For each column you will have to set HDF_OWNERDRAW format.

    MSDN contains more information.
    See Header Controls topic. Also check HDITEM.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  3. #3
    Join Date
    Jan 2004
    Posts
    143

    Re: How to change the color of header in a list box control?

    Thanks for ur valuable response.

    I have achieved the coloured header of the listview control by calling WM_ERASEBKGND message.

    header file
    Code:
    class  CHeaderCustom : public CHeaderCtrl
    {}
    Definition
    Code:
    BOOL CHeaderCustom::OnEraseBkgnd(CDC* pDC) 
    {
       CRect	rc;
       GetClientRect(&rc);
       pDC->FillSolidRect(&rc, RGB(128, 128, 255));	//blue	
       return  TRUE;
    }
    and definition for WM_DRAWITEM message.
    DrawItem must override for self draw header controls.

    Code:
    void  CHeaderCustom::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    {
      
    
    }
    Form view on which ListView control is embedded
    Definition->
    Code:
    void  MyFormView :: OnInitialUpdate()
    {
        CHeaderCtrl* pHeader = NULL;
        pHeader=m_List.GetHeaderCtrl();
        	
        VERIFY(m_HeaderCtrl.SubclassWindow(pHeader->m_hWnd));	
    	
        HDITEM hdItem;
        hdItem.mask = HDI_FORMAT;
       
       //     
       // setting of  some styles and text to the header of List control goes here  
       //  
    
    }
    After this much of coding it erases the background of header button with blue color.

    Problem:
    -> Does not show the text written on header button of list ctrl(It erases the text items).

    If i override the DrawItem function, then the OnEraseBkgnd() doesn't work(no color with header of list ctrl) but the text item on header get displayed
    Definition:

    Code:
    void  CHeaderCustom::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    {
    
       ASSERT(lpDrawItemStruct->CtlType == ODT_HEADER);
    
       HDITEM hdi;
       TCHAR  lpBuffer[256];
    
       hdi.mask = HDI_TEXT;
       hdi.pszText = lpBuffer;
       hdi.cchTextMax = 256;
    
       GetItem(lpDrawItemStruct->itemID, &hdi);
    
       CDC* pDC;
       pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
       pDC->SelectObject(GetStockObject(DEFAULT_GUI_FONT));
    
       // Draw the button frame.
       ::DrawFrameControl(lpDrawItemStruct->hDC, 
          &lpDrawItemStruct->rcItem, DFC_BUTTON, DFCS_BUTTONPUSH);
    
        UINT uFormat = DT_CENTER;
    
        pDC->SetTextColor(RGB(255,255,255));   //Set text color white
    	
       //DRAW THE TEXT
       ::DrawText(lpDrawItemStruct->hDC, lpBuffer, strlen(lpBuffer), 
        &lpDrawItemStruct->rcItem, uFormat);
        
    }
    Please tell me how to achieve both task
    <1> Coloured header and
    <2> Display text item on it.

  4. #4
    Join Date
    Jan 2004
    Posts
    143

    Re: How to change the color of header in a list box control?

    Hello!

    I have posted my code in previous post, and described my problem in detail. If anything there you people not able to understand, please ask me about but reply me as soon as possible.


  5. #5
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,917

    Re: How to change the color of header in a list box control?

    To draw text and set background color:
    Code:
    void CColorHeader::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    {
    	HDITEM hdi;
    	CBrush brush(RGB(255, 0, 0));
    	CString csBuffer;
    	CRect rectFrame(lpDrawItemStruct->rcItem);
    	
    	ZeroMemory(&hdi, sizeof(HDITEM));
    	hdi.mask = HDI_TEXT;
    	
    	LPTSTR pBuf = csBuffer.GetBufferSetLength(MAX_PATH);
    	
    	hdi.mask = HDI_TEXT;
    	hdi.pszText = pBuf;
    	hdi.cchTextMax = MAX_PATH;
    	
    	GetItem(lpDrawItemStruct->itemID, &hdi);
    	csBuffer.ReleaseBuffer();
    	
    	CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    	
    	pDC->FillRect(rectFrame, &brush);
    	
    	pDC->SetTextColor(RGB(255, 255, 255));
    	pDC->SetBkMode(TRANSPARENT);
    
    	pDC->DrawText(csBuffer, &lpDrawItemStruct->rcItem, DT_LEFT | DT_SINGLELINE | DT_VCENTER);
    
    	brush.Detach();
    	
    }
    DrawFrameControl in DrawItem is not going to help you drawing frame around header button. DrawFrameControl draws frame that uses whitte and black to draw 3D frame.
    You will also not be able to set color for remainder.
    For all 9 yards, you will have to override WM_PAINT and either call default handling, draw color frame, or handle whole drawing in WM_PAINT handler without setting owner drawn.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  6. #6
    Join Date
    Jan 2004
    Posts
    143

    Re: How to change the color of header in a list box control?

    Hello sir,

    Thanks a lot, my problem has solved.
    sir , Please help me on this post.
    http://www.codeguru.com/forum/showth...2&page=1&pp=15

    Dozens of thanks.

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)