In the previous course, all the examples that I am stupid about are Win32Application projects created under VC.
CChart has nothing to do with the framework. It only uses Win32API internally and does not use any framework-related code, such as MFC, WTL, and QT. However, CChart can be used in these frameworks.
By the way, in fact, the earliest CChart version was based on MFC, and later it was stupid to re-develop and abandon MFC.
This section describes how to program CChart In the MFC framework.
Most people are familiar with MFC. In fact, most people who use VC are using MFC, and most people who don't use VC do not know what it is. It's stupid to assume that all of you are using VC, so you don't need to explain the MFC.
Still in the form of an instance.
Step 1: Open VC and create a project Lesson15 Based on the MFCAppWizard (exe) wizard. Do not make any changes in the Wizard. Click Finish.
Step 2: copy the five library files to the Lesson15 folder.
Step 3: Open the Lesson15View. h file in VC and add the following code in its header.
#include "Chart.h"#ifdef _DEBUG#pragma comment(lib, "PlotDll_d.lib")#else#pragma comment(lib, "PlotDll.lib")#endif
Step 4: In the Lesson15View. h file, add a CChartWnd variable to the CLesson15View class.
CChartWnd m_ChartWnd;
Step 5: Use ClassWizard to add OnCreate and OnDestroy message processing functions to the CLesson15View class.
Step 6: Modify the OnCreate function as follows.
Int CLesson15View: OnCreate (maid) {if (CView: OnCreate (maid) =-1) return-1; // TODO: add your specialized creation code values (m_hWnd, kTypeXY); m_ChartWnd.GetChart ()-> AddPoint2D (1.0, 3.0); m_ChartWnd.GetChart ()-> AddPoint2D (2.0, 2.5); values () -> AddPoint2D (3.0, 1.0); m_ChartWnd.GetChart ()-> AddPoint2D (4.0, 6.0); m_ChartWnd.GetChart ()-> AddPoint2D (5.0, 7.0); m_ChartWnd.GetChart () -> AddPoint2D (6.0, 2.0); m_ChartWnd.GetChart ()-> AddPoint2D (7.0, 1.0); m_ChartWnd.GetChart ()-> AddPoint2D (8.0, 4.0); m_ChartWnd.GetChart () -> SetTitle (_ T ("Drawing under MFC test"); return 0 ;}
Step 7: Modify the OnDestroy function as follows.
void CLesson15View::OnDestroy() {CView::OnDestroy();// TODO: Add your message handler code herem_ChartWnd.Detach();}
Run the program.
650) this. width = 650; "src =" http://img.blog.csdn.net/20130919183355343? Watermark/2/text/plain =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/SouthEast "alt =" SouthEast "/>
Haha, it succeeded.
Programming in MFC is also quite simple.
The preceding CChartWnd programming method can also use the CChart class, but messages must be processed manually. Next, modify the above steps.
Step 4 (new): In the Lesson15View. h file, add a CChart variable to the CLesson15View class.
CChart m_Chart;
Step 5 (new): Use ClassWizard to add the OnCreate message processing function to the CLesson15View class.
OnDestroy is not required here.
Step 6 (new): Modify the OnCreate function as follows.
Int CLesson15View: OnCreate (maid) {if (CView: OnCreate (maid) =-1) return-1; // TODO: add your specialized creation code values (kTypeXY); m_Chart.AddPoint2D (1.0, 3.0); m_Chart.AddPoint2D (2.0, 2.5); m_Chart.AddPoint2D (3.0, 1.0); m_Chart.AddPoint2D (4.0, 6.0 ); m_Chart.AddPoint2D (5.0, 7.0); m_Chart.AddPoint2D (6.0, 2.0); m_Chart.AddPoint2D (7.0, 1.0); m_Chart.AddPoint2D (8.0, 4.0 ); m_Chart.SetTitle (_ T ("Drawing under MFC test"); return 0 ;}
Step 7 (new): Modify the OnDraw function as follows.
void CLesson15View::OnDraw(CDC* pDC){CLesson15Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data herem_Chart.OnDraw(m_hWnd);}
It is now ready to run, but interaction has not yet been implemented.
Step 8: Use ClassWizard to add the response functions of WM_LBUTTONDOWN, WM_LBUTTONUP, WM_LBUTTONDBLCLK, WM_MOUSEMOVE, WM_CONTEXTMENU, WM_KEYDOWN, and WM_ERASEBKGND messages.
Step 9: Modify the response function as follows.
void CLesson15View::OnLButtonDown(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultif(m_Chart.OnLButtonDown(m_hWnd, point))GetDocument()->UpdateAllViews(NULL);//CView::OnLButtonDown(nFlags, point);}void CLesson15View::OnLButtonUp(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultif(m_Chart.OnLButtonUp(m_hWnd, point))GetDocument()->UpdateAllViews(NULL);//CView::OnLButtonUp(nFlags, point);}void CLesson15View::OnLButtonDblClk(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultif(m_Chart.OnLButtonDblClk(m_hWnd, point))GetDocument()->UpdateAllViews(NULL);//CView::OnLButtonDblClk(nFlags, point);}void CLesson15View::OnMouseMove(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultif(m_Chart.OnMouseMove(m_hWnd, point))GetDocument()->UpdateAllViews(NULL);//CView::OnMouseMove(nFlags, point);}void CLesson15View::OnContextMenu(CWnd* pWnd, CPoint point) {// TODO: Add your message handler code hereif(m_Chart.OnContextMenu(NULL, m_hWnd, point))GetDocument()->UpdateAllViews(NULL);}void CLesson15View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) {// TODO: Add your message handler code here and/or call defaultif(m_Chart.OnKeyDown(m_hWnd, nChar))GetDocument()->UpdateAllViews(NULL);//CView::OnKeyDown(nChar, nRepCnt, nFlags);}BOOL CLesson15View::OnEraseBkgnd(CDC* pDC) {// TODO: Add your message handler code here and/or call defaultreturn TRUE;//return CView::OnEraseBkgnd(pDC);}
Run the program, and the effect is exactly the same as the previous one.
It can be seen from this that it is more convenient to use CChartWnd programming in MFC.
This lesson introduces the example of using CChart to draw a line chart under MFC. Other images can be painted in different ways.