The button control of MFC is a different control from the other, and its Cbuttton class is a subclass of CWnd, and it is necessary to write a new class, such as Cmybutton, when modifying the button's background, color, and border. Here are the steps for background, color, and border modification:
1. Add Class->cmybutton on project. This will automatically generate two files, one for each. CPP file and A. h file. such as CMyButton.cpp and CMyButton.h
2. Add the inclusion of CMyButton.h to the header file C**dlg.h of your main project, i.e. #include "CMyButton.h"
3. Locate the OnInitDialog () function and add the following code to the function:
cmybuttonm_btn;//defines a Cmybutton variable that can be defined elsewhere, and only contains "CMyButton.h" to
Modify buttons to Bs_ownerdraw style, allowing button to be painted in a custom mode
GetDlgItem (Idc_button1)->modifystyle (0,bs_ownerdraw,0);
Bind control Idc_button1 with Class Cmybutton, response overloaded function DrawItem ()
M_btn.attach (Idc_button1,this);
Sets the background color of the Buttondown, Setdowncolor () and Setupncolor () are destructors in the Cmybutton class
M_btn.setdowncolor (RGB (255,0,0));
Set the background color of button up
M_btn.setupcolor (RGB (0,0,255));
Note: If the control idc_button1 in a previous message binding, then a bug will occur, just comment out the message-bound statement in the mapping function DoDataExchange (). As follows:
DDX_Control (PDX, IDC_BUTTON1,M_CBBTN);//comment out on it.
4. Add the following code to the new CMyButton.cpp file and the CMyButton.h file:
Header files: CMyButton.h as follows:
#pragmaonce
#include "AFXWIN.h"
Class Cmybutton:publiccbutton
{
Declare_dynamic (Cmybutton)
Public
Cmybutton ();
Virtual~cmybutton ();
Set the background color of Buttondown
Voidsetdowncolor (colorref color);
Set the background color of Buttonup
Voidsetupcolor (colorref color);
BOOL Attach (Constuint NID, cwnd* pparent);
Protected
Functions that must be overloaded
Virtual Voiddrawitem (lpdrawitemstructlpdrawitemstruct);
Public
Three colors for text, buttondown background color, Button up background color
Colorrefm_textcolor, M_downcolor,m_upcolor;
};
Source file: CMyButton.cpp
#include "StdAfx.h"
#include "CMyButton.h"
Cmybutton::cmybutton (void)
{
M_downcolor =m_upcolor = RGB (0,0,0);//Initialize set to Black
}
Cmybutton::~cmybutton (void)
{
}
BOOL Cmybutton::attach (constuint nid,cwnd* pparent)
{
if (! SubclassDlgItem (NID, pparent))
return FALSE;
return TRUE;
}
Voidcmybutton::setdowncolor (Colorrefcolor)
{Functions of the//cmybutton class
M_downcolor = color;
}
Voidcmybutton::setupcolor (COLORREF color)
{
M_upcolor = color;
}
Voidcmybutton::D rawitem (lpdrawitemstructlpdrawitemstruct)
{
CDCDC;
dc. Attach (LPDRAWITEMSTRUCT->HDC);//Get Painted device environment CDC
VERIFY (Lpdrawitemstruct->ctltype==odt_button);
Appropriate button on the text, here are the steps: 1, first get the text of the button edited in the resource,
Then redraw the text onto the button,
The background color of this text is also set to transparent, so that only text is displayed on the button
const int bufSize = 512;
TCHAR Buffer[bufsize];
GetWindowText (buffer,bufsize);
Intsize=strlen (buffer);//Get length
DrawText (lpdrawitemstruct->hdc,buffer,size,&lpdrawitemstruct->rcitem,dt_center| dt_vcenter| dt_singleline| Dt_tabstop);//Draw Text
SetBkMode (lpdrawitemstruct->hdc,transparent);//Transparent
if (lpdrawitemstruct->itemstate&ods_selected)//handling when the button is pressed
{////redraw the entire control
Cbrushbrush (M_downcolor);
dc. FillRect (& (Lpdrawitemstruct->rcitem), &brush);//brush with brushes, fill rectangle box
Because this is redrawn, the text is redrawn.
DrawText (lpdrawitemstruct->hdc,buffer,size,&lpdrawitemstruct->rcitem,dt_center| dt_vcenter| dt_singleline| Dt_tabstop);
SetBkMode (lpdrawitemstruct->hdc,transparent);
}
else//when the button is not operating or bouncing
{
Cbrushbrush (M_upcolor);
dc. FillRect (& (Lpdrawitemstruct->rcitem), &brush);//
DrawText (lpdrawitemstruct->hdc,buffer,size,&lpdrawitemstruct->rcitem,dt_center| dt_vcenter| dt_singleline| Dt_tabstop);
SetBkMode (lpdrawitemstruct->hdc,transparent);
}
if (lpdrawitemstruct->itemstate&ods_selected) && (lpdrawitemstruct->itemaction& (ODA_SELECT | Oda_drawentire)))
{//Select this control, highlight border
Colorreffc=rgb (255-getrvalue (M_upcolor), 255-getgvalue (M_upcolor), 255-getbvalue (M_upcolor));
CBrush Brush (FC);
dc. Framerect (& (Lpdrawitemstruct->rcitem), &brush);//brush with brushes, fill rectangle border
}
if (! ( lpdrawitemstruct->itemstate&ods_selected) && (lpdrawitemstruct->itemaction& ODA_SELECT))
{
Cbrushbrush (M_upcolor); Control the selected state end, remove the border
dc. Framerect (&lpdrawitemstruct->rcitem,&brush);//}
dc. Detach ();
}
about changing the AfxMessageBox dialog box title
In fact, this title, which is our default project name, we should change the title without changing the project name. In fact, this title in the resource stringtable can be found, find Afx_ids_app_title, here you can easily change the title.
Reprinted from: http://blog.sina.com.cn/s/blog_65cab32d01013uad.html