First, the existence of CBitmapButton problems
In MFC, to use the graphical buttons, you will typically select the CBitmapButton class and use the CBitmapButton class to set a BMP image of the normal, Selected, focused, and disabled four states of the button. These four pairs of state images require the same size, in which normal state pictures are required. Common calling code Examples:
CBitmapButton m_bmpBtn;
m_bmpBtn.SubclassDlgItem(IDC_BUTTON1,this);//关联控件
//CBitmapButton对象m_bmpBtn的LoadBitmaps函数加载程序内bmp资源。
m_bmpBtn.LoadBitmaps(IDB_BITMAP1,IDB_BITMAP2,IDB_BITMAP3,IDB_BITMAP4);
m_bmpBtn.SizeToContent();
Unfortunately: The above code in the Loadbitmaps function can only load the program's internal BMP resource files, can not load disk image files, but sometimes we need to change the CBitmapButton object's button state diagram, such as interface skin dynamic switch, it is possible to encounter this situation. How can I get the CBitmapButton object to load the state image dynamically? Here is a solution.
Second, the solution of the Thinking analysis
The analysis CBitmapButton found that the four state diagrams are stored in four CBitmap type member variables, which are defined as follows:
class Cbitmapbutton:public CButton
{
....
Protected:
//All bitmaps must is the same size
CBitmap M_bitmap; Normal image (REQUIRED)
CBitmap M_bitmapsel; Selected Image (OPTIONAL)
CBitmap M_bitmapfocus; Focused but not selected (OPTIONAL)
CBitmap m_bitmapdisabled; Disabled Bitmap (OPTIONAL)
...
}
because the normal external function of the CBitmapButton protected property member variable cannot be accessed directly, we define a public inheriting class cgetbitmaps so that we can access the four member variables. The Cgetbitmaps class is defined as follows:
class CGetBitmaps : public CBitmapButton
{
CBitmapButton *btn;
public:
CGetBitmaps(CBitmapButton *button)
{
btn=button;
}
inline CBitmap * Nor(){ //normal image (REQUIRED)
return (CBitmap *)(PCHAR(btn)+(ULONG)(PCHAR (&m_bitmap)-PCHAR(this)));//not PTCHAR, butPCHAR
}
inline CBitmap * Sel(){ // selected image (OPTIONAL)
return (CBitmap *)(PCHAR(btn)+(ULONG)(PCHAR (&m_bitmapSel)-PCHAR(this)));//not PTCHAR, butPCHAR
}
inline CBitmap * Foc(){ // focused but not selected (OPTIONAL)
return (CBitmap *)(PCHAR(btn)+(ULONG)(PCHAR (&m_bitmapFocus)-PCHAR(this)));//not PTCHAR, butPCHAR
}
inline CBitmap * Dis(){ // disabled bitmap (OPTIONAL)
return (CBitmap *)(PCHAR(btn)+(ULONG)(PCHAR (&m_bitmapDisabled)-PCHAR(this)));//not PTCHAR, butPCHAR
}
};