Saturday, April 20, 2013

Adding JPG and GIF Images to MFC Project

1. Open the resource editor for a CDialog Window and add an MFC Picture Control
2. Change the Picture Control's "Type" property from the default "Frame" to "Bitmap"
3. Create a global pointer to a CStatic class object. Ex: CStatic * pPCView;
4. Associate the CStatic pointer with the Picture Control using the function GetDigItem() and casting to the correct data type in OnInitDialog(). We must do this before we want to try to call any methods with the pointer. Ex: pPCView = (CStatic *) GetDigItem(PC_VIEW);
5. Include the file in our globals to provide access to CImage class.
6. In our code where we want to display the image, create a CImage object and a CBitmap object. Ex:
      CImage ViewImage;
      CBitmap ViewBitmap;
7. Load the JPG or GIF file into the CImage object. Ex:
                                          ViewImage.Load(_T("View/Hyosoka.jpg"));
8. Attach the CImage object to the CBitmap Object by passing it as an argument to CBimap's Attach() method and calling detach() on the CImage. Ex: ViewBitmap.Attach(ViewImage.Detach());
9. Call the method SetBitmap() on the pointer to the picture control and pass in the CBitmap as an argument, casting it to HBITMAP. Ex: pPCView->SetBitmap((HBITMAP)ViewBitmap);
10. We will need to add ON_WM_PAINT to our MFC Message Map tags.
11. We should override the MFC OnPaint() method.
12. Call the CPaintDC method, passing in the this pointer.

No comments:

Post a Comment