Cchart Daily Lesson--happy started playing against the seventh lesson Iron Egg Bamboo Horse, to the Basics of the dialog window multi-area drawing

Source: Internet
Author: User

Previous lesson stupid to introduce you to the General window on the multi-area drawing, this lesson will describe the situation under the dialog box. Because the dialog box has some differences between the message loop and the normal window, so the specific operation is slightly different, the following process can be referred to you.

The first step is to open the VC and build a project LessonA07 based on the MFC AppWizard (EXE) wizard. Select dialog Based in the wizard, and don't make any changes, just point to finish.

In the second step, copy the library file to the LessonA07 folder,

The third step, in the VC interface of the resource editor, delete the middle of the dialog box "TODO: Here to set the dialog control." "This tab then puts two picture controls in the dialog box and sets the IDs of the two controls to Id_chart1 and Id_chart2.


Fourth step, open the LessonA07 file in VC, add Cchart header file and library file reference in its head.

The fifth step is to add member variables of the Cchart type and cwnd* type to the Clessona07dlg class in the LessonA07Dlg.h file.

CCHARTM_CHART[2]; CWND*M_PWND[2];

Note that this is an array of stupid, in fact, like the previous lesson that is OK to use the array, just because stupid lazy accustomed to, with the array can write loops, improve the life of the keyboard

The sixth step is to write the following initialization code in the OnInitDialog function of the LessonA07Dlg.h file.

CRect rt;//Chart 1m_chart[0]. SetType (ktypexy); for (int i=-10; i<=10; i++) {m_chart[0]. ADDPOINT2D (i, i*i);} M_pwnd[0] = GetDlgItem (IDC_CHART1); M_pwnd[0]->getclientrect (&RT); M_chart[0]. Setconfinerect (RT); M_chart[0]. Setbkgndcolor (GetSysColor (Color_3dface));//Chart 2m_chart[1]. SetType (Ktypepie); m_chart[1]. AddPie (2, "Liu Yan"); m_chart[1]. AddPie (2, "Qu Ying"); M_chart[1]. AddPie (4, "Zhou Xun"); M_chart[1]. AddPie (8, "Faye Wong"); m_pwnd[1] = GetDlgItem (IDC_CHART2); M_pwnd[1]->getclientrect (&RT); m_chart[1]. Setconfinerect (RT);

Note here as before, use Setconfinerect to determine the plot target area for each chart.

Seventh, add the following code to the front of the OnPaint function of the LessonA07Dlg.h file.

for (int i=0; i<2; i++) {m_chart[i]. OnDraw (M_pwnd[i]->m_hwnd);}

You can now run it with the following effect.


Eighth step, add the message processing routines. Because the message loop of the dialog box is special, we are dealing with it in PreTranslateMessage. This function needs to be overloaded first, and then rewritten as follows.

BOOL Clessona07dlg::P retranslatemessage (msg* pMsg) {//Todo:add your specialized code here and/or call the base Classcrec T rect; CPoint pt; CWnd *pwnd;int i;if (Pmsg->message==wm_lbuttondown) {for (i=0; i<2; i++) {pwnd=m_pwnd[i];p wnd->getwindowrect ( &rect); if (rect. PtInRect (PMSG-&GT;PT)) {pt = pmsg->pt;pwnd->screentoclient (&pt), if (M_chart[i]. OnLButtonDown (Pwnd->m_hwnd, PT, 0)) {M_chart[i]. OnDraw (Pwnd->m_hwnd);}} }}else if (pmsg->message==wm_lbuttonup) {for (i=0; i<2; i++) {pwnd=m_pwnd[i];p wnd->getwindowrect (&rect); if (rect. PtInRect (PMSG-&GT;PT)) {pt = pmsg->pt;pwnd->screentoclient (&pt), if (M_chart[i]. OnLButtonUp (Pwnd->m_hwnd, PT, 0)) M_chart[i]. OnDraw (Pwnd->m_hwnd);} }}else if (PMSG-&GT;MESSAGE==WM_LBUTTONDBLCLK) {for (i=0; i<2; i++) {pwnd=m_pwnd[i];p wnd->getwindowrect (& RECT); if (rect. PtInRect (PMSG-&GT;PT)) {pt = pmsg->pt;pwnd->screentoclient (&pt), if (M_chart[i]. ONLBUTTONDBLCLK (Pwnd->m_hwnd, PT, 0)) M_chart[i]. OnDraw (pwnd->m_hwnd);} }}else if (pmsg->message==wm_mousemove) {for (i=0; i<2; i++) {pwnd=m_pwnd[i];p wnd->getwindowrect (&rect); if (rect. PtInRect (PMSG-&GT;PT)) {pt = pmsg->pt;pwnd->screentoclient (&pt), if (M_chart[i]. OnMouseMove (Pwnd->m_hwnd, PT, 0)) M_chart[i]. OnDraw (Pwnd->m_hwnd);} }}else if (pmsg->message==wm_rbuttondown) {//for (i=0; i<2; i++) {pwnd=m_pwnd[i];p wnd->getwindowrect (& RECT); if (rect. PtInRect (PMSG-&GT;PT)) {pt = pmsg->pt;//pwnd->screentoclient (&pt), if (M_chart[i]. OnContextMenu (NULL, Pwnd->m_hwnd, PT)) M_chart[i]. OnDraw (Pwnd->m_hwnd);} }}return CDialog::P retranslatemessage (PMSG);}

Note that the location of processing messages here is in PreTranslateMessage because of the particularity of the dialog message processing. In addition, in the specific process of not responding to Wm_contextmenu, but instead of using Wm_rbuttondown, also for this reason.

Well, this lesson is also over, I hope the content of this lesson can be a reference for everyone.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Cchart Daily Lesson--happy started playing against the seventh lesson Iron Egg Bamboo Horse, to the Basics of the dialog window multi-area drawing

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.