Four-state diagram of dynamic loading CBitmapButton

Source: Internet
Author: User
Tags bmp image

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
  }
};

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.