CChartScrollBar类用来针对每个轴的数据进行滚动,将那些不在当前区域内的数据通过滚动展示出来。

CChartScrollBar类的头文件。

#pragma once
class CChartAxis;
class CChartScrollBar : public CScrollBar
{
public:
CChartScrollBar(CChartAxis* pParentAxis);
~CChartScrollBar();
void CreateScrollBar(const CRect& PlottingRect);
void OnHScroll(UINT nSBCode, UINT nPos);
void OnVScroll(UINT nSBCode, UINT nPos);
void Refresh();
void SetEnabled(bool bEnabled) { m_bEnabled = bEnabled; }
bool GetEnabled() const { return m_bEnabled; }
void SetAutoHide(bool bAutoHide) { m_bAutoHide = bAutoHide; }
bool GetAutoHide() const { return m_bAutoHide; }
void OnMouseEnter();
void OnMouseLeave();
private:
bool IsScrollInverted() const;
void MoveAxisToPos(int PreviousPos, int CurPos);
CChartAxis* m_pParentAxis;
bool m_bEnabled;
bool m_bAutoHide;
};

CChartScrollBar类的源文件。

#include "stdafx.h"
#include "ChartScrollBar.h"
#include "ChartAxis.h"
#include "ChartCtrl.h"
#include "math.h"
CChartScrollBar::CChartScrollBar(CChartAxis* pParentAxis)
: CScrollBar(), m_pParentAxis(pParentAxis), m_bEnabled(false),
m_bAutoHide(true)
{
}
CChartScrollBar::~CChartScrollBar()
{
}
void CChartScrollBar::CreateScrollBar(const CRect& PlottingRect)
{
CRect Temp = PlottingRect;
Temp.top++; Temp.left++;
DWORD dwStyle = SBS_HORZ | WS_CHILD;
if (m_pParentAxis->IsHorizontal())
{
if (m_pParentAxis->m_bIsSecondary)
dwStyle |= SBS_TOPALIGN;
else
dwStyle += SBS_BOTTOMALIGN;
}
else
{
if (m_pParentAxis->m_bIsSecondary)
dwStyle |= SBS_VERT | SBS_RIGHTALIGN;
else
dwStyle += SBS_VERT | SBS_LEFTALIGN;
}
CScrollBar::Create(dwStyle, Temp, m_pParentAxis->m_pParent,);
SCROLLINFO info;
info.cbSize = sizeof(SCROLLINFO);
info.fMask = SIF_ALL;
info.nMin = ;
info.nMax = ;
info.nPage = ;
info.nPos = ;
CScrollBar::SetScrollInfo(&info);
}
bool CChartScrollBar::IsScrollInverted() const
{
bool bInverted = false;
if (m_pParentAxis->IsInverted() && m_pParentAxis->m_bIsHorizontal)
bInverted = true;
if (!m_pParentAxis->IsInverted() && !m_pParentAxis->m_bIsHorizontal)
bInverted = true;
return bInverted;
}
void CChartScrollBar::Refresh()
{
double AxisMin=, AxisMax=;
double SeriesMin=, SeriesMax=;
m_pParentAxis->GetMinMax(AxisMin,AxisMax);
m_pParentAxis->GetSeriesMinMax(SeriesMin,SeriesMax);
double dStep = ;
int iTotalSteps = ;
int iCurrentStep = ;
if (m_pParentAxis->IsLogarithmic())
{
// TODO: do something if the series has 0 in it
dStep = pow(AxisMax/AxisMin,0.1);
iTotalSteps = (int)ceil(log(SeriesMax/SeriesMin)/log(dStep));
iCurrentStep = (int)(log(AxisMin/SeriesMin)/log(dStep));
}
else
{
dStep = (AxisMax - AxisMin) / 10.0;
iTotalSteps = (int)ceil((SeriesMax - SeriesMin)/dStep);
iCurrentStep = (int)(iTotalSteps * ((AxisMin - SeriesMin)/(SeriesMax-SeriesMin)));
}
SCROLLINFO info;
info.cbSize = sizeof(SCROLLINFO);
info.fMask = SIF_ALL;
if ( (AxisMax-AxisMin) == || (SeriesMax-SeriesMin)== )
{
info.nMin = ;
info.nMax = ;
info.nPage = ;
info.nPos = ;
}
else
{
info.nMin = ;
info.nMax = iTotalSteps;
info.nPage = ;
info.nPos = iCurrentStep;
if (IsScrollInverted())
info.nPos = iTotalSteps - - iCurrentStep;
else
info.nPos = iCurrentStep;
}
CScrollBar::SetScrollInfo(&info);
}
void CChartScrollBar::OnHScroll(UINT nSBCode, UINT nPos)
{
int MinPos;
int MaxPos;
int PreviousPos = CScrollBar::GetScrollPos();
CScrollBar::GetScrollRange(&MinPos, &MaxPos);
int CurPos = PreviousPos;
bool bUpdate = true;
switch (nSBCode)
{
case SB_LEFT:
CurPos = ;
break;
case SB_RIGHT:
CurPos = MaxPos;
break;
case SB_ENDSCROLL:
bUpdate = false;
break;
case SB_LINELEFT:
if (CurPos > MinPos)
CurPos--;
break;
case SB_LINERIGHT:
if (CurPos < MaxPos-)
CurPos++;
break;
case SB_PAGELEFT:
if (CurPos > MinPos)
CurPos = max(MinPos, CurPos - );
break;
case SB_PAGERIGHT:
if (CurPos < MaxPos-)
CurPos = min(MaxPos, CurPos + );
break;
case SB_THUMBPOSITION:
CurPos = nPos;
break;
case SB_THUMBTRACK:
CurPos = nPos;
break;
}
if (bUpdate)
{
// Set the new position of the thumb (scroll box).
CScrollBar::SetScrollPos(CurPos);
MoveAxisToPos(PreviousPos,CurPos);
}
}
void CChartScrollBar::OnVScroll(UINT nSBCode, UINT nPos)
{
int MinPos;
int MaxPos;
int PreviousPos = CScrollBar::GetScrollPos();
CScrollBar::GetScrollRange(&MinPos, &MaxPos);
int CurPos = PreviousPos;
bool bUpdate = true;
switch (nSBCode)
{
case SB_BOTTOM:
CurPos = MaxPos;
break;
case SB_TOP:
CurPos = ;
break;
case SB_ENDSCROLL:
bUpdate = false;
break;
case SB_LINEDOWN:
if (CurPos < MaxPos-)
CurPos++;
break;
case SB_LINEUP:
if (CurPos > MinPos)
CurPos--;
break;
case SB_PAGEUP:
if (CurPos > MinPos)
CurPos = max(MinPos, CurPos - );
break;
case SB_PAGEDOWN:
if (CurPos < MaxPos-)
CurPos = min(MaxPos, CurPos + );
break;
case SB_THUMBPOSITION:
CurPos = nPos;
break;
case SB_THUMBTRACK:
CurPos = nPos;
break;
}
if (bUpdate)
{
// Set the new position of the thumb (scroll box).
CScrollBar::SetScrollPos(CurPos);
MoveAxisToPos(PreviousPos,CurPos);
}
}
void CChartScrollBar::MoveAxisToPos(int PreviousPos, int CurPos)
{
double AxisMin=, AxisMax=;
double SeriesMin=, SeriesMax=;
m_pParentAxis->GetMinMax(AxisMin,AxisMax);
m_pParentAxis->GetSeriesMinMax(SeriesMin,SeriesMax);
if (m_pParentAxis->IsLogarithmic())
{
double dStep = pow(AxisMax/AxisMin,0.1);
double dFactor = pow(dStep,(CurPos - PreviousPos));
if (IsScrollInverted())
m_pParentAxis->SetZoomMinMax(AxisMin/dFactor,AxisMax/dFactor);
else
m_pParentAxis->SetZoomMinMax(AxisMin*dFactor,AxisMax*dFactor);
}
else
{
double dStep = (AxisMax - AxisMin) / 10.0;
double dOffset = (CurPos - PreviousPos) * dStep;
if (IsScrollInverted())
m_pParentAxis->SetZoomMinMax(AxisMin-dOffset,AxisMax-dOffset);
else
m_pParentAxis->SetZoomMinMax(AxisMin+dOffset,AxisMax+dOffset);
}
}
void CChartScrollBar::OnMouseEnter()
{
if (m_bEnabled && m_bAutoHide)
ShowWindow(SW_SHOW);
}
void CChartScrollBar::OnMouseLeave()
{
if (m_bEnabled && m_bAutoHide)
ShowWindow(SW_HIDE);
}
这份源码一开始读的时候在info.nPage产生了理解偏差,再一次读的时候又在这个地方纠结了很久,现在把这个参数的意义再捋一遍,它的英文解释如下:
nPage 
Specifies the page size. A scroll bar uses this value to determine the appropriate size of the proportional scroll box. 
该值表示页尺寸,同时表示比例滚动框的大小。将这个理解清楚之后,后面OnHScroll函数和OnVScroll函数里面的加减10与加减9就相对比较好理解。

最新文章

  1. Android手机_软件01
  2. spread语法解析与使用
  3. atitit.sql server2008导出导入数据库大的表格文件... oracle mysql
  4. android自定义弹出框样式实现
  5. mysql linux 备份脚本
  6. 【caffe-windows】 caffe-master 之 mnist 超详细
  7. ylbtech-数据库设计与优化-对作为复选框/单选列表的集合表的设计
  8. 对于android拦截短信的一些疑问
  9. 基于ATmgea8单片机设计的加热控制系统(转)
  10. FreeRTOS 启动进程调度后,程序卡死的部分原因分析。
  11. Linux上安装Zookeeper以及一些注意事项
  12. React子组件和父组件通信
  13. Appium自动化学习1
  14. LVM : 快照
  15. datetime学习
  16. .NET本质论 组件
  17. IntelliJ IDEA 2017版 编译器使用学习笔记(六) (图文详尽版);IDE快捷键使用
  18. HDU 5083 Instruction(字符串处理)
  19. iOS - 互斥锁&amp;&amp;自旋锁 多线程安全隐患(转载)
  20. sql server 只读帐号设置能读取存储过程,view等内容。

热门文章

  1. SQL Server 创建唯一约束sql语句
  2. Android操作系统架构
  3. Linux虚拟机fdisk分区
  4. Django学习之 - 基础ORM
  5. InfluxDB useful commands
  6. linux 中断机制浅析
  7. HDU OJ Max sum 题目1003
  8. react 开发 PC 端项目(一)项目环境搭建 及 处理 IE8 兼容问题
  9. 杭电1232畅通project
  10. 使用JS对select标签进行联动选择