MFC绘制直方图和饼图

转载原文:使用Win32API 编程方法编写一个程序绘制直方图和饼图

 

MFC 工程中的代码

 void CChildView::OnPaint() 
{
    CPaintDC dc(this); // device context for painting
//  改字体
    LOGFONT log;
    CFont font;
    log.lfHeight=0;
    log.lfEscapement=0;
    log.lfItalic=0;
    log.lfUnderline=0;
    log.lfStrikeOut=0;

//    dc.SelectObject(&font); 
    font.CreateFontIndirect(&log);
    //画笔
    CPen pen(PS_SOLID,1,RGB(0,0,0));         
    dc.SelectObject(&pen); 
//    *OldPen = dc.SelectObject(&pen);//旧画笔


    //逻辑坐标
    SetViewportOrgEx(dc,50,500,NULL);
    dc.SetMapMode(MM_LOMETRIC);//0.1毫米
   
    //纵坐标
    dc.MoveTo(0,0);
    dc.LineTo(0,1000);//高1000
    dc.LineTo(-10,980);
    dc.MoveTo(0,1000);
    dc.LineTo(10,980);
  //纵坐标循环
    CString str;
    int i;
    for(i=1;i<10;i++)
    {   str.Format("%d",i*10);
    
        dc.TextOut(-30,i*100,str);
    }
        dc.TextOut(-50,1050,"%");
    //横坐标
    dc.MoveTo(0,0);
   dc.LineTo(850,0);
    dc.LineTo(830,10);
    dc.MoveTo(850,0);
    dc.LineTo(830,-10);

    int x1,y1,x2,y2,y3,x3;
    float zhuan=0.4,ben=0.3,shuo=0.2,bo=0.1;
    int h=1000,r=400;

    
    
    //画柱子
 
    dc.SelectObject(&font);     //修改字体
    
    CRect r1(100,zhuan*h,200,0);
    CBrush br1(RGB(253,51,67));
    dc.FillRect(r1,&br1);//矩形,颜色
   dc.TextOut(130,-10,"专科");//横坐标的说明文字

    CRect r2(300,ben*h,400,0);
    CBrush br2(RGB(250,164,51));
    dc.FillRect(r2,&br2);
    dc.TextOut(330,-10,"本科");

    CRect r3(500,shuo*h,600,0);
    CBrush br3(RGB(139,197,51));
    dc.FillRect(r3,&br3);
    dc.TextOut(530,-10,"硕士");

    CRect r4(700,bo*h,800,0);
    CBrush br4(RGB(3,141,216));
    dc.FillRect(r4,&br4);
    dc.TextOut(730,-10,"博士");


//    dc.SetBkColor(RGB(0,0,0));
//    dc.Rectangle(100,zhuan*h,200,0);
//    dc.Rectangle(300,ben*h,400,0);
//    dc.Rectangle(500,shuo*h,600,0);
//    dc.Rectangle(700,bo*h,800,0);
    
    //画饼图,利用cos,sin函数计算顶点值,颜色沿用之前已经定义了的brush
    dc.SelectObject(&br1);
    x1=int(r*cos(zhuan*3.14*2))+1400;
    y1=int(r*sin(zhuan*3.14*2)+400);
  dc.Pie(1000,800,1800,0,1800,400,x1,y1);


    dc.SelectObject(&br2);
    x2=int(r*cos((ben+zhuan)*3.14*2))+1400;
    y2=int(r*sin((ben+zhuan)*3.14*2))+400;
    dc.Pie(1000,800,1800,0,x1,y1,x2,y2);

    dc.SelectObject(&br3);
    x3=int(r*cos((ben+zhuan+shuo)*3.14*2))+1400;
    y3=int(r*sin((ben+zhuan+shuo)*3.14*2))+400;
    dc.Pie(1000,800,1800,0,x2,y2,x3,y3);
    CBrush   br9(RGB(125,30,227));
    dc.SelectObject(&br9);

    dc.SelectObject(&br4);
    dc.Pie(1000,800,1800,0,x3,y3,1800,400); 

    //在园外右上角画图例,加上说明文字
   CRect r5(2000,1000,2080,1060);
    dc.FillRect(r5,&br1);
    dc.TextOut(2100,1040,"专科");

    CRect r6(2000,900,2080,960);
    dc.FillRect(r6,&br2);
    dc.TextOut(2100,940,"本科");

    CRect r7(2000,800,2080,860);
    dc.FillRect(r7,&br3);
    dc.TextOut(2100,840,"硕士");

    CRect r8(2000,700,2080,760);
    dc.FillRect(r8,&br4);
    dc.TextOut(2100,740,"博士");

    // TODO: Add your message handler code here
    
    // Do not call CWnd::OnPaint() for painting messages
}

 

PS:

CPaintDCCClientDC的区别

  CPaintDC类是CDC类的一个派生类,一般用在响应WM_PAINT消息的函数OnPaint()中。如果在OnPaint函数内定义了CPaintDC类的对象,通过这个类对象就可以使用CDC类的成员函数完成视图客户区中的图形绘制操作。

  CClientDC类也是CDC类的派生类。它只能在窗口的客户区(即窗口中除了边框、标题栏、菜单栏以及状态栏外的中间部分)中进行绘图,坐标点(0,0)通常指的是客户区的左上角。

posted @ 2015-10-26 15:19  wenglabs  阅读(6047)  评论(0编辑  收藏  举报