0

I got MFC app and I wanted to upload a picture using those 3 lines:

cbitmap_.LoadBitmap(IDB_BITMAP1);
picture_control_.SetBitmap((HBITMAP)cbitmap_.Detach());
UpdateData(FALSE);

When I did it on the main Dialog it worked. I tried to use it on a second Dialog that I open and it doesnt work on load. If I use it with a button on the second dialog it works as well... Where in the cpp file of the second dialog should I paste that code?

Thank you !

Danielgard
  • 45
  • 4
  • If you just want to present a resource bitmap, look at how `CAboutDlg` shows the MFC Icon when it opens. It is all done within the resource, no additional code is necessary. Just change the type to `Bitmap`. If you want to change it after the dialog is established, that is something else. But I think calling `UpdateData`, ever, is bad form. It breaks the data exchange mechanism that keeps your data clean where the user could `CANCEL` or `OK`. – lakeweb Sep 27 '21 at 18:10

1 Answers1

0

Final initialization of controls in a dialog is commonly done in response to the WM_INITDIALOG message. It is sent by the system after all controls have been created, but before the dialog is displayed. The CDialog class provides the virtual OnInitDialog member you can override in a derived implementation.

The call to UpdateData(FALSE) is not required either way. Assuming that picture_control_ is of type CStatic and properly attached to the control with a call to DDX_Control from the DoDataExchange implementation.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • Without the UpdateData it doesnt work in any version.. Even not in the buttons when it used to work before..I still didnt understand when is the right place to put those lines :( – Danielgard Sep 26 '21 at 16:51
  • This answer explains where and when to initialize controls in a dialog. If you are still having issues, you will need to provide more complete information in your question. Click [Edit](https://stackoverflow.com/posts/69336635/edit) to provide additional details, ideally a [mcve]. – IInspectable Sep 26 '21 at 17:05