(Includes: Improved Cxpbutton class, practice program, practice program answer)
The Cxpbutton button shown in the article "Self-painting button implementation" can be described as an irregular button from the shape. We make the controls provided in MFC a standard control, and the button controls in it are called rule buttons because the shape is a rectangular rectangle of rules. But as streamlined design is widely used in interface design, more and more programmers are willing to add some irregular-shaped controls to their work. Take the button as an example, the simplest is to have a round button, triangle buttons and so on. The following figure is a well-defined set of circular buttons:
But these seem to show irregular-shaped buttons in our eyes, sometimes not nearly as satisfying. Take the previous design of the Cxpbutton as an example, let's look for its flaws below. Let's change the background color of the dialog to a different color to see what happens. You can modify the background color as follows: Add a WM_ERASEBKGND message for the dialog class, and if you can't find the message in the Class Wizard, refer to the method described in the custom button implementation, and notice that in the Add dialog box, filter for messages available To set as child windows to see WM_ERASEBKGND messages in the list. We add the code that paints the background color to the function:
BOOL CXPButtonDemoDlg::OnEraseBkgnd(CDC* pDC)
{
BOOL retValue= CDialog::OnEraseBkgnd(pDC);
CRect rc;
GetClientRect(&rc);
pDC->FillSolidRect(&rc,RGB(0,0,255));
return retValue;
}
After compiling the program, you can see the following effect, in the four corners of the button there is an ugly corner, which is what I call the flaw:
Now that you've found the problem, of course it's time to start solving the problem. As has been said in the article "Self-painting button implementation", the drawing of a button is mainly done in the DrawItem () function. The added point here is that DrawItem () is a system function provided by MFC, so you can add it by using the following diagram. Select Add Virtual Function ... Then find DrawItem in the list on the left side of the pop-up dialog box, and select Add and edit.