ChartCtrl源码剖析之——CChartLegend类
CChartLegend类用来绘制每一个波形的描述信息,它处于该控件的区域,如下图所示:

CChartLegend类的头文件。
#if !defined(AFX_CHARTLEGEND_H__CD72E5A0_8F52_472A_A611_C588F642080B__INCLUDED_)
#define AFX_CHARTLEGEND_H__CD72E5A0_8F52_472A_A611_C588F642080B__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "ChartObject.h"
#include "ChartCtrl.h"
#include "ChartString.h"
class CChartSerie;
class CChartLegend : public CChartObject
{
friend CChartCtrl;
public:
void SetFont(int iPointSize, const TChartString& strFaceName);
CChartLegend(CChartCtrl* pParent);
virtual ~CChartLegend();
enum DockSide
{
dsDockRight,
dsDockLeft,
dsDockTop,
dsDockBottom
};
void DockLegend(DockSide dsSide);
void UndockLegend(int iLeftPos, int iTopPos);
void SetTransparent(bool bTransparent);
void SetHorizontalMode(bool bHorizontal);
private:
void Draw(CDC* pDC);
void ClipArea(CRect& rcControl, CDC* pDC);
void UpdatePosition(CDC* pDC, const CRect& rcControl);
TChartString m_strFontName;
int m_iFontSize;
bool m_bDocked; // true if the legend is docked
DockSide m_DockSide;
// If the legend is not docked:
int m_iLeftPos;
int m_iTopPos;
bool m_bIsTransparent;
bool m_bIsHorizontal;
CSize m_BitmapSize;
};
#endif // !defined(AFX_CHARTLEGEND_H__CD72E5A0_8F52_472A_A611_C588F642080B__INCLUDED_)
CChartLegend类的源文件。
#include "stdafx.h"
#include "ChartLegend.h"
#include "ChartSerie.h"
#include "ChartCtrl.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CChartLegend::CChartLegend(CChartCtrl* pParent):CChartObject(pParent)
{
m_ObjectColor = RGB(,,);
m_iFontSize = ;
m_strFontName = _T("Times New Roman");
m_bIsVisible = false;
m_bDocked = true;
m_DockSide = dsDockRight;
m_iLeftPos = m_iTopPos = ;
m_bIsTransparent = false;
m_bIsHorizontal = false;
m_bShadow = true;
m_iShadowDepth = ;
m_BitmapSize.cx = ;
m_BitmapSize.cy = ;
}
CChartLegend::~CChartLegend()
{
}
void CChartLegend::SetFont(int iPointSize, const TChartString& strFaceName)
{
m_iFontSize = iPointSize;
m_strFontName = strFaceName;
m_pParent->RefreshCtrl();
}
void CChartLegend::SetTransparent(bool bTransparent)
{
m_bIsTransparent = bTransparent;
m_pParent->RefreshCtrl();
}
void CChartLegend::SetHorizontalMode(bool bHorizontal)
{
m_bIsHorizontal = bHorizontal;
m_pParent->RefreshCtrl();
}
void CChartLegend::DockLegend(DockSide dsSide)
{
m_bDocked = true;
m_DockSide = dsSide;
m_pParent->RefreshCtrl();
}
void CChartLegend::UndockLegend(int iLeftPos, int iTopPos)
{
m_bDocked = false;
m_iLeftPos = iLeftPos;
m_iTopPos = iTopPos;
m_pParent->RefreshCtrl();
}
void CChartLegend::ClipArea(CRect& rcControl, CDC* pDC)
{
UpdatePosition(pDC,rcControl);
if (m_ObjectRect.IsRectEmpty())
return;
if (m_bDocked)
{
switch (m_DockSide)
{
case dsDockRight:
rcControl.right = m_ObjectRect.left + ;
break;
case dsDockLeft:
rcControl.left = m_ObjectRect.right - ;
break;
case dsDockTop:
rcControl.top = m_ObjectRect.bottom + ;
break;
case dsDockBottom:
rcControl.bottom = m_ObjectRect.top - ;
break;
}
}
}
void CChartLegend::UpdatePosition(CDC* pDC, const CRect& rcControl)
{
CRect NewPosition;
NewPosition.SetRectEmpty();
if (!m_bIsVisible)
{
SetRect(NewPosition);
return;
}
CFont* pOldFont;
CFont NewFont;
NewFont.CreatePointFont(m_iFontSize,m_strFontName.c_str(),pDC);
pOldFont = pDC->SelectObject(&NewFont);
int Height = ;
int Width = ;
int MaxText = ;
CSize TextSize;
size_t SeriesCount = m_pParent->GetSeriesCount();
int Drawn = ;
for (size_t i=;i<SeriesCount;i++)
{
CChartSerie* pSerie = m_pParent->GetSerie(i);
if ( (pSerie->GetName() == _T("")) || !pSerie->IsVisible() )
continue;
Drawn++;
TextSize = pDC->GetTextExtent(pSerie->GetName().c_str());
if (!m_bIsHorizontal)
{
if (TextSize.cy>m_BitmapSize.cy)
Height += TextSize.cy + ;
else
Height += m_BitmapSize.cy + ;
if (TextSize.cx > MaxText)
MaxText = TextSize.cx;
}
else
{
Width += TextSize.cx + + m_BitmapSize.cx + ;
if (TextSize.cy > MaxText)
MaxText = TextSize.cy;
}
}
pDC->SelectObject(pOldFont);
DeleteObject(NewFont);
if (!Drawn)
{
SetRect(NewPosition);
return;
}
if (!m_bIsHorizontal)
{
Width += MaxText + m_BitmapSize.cx + ;
Height += + - ; // Top and bottom margins. -2 because space counted once too much
}
else
{
Width += + - ;
Height = + max(m_BitmapSize.cy,MaxText) + ;
} if (!m_bDocked)
{
NewPosition.top = m_iTopPos;
NewPosition.left = m_iLeftPos;
NewPosition.bottom = m_iTopPos + Height + ;
NewPosition.right = m_iLeftPos + Width;
}
else
{
switch (m_DockSide)
{
case dsDockRight:
NewPosition.top = ((rcControl.bottom-rcControl.top)/) - ((Height + )/);
NewPosition.left = rcControl.right - (Width + );
NewPosition.bottom = NewPosition.top + Height;
NewPosition.right = NewPosition.left + Width;
break;
case dsDockLeft:
NewPosition.top = ((rcControl.bottom-rcControl.top)/) - ((Height + )/);
NewPosition.left = rcControl.left + ;
NewPosition.bottom = NewPosition.top + Height;
NewPosition.right = NewPosition.left + Width;
break;
case dsDockTop:
NewPosition.top = rcControl.top + ; //((rcControl.bottom-rcControl.top)/2) - ((Height + 2)/2);
NewPosition.left = ((rcControl.right-rcControl.left)/) - (Width/); // rcControl.left + 3;
NewPosition.bottom = NewPosition.top + Height;
NewPosition.right = NewPosition.left + Width;
break;
case dsDockBottom:
NewPosition.top = rcControl.bottom - (Height + ); //((rcControl.bottom-rcControl.top)/2) - ((Height + 2)/2);
NewPosition.left = ((rcControl.right-rcControl.left)/) - (Width/); // rcControl.left + 3;
NewPosition.bottom = NewPosition.top + Height;
NewPosition.right = NewPosition.left + Width;
break;
}
}
SetRect(NewPosition);
}
void CChartLegend::Draw(CDC *pDC)
{
if (!pDC->GetSafeHdc())
return;
if (!m_bIsVisible)
return;
if (m_ObjectRect.IsRectEmpty())
return;
CPen SolidPen(PS_SOLID,,RGB(,,));
CPen* pOldPen;
CFont* pOldFont;
CFont NewFont;
NewFont.CreatePointFont(m_iFontSize,m_strFontName.c_str(),pDC);
// Draw the shadow
if (m_bShadow)
{
CRect ShadowRect = m_ObjectRect;
ShadowRect.OffsetRect(m_iShadowDepth,m_iShadowDepth);
CBrush BrushShadow;
BrushShadow.CreateSolidBrush(m_ShadowColor) ;
pDC->FillRect(ShadowRect,&BrushShadow);
}
if (!m_bIsTransparent)
{
//Fill back color
CBrush BrushBack;
BrushBack.CreateSolidBrush(m_ObjectColor) ;
pDC->FillRect(m_ObjectRect,&BrushBack);
}
pOldFont = pDC->SelectObject(&NewFont);
pOldPen = pDC->SelectObject(&SolidPen);
//Draw rectangle:
pDC->MoveTo(m_ObjectRect.left,m_ObjectRect.top);
pDC->LineTo(m_ObjectRect.right,m_ObjectRect.top);
pDC->LineTo(m_ObjectRect.right,m_ObjectRect.bottom);
pDC->LineTo(m_ObjectRect.left,m_ObjectRect.bottom);
pDC->LineTo(m_ObjectRect.left,m_ObjectRect.top);
int iPrevMode = pDC->SetBkMode(TRANSPARENT);
CRect rectBitmap(m_ObjectRect.left+,m_ObjectRect.top+,
m_ObjectRect.left++m_BitmapSize.cx,
m_ObjectRect.top++m_BitmapSize.cy);
int SeriesCount = m_pParent->GetSeriesCount();
for (int i=;i<SeriesCount;i++)
{
CChartSerie* pSerie = m_pParent->GetSerie(i);
if ( (pSerie->GetName() == _T("")) || !pSerie->IsVisible() )
continue;
int MaxHeight = ;
CSize TextSize = pDC->GetTextExtent(pSerie->GetName().c_str());
if (TextSize.cy > m_BitmapSize.cy)
{
pDC->ExtTextOut(rectBitmap.right+,rectBitmap.top,ETO_CLIPPED,NULL,pSerie->GetName().c_str(),NULL);
CRect rectTemp(rectBitmap);
int YOffset = TextSize.cy/ - rectBitmap.Height()/;
rectTemp.OffsetRect(,YOffset);
pSerie->DrawLegend(pDC,rectTemp);
MaxHeight = TextSize.cy;
}
else
{
int YOffset = rectBitmap.CenterPoint().y - TextSize.cy/;
pDC->ExtTextOut(rectBitmap.right+,YOffset,ETO_CLIPPED,NULL,pSerie->GetName().c_str(),NULL);
MaxHeight = m_BitmapSize.cy;
pSerie->DrawLegend(pDC,rectBitmap);
} if (!m_bIsHorizontal)
rectBitmap.OffsetRect(,MaxHeight+);
else
rectBitmap.OffsetRect(m_BitmapSize.cx++TextSize.cx+,);
}
pDC->SetBkMode(iPrevMode);
pDC->SelectObject(pOldFont);
DeleteObject(NewFont);
pDC->SelectObject(pOldPen);
DeleteObject(SolidPen);
}
ClipArea函数的作用是将ChartLegend与波形绘制区域分离开,其中rcControl表示的是绘制波形的区域,m_ObjectRect表示的是绘制ChartLegend的区域。UpdatePosition函数用来在当前绘制区域里面计算ChartLegend所处的位置并记录下该位置。Draw函数用来绘制ChartLegend控件,调用CChartSeries类的DrawLegend用来绘制ChartLegend里面的波形图像信息。
ChartCtrl源码剖析之——CChartLegend类的更多相关文章
- ChartCtrl源码剖析之——CChartObject类
首先,做一些简单的铺垫,目前针对ChartCtrl源码的剖析只针对V.15版本.名义上说是剖析,倒不如说是记录下自己针对该控件的理解,非常感谢Cedric Moonen大神,一切的功劳与掌声都该赠予给 ...
- ChartCtrl源码剖析之——CChartScrollBar类
CChartScrollBar类用来针对每个轴的数据进行滚动,将那些不在当前区域内的数据通过滚动展示出来. CChartScrollBar类的头文件. #pragma once class CChar ...
- ChartCtrl源码剖析之——CChartAxis类
CChartAxis类用来绘制波形控件的坐标轴,这个源码相对较复杂,当初阅读的时候耗费了不少精力来理解源码中的一些实现细节. CChartAxis类的头文件. #if !defined(AFX_CHA ...
- ChartCtrl源码剖析之——CChartTitle类
CChartTitle类顾名思义,该类用来绘制波形控件的标题,它处于该控件的区域,如下图所示: CChartTitle类的头文件. #if !defined(AFX_CHARTTITLE_H__499 ...
- ChartCtrl源码剖析之——CChartAxisLabel类
CChartAxisLabel类用来绘制轴标签,上.下.左.右都可以根据实际需要设置对应的轴标签.它处于该控件的区域,如下图所示: CChartAxisLabel类的头文件. #if !defined ...
- ChartCtrl源码剖析之——CChartGrid类
CChartGrid类用来绘制波形区域中的表格,当绘制波形时波形就显示在这些表格上面.它处于该控件的区域,如下图所示: CChartGrid类的头文件. #if !defined(AFX_CHARTG ...
- PART(Persistent Adaptive Radix Tree)的Java实现源码剖析
论文地址 Adaptive Radix Tree: https://db.in.tum.de/~leis/papers/ART.pdf Persistent Adaptive Radix Tree: ...
- 老李推荐:第6章3节《MonkeyRunner源码剖析》Monkey原理分析-事件源-事件源概览-命令翻译类
老李推荐:第6章3节<MonkeyRunner源码剖析>Monkey原理分析-事件源-事件源概览-命令翻译类 每个来自网络的字串命令都需要进行解析执行,只是有些是在解析的过程中直接执行 ...
- WorldWind源码剖析系列:影像存储类ImageStore、Nlt影像存储类NltImageStore和WMS影像存储类WmsImageStore
影像存储类ImageStore 影像存储类ImageStore提供了计算本地影像路径和远程影像影像URL访问的各种接口,是WmsImageStore类和NltImageStore类的基类.当划分完层次 ...
随机推荐
- Java多线程干货系列—(四)volatile关键字
原文地址:http://tengj.top/2016/05/06/threadvolatile4/ <h1 id="前言"><a href="#前言&q ...
- 匈牙利游戏(codevs 1269)
题目描述 Description Welcome to the Hungary Games! The streets of Budapest form a twisted network of one ...
- POJ1256 Anagram
Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %lld & %llu Submit Status Descript ...
- DNS Prefetch 【DNS 预解析技术】
DNS 实现域名到IP的映射.通过域名访问站点,每次请求都要做DNS解析.目前每次DNS解析,通常在200ms以下.针对DNS解析耗时问题,一些浏览器通过DNS Prefetch 来提高访问的流畅性. ...
- 洛谷——P1451 求细胞数量
P1451 求细胞数量 题目描述 一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右若还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数.(1<=m,n<=10 ...
- 3469 [POI2008]BLO-Blockade
洛谷—— P3469 [POI2008]BLO-Blockade 题目描述 There are exactly towns in Byteotia. Some towns are connected ...
- Spring的JDBC框架概述
以下内容引用自http://wiki.jikexueyuan.com/project/spring/jdbc-framework.html: 在使用普通的JDBC操作数据库时,就会很麻烦的写很多不必要 ...
- Go和HTTPS(TLS)
原文链接: http://studygolang.com/wr?u=http%3a%2f%2ftonybai.com%2f2015%2f04%2f30%2fgo-and-https%2f 近期在构思一 ...
- 一句话从MySQL导出CSV文件
mysql -h <host> -u<user> -p<passport> crm -e "select ....." | csvcut -t ...
- JAVA数组去除重复数据
一.用List集合实现 , , , , , , ,}; List<Integer> list = new ArrayList<Integer>(); ; i<str. ...