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.