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
- void Cexample51dlg::onbnclickedcolorselbutton ()
- {
- //Todo:add your control notification handler code here
- COLORREF color = RGB (255, 0, 0); the initial color of the//Color dialog box
- CColorDialog Colordlg (color); //Construct Color dialog box, initial color is red
- CRect rectpicture; //Picture control's rectangular area coordinates
- CBrush Newbrush; //Create a new paint brush
- CBrush *poldbrush; //Hands of old brush
- CClientDC Clientdc (this); //Constructs a device context object for the client area
- if (IDOK = = Colordlg.domodal ()) //Display Color dialog box
- {
- //If you clicked the OK button on the color dialog, do the following
- //Gets the color selected in the Color dialog box
- color = Colordlg.getcolor ();
- //Create a new brush with selected colors
- Newbrush.createsolidbrush (color);
- //Gets the screen coordinates of the rectangle area of the picture control
- M_piccolor.getwindowrect (&rectpicture);
- //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
- ScreenToClient (&rectpicture);
- //Select New brush and save the old brush pointer to Poldbrush
- Poldbrush = Clientdc.selectobject (&newbrush);
- //Fill color with new paint brush for picture control
- Clientdc.rectangle (rectpicture);
- //restore old painting brush
- Clientdc.selectobject (Poldbrush);
- //Remove a new paint Brush
- Newbrush.deleteobject ();
- }
- }
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)