QQ登录

只需一步,快速开始

上位机MFC扩展编程图表图例添加单选复选框及自定义内容

[ 复制链接 ]
上位机MFC扩展编程图表图例添加单选复选框及自定义内容

前面实例介绍图表中图例元素的管理,元素如标题,网格等都是自带的。
这个实例实现在图例中添加自定义的内容,实现添加自定义三行内容,
第一行为单列,第二行为三列,第三行为三列,列为单选框与复选框控件。
效果如下图:
2020-01-09_190214.jpg

例程是在前面例程基础上创建,所以会带有几个图例元素操作按钮。
例程实现的是在图例上添加自定义内容,如图例尾部添加的三行自定义控件。
其中最后一行带有单选框与两个复选框。
点击单选框或复选框会触发消息,弹出对话框。

这里例程的关键点为:
图例成功创建后会发送消息BCGM_ON_CHART_LEGEND_CONTENT_CREATED。
所以我们可以关联消息处理函数,在函数内向图例添加自定义内容。
ON_REGISTERED_MESSAGE(BCGM_ON_CHART_LEGEND_CONTENT_CREATED, OnLegendContentCreated)

由于单选框,复选框要处理自绘功能与鼠标选择等事件,所以例程创建了两个集成类来管理,还有一个类为父类。
控件选择事件通过定义用户消息来发送。

  1. CMy123View* g_View = NULL;
  2. #define UM_CHECKCLICKED WM_USER+1
  3. #define UM_RADIOCLICKED WM_USER+2
  4. class CChartLegendControl : public CBCGPChartLegendCell
  5. {
  6. protected:
  7.         BOOL                                                m_bIsSelected;
  8. public:
  9.         DECLARE_DYNAMIC(CChartLegendControl);

  10.         CChartLegendControl(const CString& strName,BOOL bChecked) :
  11.         CBCGPChartLegendCell(strName)
  12.         {
  13.                 m_bIsSelected = bChecked;
  14.         }
  15.         void SetSelected(BOOL bSet)
  16.         {
  17.                 m_bIsSelected = bSet;
  18.                 Redraw();
  19.         }
  20.         void SetSelected()
  21.         {
  22.                 m_bIsSelected = !m_bIsSelected;
  23.                 Redraw();
  24.         }
  25.         BOOL IsSelected() const
  26.         {
  27.                 return m_bIsSelected;
  28.         }

  29.         virtual CBCGPSize OnCalcCustomCellSize(CBCGPGraphicsManager* pGM)
  30.         {
  31.                 CBCGPSize size = CBCGPChartLegendCell::OnCalcCustomCellSize(pGM);
  32.                 size.cy += 4;
  33.                 size.cx += size.cy + size.cy / 4.0;
  34.                 return size;
  35.         }

  36.         virtual void OnDrawCheckRadio(CBCGPGraphicsManager* pGM, const CBCGPRect& rect, const CBCGPColor& color) = 0;
  37.         //由库调用,控件自绘;
  38.         virtual void OnDrawCustomCell(CBCGPGraphicsManager* pGM, const CBCGPRect& rect)
  39.         {
  40.                 CBCGPRect rectCheck = rect;
  41.                 rectCheck.right = rectCheck.left + rectCheck.Height();

  42.                 CBCGPRect rectLabel = rect;
  43.                 rectLabel.left = rectCheck.right + rectCheck.Height() / 4.0;

  44.                 rectCheck.DeflateRect(2, 2);

  45.                 OnDrawCheckRadio(pGM, rectCheck, m_cellParams.m_brTextColor.GetColor());

  46.                 CBCGPChartLegendCell::OnDrawCustomCell(pGM, rectLabel);
  47.         }
  48. };


  49. class CChartLegendCheckBox : public CChartLegendControl
  50. {
  51. public:
  52.         DECLARE_DYNAMIC(CChartLegendCheckBox);

  53.         CChartLegendCheckBox(const CString& strName,BOOL bChecked=FALSE) :
  54.         CChartLegendControl(strName,bChecked)
  55.         {
  56.         }

  57.         virtual void OnMouseUp(const CBCGPPoint& pt)
  58.         {
  59.                 CBCGPChartLegendCell::OnMouseUp(pt);
  60.                 if(this->IsSelected())
  61.                         return;
  62.                 SetSelected();
  63.                 PostMessage(g_View->m_hWnd,UM_CHECKCLICKED,(WPARAM)0,(LPARAM)IsSelected() );
  64.         }
  65.         //绘制单选框;
  66.         virtual void OnDrawCheckRadio(CBCGPGraphicsManager* pGM, const CBCGPRect& rect, const CBCGPColor& color)
  67.         {
  68.                 pGM->DrawCheckBox(rect, m_bIsSelected, color);
  69.         }
  70. };

  71. class CChartLegendRadioButton : public CChartLegendControl
  72. {
  73. public:
  74.         DECLARE_DYNAMIC(CChartLegendRadioButton);

  75.         CChartLegendRadioButton(const CString& strName,BOOL bChecked=TRUE) :
  76.         CChartLegendControl(strName,bChecked)
  77.         {
  78.         }

  79.         virtual void OnMouseUp(const CBCGPPoint& pt)
  80.         {
  81.                 CBCGPChartLegendCell::OnMouseUp(pt);
  82.                 if(this->IsSelected())
  83.                         return;

  84.                 CBCGPChartLegendVisualObject* pLegend = this->GetParentLegend();
  85.                 int nCheckedIndex=0;
  86.                 int nCount = pLegend->GetLegendEntryCount();
  87.                 for(int i=0; i<nCount; i++)
  88.                 {
  89.                         CBCGPChartLegendEntry* pEntry = pLegend->GetLegendEntry(i);
  90.                         int n=pEntry->GetCellCount();
  91.                         for(int y=0; y<n; y++)
  92.                         {
  93.                                 CChartLegendRadioButton* pRadio = DYNAMIC_DOWNCAST(CChartLegendRadioButton,pEntry->GetCell(y));
  94.                 if(pRadio != NULL)
  95.                                 {
  96.                                         pRadio->SetSelected(FALSE);
  97.                                         if(pRadio == this)
  98.                                                 nCheckedIndex = y-1;//实例中添加有1单选框,所以这里减1;
  99.                                 }
  100.                         }
  101.                 }
  102.                 this->SetSelected(TRUE);
  103.                 PostMessage(g_View->m_hWnd,UM_RADIOCLICKED,(WPARAM)0,(LPARAM)nCheckedIndex);               
  104.         }

  105.         virtual void OnDrawCheckRadio(CBCGPGraphicsManager* pGM, const CBCGPRect& rect, const CBCGPColor& color)
  106.         {
  107.                 pGM->DrawRadioButton(rect, m_bIsSelected, color);
  108.         }
  109. };


  110. IMPLEMENT_DYNAMIC(CChartLegendControl, CBCGPChartLegendCell)
  111. IMPLEMENT_DYNAMIC(CChartLegendCheckBox, CChartLegendControl)
  112. IMPLEMENT_DYNAMIC(CChartLegendRadioButton, CChartLegendControl)
复制代码
这样我们要处理单选框,复选框选择消息,就可以关联两消息UM_CHECKCLICKED,UM_RADIOCLICKED处理函数。
另外这两消息是向视窗类发送,所以例程还定义了视窗类的全局变量CMy123View* g_View = NULL;
记得初始化它。

准备好后,就可以处理消息BCGM_ON_CHART_LEGEND_CONTENT_CREATED关联的函数OnLegendContentCreated了。
  1. //图例创建或内容更新后发送消息BCGM_ON_CHART_LEGEND_CONTENT_CREATED;
  2. //更新时要重新创建内容,所以下面要重新NEW;
  3. LRESULT CMy123View::OnLegendContentCreated(WPARAM, LPARAM lp)
  4. {
  5.         CBCGPChartLegendVisualObject* pLegend = (CBCGPChartLegendVisualObject*)lp;
  6.         ASSERT_VALID(pLegend);

  7.         BCGPChartCellParams params;
  8.         params.m_brContentFill = CBCGPBrush(CBCGPColor::LightSalmon, CBCGPColor::Wheat, CBCGPBrush::BCGP_GRADIENT_PIPE_HORIZONTAL);
  9.         params.m_brTextColor   = CBCGPBrush(CBCGPColor::DarkRed);;
  10.         params.SetContentPadding(CBCGPSize(10.0, 2.0));

  11.         // 添加自定义行1;
  12.         CBCGPChartLegendEntry* pEntry = new CBCGPChartLegendEntry(m_pLegend);
  13.         CBCGPChartLegendCell* pCell = new CBCGPChartLegendCell(_T("自定义行1"));
  14.         pCell->SetCellParams(params);
  15.         pEntry->AddLegendCell(pCell);
  16.         pLegend->InsertLegendEntry(pEntry, pLegend->GetLegendEntryCount());

  17.         // 添加自定义行2;
  18.         pEntry = new CBCGPChartLegendEntry(m_pLegend);
  19.         pEntry->AddLegendCell(new CBCGPChartLegendCell(_T("自定义行2")));
  20.         pEntry->AddLegendCell(new CBCGPChartLegendCell(_T("行2列1")));
  21.         pEntry->AddLegendCell(new CBCGPChartLegendCell(_T("行2列2")));
  22.         pLegend->AddLegendEntry(pEntry);

  23.         // 添加自定义行3;
  24.         pEntry = new CBCGPChartLegendEntry(m_pLegend);
  25.         pEntry->AddLegendCell(new CChartLegendCheckBox(_T("单选框")));
  26.         pEntry->AddLegendCell(new CChartLegendRadioButton(_T("复选框1"),TRUE));
  27.         pEntry->AddLegendCell(new CChartLegendRadioButton(_T("复选框2"),FALSE));
  28.         pLegend->AddLegendEntry(pEntry);
  29.         return 0;
  30. }
复制代码
例程中关于图表的创建可以参考前面帖子介绍
例程中使用到的扩展库,可以搜索BCGP下载使用。
例程源代码下载:
请点击此处下载

请先注册会员后在进行下载

已注册会员,请先登录后下载

文件名称:上位机MFC扩展编程图表图例添加单选复选框及自定义内容.rar 
文件大小:83.43 KB  售价:3金币
下载权限: 不限 以上或 VIP会员   [购买捐助会员]   [充值积分]   有问题联系我


  

如果您认可,可联系功能定制!

  

如果您着急,充值会员可直接联系发您资料!

  

QQ联系我

微信扫扫联系我

  



回复

使用道具 举报

点击查看
快速回复 返回列表 客服中心 搜索