(reprint) VS2010/MFC Programming Primer 50 (Graphic Image: GDI object brush CPen)

Source: Internet
Author: User

In the last section, the chicken pecked the rice. The CDC class and its screen drawing functions, the main content of this section is the brush CPen of GDI objects.

GDI Objects

In MFC, the CGdiObject class is the base class for GDI objects, and by looking at MSDN we can see that the CGdiObject class has six direct derived classes, and the GDI objects are mainly those six, respectively: CBitmap, CBrush, CFont, CPalette, CPen and CRgn.

Of the six GDI objects, the most common ones are brushes and brushes, the CPen class and the CBrush class. This article mainly explains the use of brushes.

Application Examples of brushes

Chicken Peck Rice Here directly through an example of a waveform diagram, to explain in detail the use of the brush.

Let's first describe what this instance does: There is a picture control on the dialog box that fills the background of the control with black, a timer is started, every time the timer is reached, all waveform data is moved forward one unit, and a random number within 80 is taken as the last data of the waveform. Then draw the waveform on the drawing control with a green brush. This enables the waveform to be drawn and changed dynamically.

Here are the specific implementation steps:

1, create a dialog-based MFC project, the name is set to "Example50".

2. In the automatically generated dialog template Idd_example50_dialog, delete "Todo:place dialog controls here." Static text box, add a picture control with ID set to Idc_wave_draw.

3. Add the CStatic variable to the picture control Idc_wave_draw and set the name to M_picdraw.

4. Add a macro definition above the Cexample50dlg class declaration in the file Example50Dlg.h file:

C + + code
    1. #define POINT_COUNT 100

The meaning of this symbolic constant is the number of points of the waveform, where it is defined as a symbolic constant with define to facilitate possible changes later, if we want to change the number of points to 200, then only the macro definition can be: #define POINT_COUNT 200, and if you do not use symbolic constants, If you use 100 directly in your program, you'll need to find all the 100 locations and replace them with 200, so it's not only cumbersome but also error-prone, so it's best to define them as symbolic constants.

5. Add a member array to the Cexample50dlg class in the CExample50Dlg.h file:

C + + code
    1. int M_nzvalues[point_count];

This array is used to hold the waveform data.

6. In the constructor of the Cexample50dlg class, the elements of the array m_nzvalues are assigned an initial value:

C + + code
    1. Cexample50dlg::cexample50dlg (cwnd* pparent /*=null*/)
    2. : CDialogEx (Cexample50dlg::idd, pparent)
    3. {
    4. M_hicon = AfxGetApp ()->loadicon (IDR_MAINFRAME);
    5. //Initializes the elements of the array m_nzvalues to 0
    6. memset (m_nzvalues, 0, sizeof (int) * point_count);
    7. }

7. In the initialization member function Cexample50dlg::oninitdialog () of the Cexample50dlg dialog box, construct the random number generator and start the timer. Cexample50dlg::oninitdialog () modified as follows:

C + + code
  1. BOOL Cexample50dlg::oninitdialog ()
  2. {
  3. Cdialogex::oninitdialog ();
  4. //Add "About ..." menu item to System menu.    
  5. //Idm_aboutbox must is in the System command range.    
  6. ASSERT ((Idm_aboutbox & 0xfff0) = = Idm_aboutbox);
  7. ASSERT (Idm_aboutbox < 0xf000);
  8. cmenu* Psysmenu = GetSystemMenu (FALSE);
  9. if (psysmenu! = NULL)
  10. {
  11. BOOL Bnamevalid;
  12. CString Straboutmenu;
  13. Bnamevalid = straboutmenu.loadstring (Ids_aboutbox);
  14. ASSERT (Bnamevalid);
  15. if (!straboutmenu.isempty ())
  16. {
  17. Psysmenu->appendmenu (Mf_separator);
  18. Psysmenu->appendmenu (mf_string, Idm_aboutbox, Straboutmenu);
  19. }
  20. }
  21. //Set The icon for this dialog. The framework does this automatically
  22. //When the application ' s main window is not a dialog
  23. SetIcon (M_hicon, TRUE); //Set big icon
  24. SetIcon (M_hicon, FALSE); //Set small icon
  25. //Todo:add extra initialization here
  26. //using time as seed to construct random number generator
  27. Srand ((unsigned) time (NULL));
  28. //start timer, ID 1, timing time is 200ms
  29. SetTimer (1, $, NULL);
  30. return TRUE; //Return TRUE unless you set the focus to a control
  31. }

8. Add waveform for Cexample50dlg class member function Cexample50dlg::D rawwave (CDC *PDC, CRect &rectpicture), parameters are the device context pointer and the rectangle area of the drawing.

C + + code
  1. void Cexample50dlg::D rawwave (CDC *PDC, CRect &rectpicture)
  2. {
  3. float Fdeltax; coordinate distance of two plot points adjacent to//x-axis
  4. float Fdeltay; coordinate values for each logical unit of the//y-axis
  5. int NX; //The horizontal axis used to store the drawing point when connected
  6. int NY; //The ordinate used to store the drawing point when connected
  7. CPen Newpen; //For creating a new brush
  8. CPen *poldpen; //For storing old brushes
  9. CBrush Newbrush; //For creating a new paint Brush
  10. CBrush *poldbrush; //For storing old paint brushes
  11. //Calculate fdeltax and Fdeltay
  12. Fdeltax = (float) rectpicture.width ()/(POINT_COUNT-1);
  13. Fdeltay = (float) rectpicture.height ()/80;
  14. //Create a New Black paint Brush
  15. Newbrush.createsolidbrush (RGB (0,0,0));
  16. //Select a new brush and save the old brush's pointer to Poldbrush
  17. Poldbrush = Pdc->selectobject (&newbrush);
  18. //Fill black with black brush for drawing control to form a black background
  19. Pdc->rectangle (rectpicture);
  20. //restore old painting brush
  21. Pdc->selectobject (Poldbrush);
  22. //Remove a new paint Brush
  23. Newbrush.deleteobject ();
  24. //Create a solid brush with a thickness of 1 and a green color
  25. Newpen.createpen (Ps_solid, 1, RGB (0,255,0));
  26. //Select a new brush and save the old brush's pointer to Poldpen
  27. Poldpen = Pdc->selectobject (&newpen);
  28. //Moves the current point to the lower-left corner of the Drawing Control window, which is the starting point of the waveform
  29. Pdc->moveto (Rectpicture.left, Rectpicture.bottom);
  30. //calculate the coordinate position of each point in the m_nzvalues array, connect in turn, and eventually form a curve
  31. For (int i=0; i<point_count; i++)
  32. {
  33. NX = Rectpicture.left + (int) (i * fdeltax);
  34. NY = Rectpicture.bottom-(int) (m_nzvalues[i] * fdeltay);
  35. Pdc->lineto (NX, NY);
  36. }
  37. //restore old brushes
  38. Pdc->selectobject (Poldpen);
  39. //delete new brushes
  40. Newpen.deleteobject ();
  41. }

9, with the timer and the drawing member function, we can add to the response function of the WM_TIMER message to the waveform data timing processing and the timing of the waveform drawing. How to add a timer and Wm_timer message processing function If you forget, you can go to the VS2010/MFC Programming 44 (MFC Common class: Timer timer) review.

The processing function of the WM_TIMER message is modified as follows:

C + + code
  1. void Cexample50dlg::ontimer (uint_ptr nidevent)
  2. {
  3. //Todo:add your message handler code here and/or call default
  4. CRect rectpicture;
  5. //Move all elements in the array forward one unit, the first element discarded
  6. For (int i=0; i<point_count-1; i++)
  7. {
  8. M_nzvalues[i] = m_nzvalues[i+1];
  9. }
  10. //Assign a random value of 80 or less for the last element (integer type)
  11. M_NZVALUES[POINT_COUNT-1] = rand ()% 80;
  12. //Gets the client area coordinates of the Drawing Control
  13. //(customer area coordinates are the origin in the upper-left corner of the window, which differs from the screen coordinates of the origin in the upper -left corner of the screen)
  14. M_picdraw.getclientrect (&rectpicture);
  15. //Draw waveform diagram
  16. Drawwave (M_picdraw.getdc (), rectpicture);
  17. Cdialogex::ontimer (nidevent);
  18. }

10, when the dialog box is destroyed, the timer should be closed. So add a handler for the Wm_destroy message for the Cexample50dlg class and modify it as follows:

C + + code
    1. void Cexample50dlg::ondestroy ()
    2. {
    3. Cdialogex::ondestroy ();
    4. //Todo:add your message Handler code here
    5. //Off timer
    6. KillTimer (1);
    7. }

11, all ready, compile and run. The final effect is as follows:

About the brush, Chicken peck rice is here, the next section will be a brief talk about the use of painting brush. Thank you for your attention!

Original address: http://www.jizhuomi.com/software/246.html

(reprint) VS2010/MFC Programming Primer 50 (Graphic Image: GDI object brush CPen)

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.