Dynamically change the bitmap on the picture control

Source: Internet
Author: User

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

}

 

 

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.