本文主要是介绍CAA二次开发:MFC创建柱状图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
本文只提供局部代码。
步骤
在dialog中添加一个CATDlgFrame
dialog对应的.cpp中添加:
void ExportPrdMesCmd::BuildGraph()
{//在此函数内添加下面内容AddAnalyseNotificationCB(m_Dlg, m_Dlg->GetWindSizeNotification(),//拖动窗口执行ResizeDlg(CATCommandMethod)&ExportPrdMesCmd::ResizeDlg,NULL);}
void ExportPrdMesCmd::ResizeDlg(CATCommand * cmd,CATNotification * evt,CATCommandClientData data)
{CreatListCtrl();}
void ExportPrdMesCmd::CreatListCtrl()
{//创建listctrlHWND hwnd=(HWND)(m_Dlg->GetWindowHandle());if(0 != hwnd){DRECT oFrame1Rectangle;m_Dlg->GetFrame1()->GetRectDimensions(&oFrame1Rectangle);CWnd * pCWnd;pCWnd= CWnd::FromHandle(hwnd);RECT rct;rct.left=oFrame1Rectangle.x;//左rct.top=oFrame1Rectangle.y;//上rct.right=oFrame1Rectangle.x+oFrame1Rectangle.dx;//右rct.bottom=oFrame1Rectangle.y+oFrame1Rectangle.dy;//下//打印看一下数据cout<<"左:"<<rct.left<<endl;//8cout<<"上:"<<rct.top<<endl;//53cout<<"右:"<<rct.right<<endl;//261cout<<"下:"<<rct.bottom<<endl;//253//定义坐标系原点,x轴箭头处坐标,y轴箭头处坐标int d=30;//轴系和控件边框的距离int ox=rct.left+d;//原点o的x值int oy=rct.bottom-d;//原点o的y值int xx=rct.right-d;//x轴箭头处x值int xy=rct.bottom-d;//x轴箭头处y值int yx=rct.left+d;int yy=rct.top+d;CPaintDC dc(pCWnd);//CPaintDC 用于响应窗口重绘消息(WM_PAINT)是的绘图输出//标题CFont font;//用来设置大小、样式,颜色用dc.SetTextColor设置font.CreatePointFont(80, TEXT("Arial"));//设置字体,8号字dc.SelectObject(&font); dc.SetBkMode(TRANSPARENT);//设置透明背景dc.TextOut(150,70,TEXT("柱状图"));//x轴dc.MoveTo(ox,oy);dc.LineTo(xx,xy);//最后的那个箭头dc.MoveTo(xx,xy);dc.LineTo(xx-5,xy+2);dc.MoveTo(xx,xy);dc.LineTo(xx-5,xy-2);//y轴dc.MoveTo(ox,oy); dc.LineTo(yx,yy); dc.MoveTo(yx,yy);dc.LineTo(yx-2,yy+5);dc.MoveTo(yx,yy);dc.LineTo(yx+2,yy+5);//x轴刻度font.CreatePointFont(80, TEXT("Arial"));//设置字体,6号字dc.SelectObject(&font); CString sca_x[8]={"10","20","30","40","50","60","70","80"};for(int k=0;k<8;k++){//参数一:x 参数二:y 参数三:值,CString类型dc.TextOut(ox-5+40*k,oy+2,sca_x[k]);}//y轴刻度int sca_y[8]={0,15,30,45,60,75,90,105};CString sca_yy[8];for (int i=0;i<8;i++){sca_yy[i].Format(_T("%d"),sca_y[i]);//int类型转换成CString}for (int j=0;j<8;j++){dc.TextOut(ox-20,oy-7-sca_y[j],sca_yy[j]);}//柱子部分int spa_x = 5;//定义柱子间距int wid_x=5;//定义柱子宽度CBrush brush(RGB(200,29,102));//定义一个画刷font.CreatePointFont(60, TEXT("Arial"));//设置字体,6号字dc.SelectObject(&font); CRect rc;const UINT nDATALENGTH = 28; int num[nDATALENGTH]={6,22,60,5,92,70,40,50,8,22,11,12,10,80,55,66,55,10,7,32,11,22,33,66,41,10,8,74,};CString str[nDATALENGTH];//柱子数据标记for (int i=0;i<nDATALENGTH;i++){str[i].Format(_T("%d"),num[i]);}for (int i = 0;i<nDATALENGTH;i++){rc.left=ox+spa_x*2*i;rc.right=rc.left+wid_x;rc.bottom=oy;rc.top=oy-num[i];dc.SetBkMode(TRANSPARENT);//设置透明背景dc.FillRect(&rc,&brush);//绘制柱子dc.TextOut(rc.left-2,rc.top-10,str[i]);//数据标记}}
}
运行
本文到此结束。
这篇关于CAA二次开发:MFC创建柱状图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!