c++mfcretro-computing

Bitmap image on button using MFC 2.5 dialog application


I've seen the other threads on this topic, but none of them work with MFC 2.5 since CButton::ModifyStyle() and CButton::SetBitmap() do not exist in MFC 2.5.

I'm making a dialog application using a Resource Editor to create a dialog with several buttons. The buttons are set to Owner Draw in the Editor. I'm using the following code:

void CMainDialog::OnInitDialog()
{
  // to do:  initialization
  CBitmapButton m_Processor;
  m_Processor.AutoLoad( IDC_CPU, this );

  return TRUE;
}

The above code should have automatically loaded the bitmap resource I have created as IDB_CPU. However, on compile the button location just shows a blank space rather than loading the bitmap.

I have also tried this code:

void CMainDialog::OnInitDialog()
{
  // to do:  initialization
  CBitmapButton m_Processor;
  m_Processor.LoadBitmaps( IDB_CPU, NULL, NULL, NULL );

  return TRUE;
}

But it produces the same result with just a blank space. I know the image exists in the bitmap (I can open it with Paint, for example), and I've verified the resource ID for IDB_CPU is properly set in the resource.h file.

Any help is appreciated!


Solution

  • Looks like m_Processor is (or at least should be) a member of class CMainDialog.
    The m_ prefix hints that it you indeed have such a member, and if not it should be added.

    However, this line inside CMainDialog::OnInitDialog():

    CBitmapButton m_Processor;
    

    Declares a local variable. It shadows a member with the same name (if exists).
    The bitmap is loaded into this local variable and discarded when it goes out of scope when the method returns, instead of into the dialog member which is shown in the GUI (assuming that there is such a member).

    Therefore you should remove this line.