前言
本文只提供局部代码。
步骤
在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()
{
//创建listctrl
HWND 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;//8
cout<<"上:"<<rct.top<<endl;//53
cout<<"右:"<<rct.right<<endl;//261
cout<<"下:"<<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]);//数据标记
}
}
}
运行
本文到此结束。