We have already demonstrated N examples of CChart programming. In these examples, our CChart images are all drawn in the main window of the program.
In many cases, this is not the case. This lesson introduces how to use CChart in the dialog box.
It should be noted that the CChart version number must be no less than 2.5.1.4 for the content of this lesson; otherwise, the operation will be abnormal. Go to the stupid download channel on the CSDN website. Click Open Link
Step 1: Open VC and create a project Lesson18 Based on the MFCAppWizard (exe) wizard. Select DialogBased in the Wizard. Click Finish without any changes.
Step 2: copy the Chart. h, PlotDll_d.lib, PlotDll_d.dll, PlotDll. lib, and PlotDll. dll files of the five CChart libraries to the Lesson18 folder,
Step 3: In the resource editor on the VC interface, delete the "TODO: Set dialog control here ." In the dialog box, place a Picture control and set the ID of the control to ID_CANVAS.
650) this. width = 650; "src =" http://img.blog.csdn.net/20130921001447125? Watermark/2/text/plain =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/SouthEast "alt =" SouthEast "/>
Step 4: Open the Lesson18Dlg. h file in VC, and add the CChart header file and library file reference to its header.
#include "Chart.h"#ifdef _DEBUG#pragma comment(lib, "PlotDll_d.lib")#else#pragma comment(lib, "PlotDll.lib")#endif
Step 5: Add a member variable of the CChartWnd type to the CLesson18Dlg class in the Lesson18Dlg. h file.
CChartWnd m_ChartWnd;
Step 6: add the Math Library header file reference to the header of Lesson18Dlg. cpp.
#include <math.h>
Step 7: Use ClassWizard to add the response function of the WM_DESTROY message to the CLesson18Dlg class.
Step 8: Modify the OnInitDialog function and add the following code under the line "// TODO: Addextrainitializationhere.
CWnd * pWnd = GetDlgItem (IDC_CANVAS); if (pWnd) {CRect rtWCtrl, rtWParent; pWnd-> GetWindowRect (& rtWCtrl); GetWindowRect (& rtWParent ); int capH = GetSystemMetrics (SM_CYCAPTION); int dx = GetSystemMetrics (SM_CXBORDER); int offsetx = (rtWCtrl. left-rtWParent. left-dx); int offsety = (rtWCtrl. top-rtWParent. top-capH); CRect rtCtrlClient; pWnd-> GetClientRect (& rtCtrlClient); OffsetRect (& rtCtrlClient, offsetx, offsety); Round (m_hWnd, success, rtCtrlClient); int I; double pX [360], pY [360]; for (I = 0; I <360; I ++) {pX [I] = I * 2.0*3.1415926536/360.0; pY [I] = sin (pX [I]);} m_ChartWnd.GetChart ()-> AddCurve (pX, pY, 360); m_ChartWnd.GetChart () -> SetTitle (_ T ("CChart in the dialog box "));}
Note that when we stick the window, it is not the Picture control, but the main window of the dialog box. This is because the dialog box intercepts the message of the control and directly sticks the window to the Picture control.
The previous Code mainly aims to calculate the offset between the Picture control and the main window so that the image can be correctly drawn.
Step 9: Modify the OnDestroy function and add the following code under the line "// TODO: Addyourmessagehandlercodehere.
m_ChartWnd.Detach();
OK. The ticket is closed. The running result of our program is as follows.
650) this. width = 650; "src =" http://img.blog.csdn.net/20130921001837906? Watermark/2/text/plain =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/SouthEast "alt =" SouthEast "/>
We can see that a CChart window is nested in the dialog box. The interaction function of CChart can also be used to bring up the Properties dialog box window of CChart, which is a subwindow of the CChart window. Haha, is this like a Russian doll.
650) this. width = 650; "src =" http://img.blog.csdn.net/20130921001938078? Watermark/2/text/plain =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/SouthEast "alt =" SouthEast "/>
It turns out that it is not difficult to use CChart in the dialog box, haha.
Class!