ChartCtrl源码剖析之——CChartGrid类

CChartGrid类用来绘制波形区域中的表格,当绘制波形时波形就显示在这些表格上面。它处于该控件的区域,如下图所示: 

CChartGrid类的头文件。

#if !defined(AFX_CHARTGRID_H__ECCBEFF4_2365_49CD_A865_F1B4DD8CA138__INCLUDED_)
#define AFX_CHARTGRID_H__ECCBEFF4_2365_49CD_A865_F1B4DD8CA138__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "ChartObject.h"
#include <list>
class CChartAxis;
class CChartGrid : public CChartObject  
{
    friend CChartAxis;
public:
    CChartGrid(CChartCtrl* pParent, bool bHoriz);
    virtual ~CChartGrid();
private:
    void Draw(CDC* pDC);
    void AddTick(int Position);
    void ClearTicks();
    std::list<int> m_vecTickPos;
    bool           m_bIsHorizontal;
};
#endif // !defined(AFX_CHARTGRID_H__ECCBEFF4_2365_49CD_A865_F1B4DD8CA138__INCLUDED_)

CChartGrid类的源文件。

#include "stdafx.h"
#include "ChartGrid.h"
#include "ChartAxis.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CChartGrid::CChartGrid(CChartCtrl* pParent, bool bHoriz)
  : CChartObject(pParent), m_bIsHorizontal(bHoriz), m_vecTickPos()
{
    m_ObjectColor = RGB(128,128,128);
}
CChartGrid::~CChartGrid()
{
}
void CChartGrid::AddTick(int Position)
{
    m_vecTickPos.push_back(Position);
}
void CChartGrid::ClearTicks()
{
    m_vecTickPos.clear();
}
void CChartGrid::Draw(CDC *pDC)
{
    if (!m_bIsVisible)
        return;
    if (!pDC->GetSafeHdc() )
        return;
    CPen* pOldPen;
    CPen NewPen(PS_SOLID,0,m_ObjectColor);
    pOldPen = pDC->SelectObject(&NewPen);
    list<int>::iterator iter = m_vecTickPos.begin();
    int ActuPosition = 0;
    for (iter; iter!=m_vecTickPos.end(); iter++)
    {
        ActuPosition = *iter;
        if (!m_bIsHorizontal)
        {
            int ActuX = m_ObjectRect.left;
            while (true)
            {
                if (!Clip(ActuX,ActuPosition))
                    break;
                pDC->MoveTo(ActuX,ActuPosition);
                ActuX += 3;
                Clip(ActuX,ActuPosition);
                pDC->LineTo(ActuX,ActuPosition);
                ActuX += 3;
            }
        }
        else
        {
            int ActuY = m_ObjectRect.bottom;
            while (true)
            {
                if (!Clip(ActuPosition,ActuY))
                    break;
                pDC->MoveTo(ActuPosition,ActuY);
                ActuY -= 3;
                Clip(ActuPosition,ActuY);
                pDC->LineTo(ActuPosition,ActuY);
                ActuY -= 3;
            }
        }
    }
    pDC->SelectObject(pOldPen);
    DeleteObject(NewPen);
}

这里简单说一下,m_bIsHorizontal用来表示轴的水平与垂直,一开始对这个地方的理解有偏差,当为垂直轴的时候死活没办法理解为什么要累加ActuX的值,因为这明明是横向的嘛。后来意识到绘制的是跟纵轴相关的时候,这个地方就特别好理解了。 

 

posted @ 2017-07-19 18:05  不安分的萝卜  阅读(1572)  评论(0编辑  收藏  举报