(reprint) VS2010/MFC Programming Primer 51 (Graphic Image: GDI object painting Brush CBrush)

Source: Internet
Author: User

The last section of the chicken Peck rice is mainly about the use of brush CPen, the front also said, GDI objects are most commonly used in the brush and brush, this section is about painting brush cbrush.

Chicken Peck rice is still the way to illustrate the use of brush by example. The function of this instance is to have a button control and a picture control on the dialog box, click the button to pop up the color dialog box, and then select a color in the Color dialog box and click OK, and the selected color will be displayed in the picture control.

In fact, the function of this instance can also be implemented by overloading the dialog box Wm_ctlcolor message response function, but in order to explain the use of brush cbrush, chicken peck Rice uses the method in the following code.

The following are the specific implementation steps for this instance:

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

2. In the automatically generated dialog template Idd_example51_dialog, delete "Todo:place dialog controls here." Static text box, add a button control and a picture control, the ID is set to Idc_color_sel_button and Idc_color_show_static, the button control's Caption property is set to select Color, and the dialog box template looks like this:

3. Add the CStatic variable to the picture control idc_color_show_static and set the name to M_piccolor.

4. In the dialog template, double-click the "Select Color" button to add a click message to the Response function Cexample51dlg::onbnclickedcolorselbutton (), modify the function is implemented as follows:

C + + code
  1. void Cexample51dlg::onbnclickedcolorselbutton ()
  2. {
  3. //Todo:add your control notification handler code here
  4. COLORREF color = RGB (255, 0, 0); the initial color of the//Color dialog box
  5. CColorDialog Colordlg (color); //Construct Color dialog box, initial color is red
  6. CRect rectpicture; //Picture control's rectangular area coordinates
  7. CBrush Newbrush; //Create a new paint brush
  8. CBrush *poldbrush; //Hands of old brush
  9. CClientDC Clientdc (this); //Constructs a device context object for the client area
  10. if (IDOK = = Colordlg.domodal ()) //Display Color dialog box
  11. {
  12. //If you clicked the OK button on the color dialog, do the following
  13. //Gets the color selected in the Color dialog box
  14. color = Colordlg.getcolor ();
  15. //Create a new brush with selected colors
  16. Newbrush.createsolidbrush (color);
  17. //Gets the screen coordinates of the rectangle area of the picture control
  18. M_piccolor.getwindowrect (&rectpicture);
  19. //Convert the screen coordinates of the picture control's rectangular area to the client area coordinates of its parent window, which is the dialog box
  20. ScreenToClient (&rectpicture);
  21. //Select New brush and save the old brush pointer to Poldbrush
  22. Poldbrush = Clientdc.selectobject (&newbrush);
  23. //Fill color with new paint brush for picture control
  24. Clientdc.rectangle (rectpicture);
  25. //restore old painting brush
  26. Clientdc.selectobject (Poldbrush);
  27. //Remove a new paint Brush
  28. Newbrush.deleteobject ();
  29. }
  30. }

The code has been added detailed comments, but chicken peck rice feel it is necessary to briefly introduce the next CCLIENTDC class. The CClientDC class is a derived class of the CDC class that generates a device context that corresponds to the client area of the window, and another class CWINDOWDC is a derived class of the CDC class that corresponds to the client and non-client areas, that is, CCLIENTDC can only draw in the client area, CWINDOWDC can be plotted in customer and non-client areas.

Here, someone will ask, what is the customer area, what is the non-client area? The non-client area includes the title bar in the window, the menu bar, the status bar, the scroll bar, the border and so on, and the customer area is the area outside the non-client area.

5, compile and run the program, in the Results dialog box, click the "Select Color" button to pop up the color dialog box, and select the color, the effect such as:

This section of the tutorial in addition to the use of painting brush CBrush, but also let us briefly review the usage of the Color dialog box.

Chicken Peck Rice reminds you that if any function or class usage is not very clear, you can consult MSDN. Finally I wish you all the work smoothly, there is nothing more to chicken peck rice stroll, hehe.

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

(reprinted) VS2010/MFC Programming Primer 51 (Graphic Image: GDI object brush CBrush)

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.