zoukankan      html  css  js  c++  java
  • ChartCtrl源码剖析之——CChartAxisLabel类

    CChartAxisLabel类用来绘制轴标签,上、下、左、右都可以根据实际需要设置对应的轴标签。它处于该控件的区域,如下图所示:

    CChartAxisLabel类的头文件。

    #if !defined(AFX_CHARTAXISLABEL_H__0E5519C8_A2F4_4CED_9681_32A56B25D0C5__INCLUDED_)
    #define AFX_CHARTAXISLABEL_H__0E5519C8_A2F4_4CED_9681_32A56B25D0C5__INCLUDED_
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    #include "ChartObject.h"
    #include "ChartString.h"
    class CChartAxis;
    class CChartAxisLabel : public CChartObject  
    {
        friend CChartAxis;
    public:
        void SetText(const TChartString& NewText);
        TChartString GetText() const        { return m_strLabelText;    }
        void SetFont(int nPointSize, const TChartString& strFaceName);
        CChartAxisLabel(CChartCtrl* pParent, bool bHorizontal);
        virtual ~CChartAxisLabel();
    private:
        void SetPosition(int LeftBorder, int TopBorder, CDC *pDC);
        void Draw(CDC* pDC);
        CSize GetSize(CDC* pDC) const;
        bool m_bIsHorizontal;      // Specifies if the axis is horizontal or not
        int  m_iFontSize;
        TChartString m_strFontName;
        TChartString m_strLabelText;
    };
    #endif // !defined(AFX_CHARTAXISLABEL_H__0E5519C8_A2F4_4CED_9681_32A56B25D0C5__INCLUDED_)

    CChartAxisLabel类的源文件。

    #include "stdafx.h"
    #include "ChartAxisLabel.h"
    #include "ChartCtrl.h"
    #ifdef _DEBUG
    #undef THIS_FILE
    static char THIS_FILE[]=__FILE__;
    #define new DEBUG_NEW
    #endif
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    CChartAxisLabel::CChartAxisLabel(CChartCtrl* pParent, bool bHorizontal):CChartObject(pParent)
    {
        m_bIsHorizontal = bHorizontal;
        m_iFontSize = 100;
        m_strFontName = _T("Microsoft Sans Serif");
        m_strLabelText = _T("");
    }
    CChartAxisLabel::~CChartAxisLabel()
    {
    }
    void CChartAxisLabel::SetText(const TChartString& NewText)  
    {
        m_strLabelText = NewText;
        m_pParent->RefreshCtrl();
    }
    void CChartAxisLabel::SetFont(int nPointSize, const TChartString& strFaceName)
    {
        m_iFontSize = nPointSize;
        m_strFontName = strFaceName;
        m_pParent->RefreshCtrl();
    }
    CSize CChartAxisLabel::GetSize(CDC *pDC) const
    {
        CSize LabelSize;
        LabelSize.cx = 0;
        LabelSize.cy = 0;
        if (!m_bIsVisible)
            return LabelSize;
        if (!pDC->GetSafeHdc())
            return LabelSize;
        if (m_strLabelText == _T(""))
            return LabelSize;
        CFont NewFont;
        CFont* pOldFont;
        NewFont.CreatePointFont(m_iFontSize,m_strFontName.c_str(),pDC);
        pOldFont = pDC->SelectObject(&NewFont);
        LabelSize = pDC->GetTextExtent(m_strLabelText.c_str());
        LabelSize.cx += 4;
        LabelSize.cy += 4;
        if (!m_bIsHorizontal)
        {
            int Width = LabelSize.cy;
            int Height = LabelSize.cx;
            LabelSize.cx = Width;
            LabelSize.cy = Height;
        }
        pDC->SelectObject(pOldFont);
        DeleteObject(NewFont);
        return LabelSize;
    }
    void CChartAxisLabel::Draw(CDC *pDC)
    {
        if (!m_bIsVisible)
            return;
        if (!pDC->GetSafeHdc())
            return;
        if (m_strLabelText == _T(""))
            return;
        int iPrevMode = pDC->SetBkMode(TRANSPARENT);
        COLORREF OldColor = pDC->SetTextColor(m_ObjectColor);
        CFont NewFont;
        NewFont.CreatePointFont(m_iFontSize,m_strFontName.c_str(),pDC);
        CFont* pOldFont;
        if (!m_bIsHorizontal)
        {
            LOGFONT LogFont;
            NewFont.GetLogFont(&LogFont);
            LogFont.lfOrientation = 900;
            LogFont.lfEscapement = 900;
            CFont VertFont;
            VertFont.CreateFontIndirect(&LogFont);
            pOldFont = pDC->SelectObject(&VertFont);
            pDC->ExtTextOut(m_ObjectRect.left + 2,m_ObjectRect.top,
                        ETO_CLIPPED,NULL,m_strLabelText.c_str(),NULL);
            pDC->SelectObject(pOldFont);
            DeleteObject(VertFont);
            DeleteObject(NewFont);
        }
        else
        {        
            pOldFont = pDC->SelectObject(&NewFont);
            pDC->ExtTextOut(m_ObjectRect.left,m_ObjectRect.top + 2,
                        ETO_CLIPPED,NULL,m_strLabelText.c_str(),NULL);
            pDC->SelectObject(pOldFont);
            DeleteObject(NewFont);
        }
        pDC->SetBkMode(iPrevMode);
        pDC->SetTextColor(OldColor);
    }
    void CChartAxisLabel::SetPosition(int LeftBorder, int TopBorder, CDC *pDC)
    {
        CSize NewSize = GetSize(pDC);
        CRect NewRect;
        NewRect.top = TopBorder;
        NewRect.bottom = TopBorder + NewSize.cy;
        NewRect.left = LeftBorder;
        NewRect.right = LeftBorder + NewSize.cx;
        CChartObject::SetRect(NewRect);
    }

    CChartAxisLabel类在CChartAxis类中设定最终绘制的位置。 

    作者:常想一二
    出处:http://www.cnblogs.com/wolfmvp/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    如果文中有什么错误,欢迎指出。以免更多的人被误导。
  • 相关阅读:
    饿了么P7级前端工程师进入大厂的面试经验
    前端程序员面试的坑,简历写上这一条信息会被虐死!
    这次来分享前端的九条bug吧
    移动端开发必会出现的问题和解决方案
    创建一个dynamics 365 CRM online plugin (八)
    创建一个dynamics 365 CRM online plugin (七)
    创建一个dynamics 365 CRM online plugin (六)
    创建一个dynamics 365 CRM online plugin (五)
    使用User Primary Email作为GUID的问题
    怎样Debug Dynamics 365 CRM Plugin
  • 原文地址:https://www.cnblogs.com/wolfmvp/p/7207073.html
Copyright © 2011-2022 走看看