Dynamically change the bitmap on the picture control
Drawpicture (INT nbmpresource)
{
CDC dcmem;
Cclientdc DC (this );
Dcmem. createcompatibledc (& DC );
Cbitmap m_bitmap;
M_bitmap.loadbitmap (nbmpresource );
Dcmem. SelectObject (m_bitmap );
Crect rect;
Getdlgitem (idc_static_pic)-> getwindowrect (& rect );
Screentoclient (& rect );
DC. bitblt (rect. Left, rect. Top, rect. Right, rect. Bottom, & dcmem, 0, srccopy );
}
Click button event
{
M_nbmpresource = idb_bmp 1; // m_nbmpresource class member function, BMP resource ID
Invalidate ();
}
Because of invalidate (), the system calls the onpaint () function of the dialog box.
{
Cpaintdc DC (this); // you must have this sentence.
This-> drawpicture (m_nbmpresource );
Cdialog: onpaint ();
}
Use the bitmap in the file (not added to the resource)
Void cxxxdlg: drawpicture (lpctstr picture, int width, int height)
{
Paintstruct pS;
HDC;
HDC hdcmem;
Hbitmap HBM;
Bitmap bm;
// Load bitmap
// HBM = (hbitmap) LoadImage (null, picture, image_bitmap, width, height, lr_loadfromfile );
// Obtain the bitmap size
GetObject (HBM, sizeof (Bitmap), (lpvoid) & BM );
// Obtain the device Environment
Hwnd;
Hwnd = This-> getdlgitem (idc_static_pic)-> getsafehwnd ();
HDC =: getdc (hwnd );
// Obtain the device memory Environment
Hdcmem = createcompatibledc (HDC );
// Release the device Environment
: Releasedc (hwnd, HDC );
HDC =: beginpaint (hwnd, & PS );
// Select the bitmap to the memory device Environment
SelectObject (hdcmem, HBM );
// Call the bitblt function to copy the bitmap of the memory device environment to the device environment.
Bitblt (HDC, BM. bmwidth, BM. bmheight, hdcmem, srccopy );
// Release the device environment handle
: Endpaint (hwnd, & PS );
Deleteobject (HBM );
Deletedc (hdcmem );
}