Set the background bitmap in the mfc dialog box and prevent bitmap distortion.

Source: Internet
Author: User

Add a message processing function to the. h header file of the class corresponding to Dialog to change the background of Dialog:

afx_msg BOOL OnEraseBkgnd(CDC* pDC);

Then add the implementation code in the. cpp implementation file:

BOOL CHGolfDlg: OnEraseBkgnd (CDC * pDC)
{
// TODO: Add your message handler code here and/or call default
// Create a memory DC
CDC MemDC;
// CBitmap object
CBitmap Bitmap, * pOldBitmap;
// BITMAP handle
BITMAP bmp;
// Load bitmap
Bitmap. LoadBitmap (IDB_MAINBG );
// Bind the bitmap resource with the handle
Bitmap. GetObject (sizeof (BITMAP), & bmp );
Int cx, cy;
Cx = GetSystemMetrics (SM_CXSCREEN );
Cy = GetSystemMetrics (SM_CYSCREEN );
// Create a memory-compatible DC
MemDC. CreateCompatibleDC (pDC );
// Replace the original bitmap
POldBitmap = (CBitmap *) (MemDC. SelectObject (& Bitmap ));
PDC-> SetStretchBltMode (COLORONCOLOR); // if this mode is not set, the image will be seriously distorted.
PDC-> StretchBlt (0, 0, cx, cy, & MemDC, 0, 0, bmp. bmWidth, bmp. bmHeight, SRCCOPY); // stretch the image to make it full screen

MemDC. SelectObject (pOldBitmap );
MemDC. DeleteDC ();
Return TRUE;
}

 

IDB_MAINBG is the background bitmap.

 

Note the following code:

PDC-> SetStretchBltMode (COLORONCOLOR); // if this mode is not set, the image will be seriously distorted.

You can try to comment it out, and you will find something unsatisfactory. The picture is distorted and damaged.

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.