In-depth introduction to the CChart daily course-the code analysis in the second lesson, the essence of the skeleton is the best.

Source: Internet
Author: User
Tags import database

In the first lesson, I wrote a small amount of code. People who are not familiar with CChart may not understand some code. Let's explain it to the students.

The first code is as follows.

#include "Chart.h"#ifdef _DEBUG#pragma comment(lib, "PlotDll_d.lib")#else#pragma comment(lib, "PlotDll.lib")#endif 

This section should be well understood. This is a preprocessing of C ++.

First, include the header file Chart. h.

Select different import/export libraries based on whether or not to compile the Debug version. If you compile the Debug version, you need to link PlotDll_d.lib to the import database, and PlotDll_d.dll will be loaded during running. If you compile the Release version, you need to link PlotDll. lib to the database and load PlotDll. dll during running.

The second code is as follows.

static CChartWnd  chartWnd;
This statement defines the variable chartWnd of a CChartWnd class.

First, explain the CChartWnd class.

To complete various drawing operations, the CChart class library has a large number of classes, and the drawing work is done by these classes. However, CChart internal classes are defined in the form of templates, which is not very convenient to use, so it is stupid to create a packaging class CChart, hiding the implementation details of the template class.

The CChartWnd class is a window class implemented on the basis of the CChart class. The purpose of this class is to automatically implement message response and reduce the amount of code written by students. CChartWnd is a window at the same time and can be understood as CWnd in MFC. Of course, it has nothing to do with CWnd, and the entire CChart class library has nothing to do with MFC.

The cchartwndclass must be fully interpreted in a few words. If you are not clear, you can view the instructions in the downloaded cchartdll.7z file.

Here, the chartWnd variable is set to static because the definition position of the chartWnd variable is inside the WndProc function of the window function. If the static attribute is not set, WndProc will exit and chartWnd will be released. If you set chartWnd to a global variable, you do not need to use static.

The third code is as follows.

Case WM_CREATE: chartWnd. attach (hWnd, kTypeXY); chartWnd. getChart ()-> AddPoint2D (-3.0, 9.0); chartWnd. getChart ()-> AddPoint2D (-2.0, 4.0); chartWnd. getChart ()-> AddPoint2D (-1.0, 1.0); chartWnd. getChart ()-> AddPoint2D (0.0, 0.0); chartWnd. getChart ()-> AddPoint2D (1.0, 1.0); chartWnd. getChart ()-> AddPoint2D (2.0, 4.0); chartWnd. getChart ()-> AddPoint2D (3.0, 9.0); chartWnd. getChart ()-> SetTitle (_ T ("Hello World, my first CChart Program Preface! "); Break;


This code responds to the WM_CREATE message and sets the drawing data and properties when the window is created.

ChartWnd. Attach (hWnd, kTypeXY );

This is the "sticking window", which is equivalent to sticking the CChartWnd window to the main window of the program.

The first parameter hWnd is the main window handle.

The second parameter, kTypeXY, is an enum defined by myself, which is actually an int variable, indicating the drawing type. KTypeXY indicates the simplest line chart. Of course there are many drawing types, which are not described here.

Note that the message loop of the main window is replaced by the message loop of chartWnd. You don't have to worry about it. CChartWnd only processes a few necessary messages, and the remaining messages are sent to the original message processing function of the Main Window for processing.

ChartWnd. GetChart ()-> AddPoint2D (-3.0, 9.0 );

The CChartWnd function CChartWnd: GetChart () gets the packaged CChart class pointer.

CChart: AddPoint2D (doublex, doubley) students should understand it well and add a data point to the image.

A total of seven data points are added here.

ChartWnd. GetChart ()-> SetTitle (_ T ("HelloWorld, my first CChart program! "));

The title of the image is well understood.

The fourth code is as follows.

chartWnd.Detach();
This sentence is placed in the response code of the message WM_DESTROY.

Previously, in the WM_CREATE message, the chartWnd "sticks" to the main window. Here, the program exits the money and the chartWnd window is "fetched" from the main window. Because chartWnd has taken over the message loop of the main window, if this is not the case, the message loop will be messy.

The fifth code is as follows. This code is not written but deleted.

case WM_PAINT:hdc = BeginPaint(hWnd, &ps);// TODO: Add any drawing code here...RECT rt;GetClientRect(hWnd, &rt);DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);EndPaint(hWnd, &ps);break; 

Because chartWnd has taken over the message loop of the main window, this paragraph does not actually work, that is, the ears of the deaf child-purely set, So delete it. It doesn't matter if you don't delete it. It's just stupid and a little clean.

The second lesson is over. Do you have any questions? If you have any questions, open the instructions in the cchartdll.7z file.

The next lesson is an exciting moment to introduce the CChart message response without writing any code. Is there free lunch in the world? It's time to witness a miracle. Coming soon.

Class now.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.