DOCX

HeaderCtrlClcpp

By Sandra Reed,2014-09-22 16:19
8 views 0
HeaderCtrlClcppHeader

// HeaderCtrlCl.cpp : º?Ì??t

    //

#include "stdafx.h"

    //#include "DemoList.h"

    #include "HeaderCtrlCl.h"

// CHeaderCtrlCl

IMPLEMENT_DYNAMIC(CHeaderCtrlCl, CHeaderCtrl)

    CHeaderCtrlCl::CHeaderCtrlCl() : m_R(207)

    , m_G(207)

    , m_B(207)

    , m_Gradient(2)

    {

     m_Format = "";

     m_Height = 1;

     m_fontHeight = 15;

     m_fontWith = 0;

     m_color = RGB(0,0,0);

    }

    CHeaderCtrlCl::~CHeaderCtrlCl() {

    }

BEGIN_MESSAGE_MAP(CHeaderCtrlCl, CHeaderCtrl)

     ON_WM_PAINT()

     ON_MESSAGE(HDM_LAYOUT, OnLayout) END_MESSAGE_MAP()

// CHeaderCtrlCl

    void CHeaderCtrlCl::OnPaint() {

     CPaintDC dc(this); // device context for painting

     // When it isn't a painting message,call CHeaderCtrl::OnPaint()

     int nItem;

     nItem = GetItemCount();//get the number of items

     for(int i = 0; i<nItem;i ++)

     {

     CRect tRect;

     GetItemRect(i,&tRect);

     int R = m_R,G = m_G,B = m_B;

     CRect nRect(tRect);//copy the size to a new container

     nRect.left++;

     //draw solid background

     for(int j = tRect.top;j<=tRect.bottom;j++)

     {

     nRect.bottom = nRect.top+1;

     CBrush _brush;

     _brush.CreateSolidBrush(RGB(R,G,B));

     dc.FillRect(&nRect,&_brush);

     _brush.DeleteObject();

     R-=m_Gradient;G-=m_Gradient;B-=m_Gradient;

     if (R<0)R = 0;

     if (G<0)G = 0;

     if (B<0)B= 0;

     nRect.top = nRect.bottom;

     }

     dc.SetBkMode(TRANSPARENT);

     CFont nFont ,* nOldFont;

     //dc.SetTextColor(RGB(250,50,50));

     dc.SetTextColor(m_color);

     nFont.CreateFont(m_fontHeight,m_fontWith,0,0,0,FALSE,FALSE,0,0,0,0,0,0,_TEXT("SimSun")

    );

     nOldFont = dc.SelectObject(&nFont);

     UINT nFormat = 1;

     if (m_Format[i]=='0')

     {

     nFormat = DT_LEFT;

     tRect.left+=3;

     }

     else if (m_Format[i]=='1')

     {

     nFormat = DT_CENTER;

     }

     else if (m_Format[i]=='2')

     {

     nFormat = DT_RIGHT;

     tRect.right-=3;

     }

     TEXTMETRIC metric;

     dc.GetTextMetrics(&metric);

     int ofst = 0;

     ofst = tRect.Height() - metric.tmHeight;

     tRect.OffsetRect(0,ofst/2);

     dc.DrawText(m_HChar[i],&tRect,nFormat);

     dc.SelectObject(nOldFont);

     nFont.DeleteObject();

     }

     //draw the rest part of the header

     CRect rtRect;

     CRect clientRect;

     GetItemRect(nItem - 1,rtRect);

     GetClientRect(clientRect);

     rtRect.left = rtRect.right+1;

     rtRect.right = clientRect.right;

     int R = m_R,G = m_G,B = m_B;

     CRect nRect(rtRect);

     //draw solid background

     for(int j = rtRect.top;j<=rtRect.bottom;j++)

     {

     nRect.bottom = nRect.top+1;

     CBrush _brush;

     _brush.CreateSolidBrush(RGB(R,G,B));

     dc.FillRect(&nRect,&_brush);

     _brush.DeleteObject();

     R-=m_Gradient;G-=m_Gradient;B-=m_Gradient;

     if (R<0)R = 0;

     if (G<0)G = 0;

     if (B<0)B= 0;

     nRect.top = nRect.bottom;

     }

    }

    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;

     //the height is 1.5 times what it was.

     //Set 1.5 to be a global variable if you want to modify it dynamically

     int nHeight = (int)(pwpos->cy * m_Height);

     pwpos->cy = nHeight;

     prc->top = nHeight;

     return lResult;

    }

Report this document

For any questions or suggestions please email
cust-service@docsford.com