关于MFC 对话框背景设置为图片

首先导入位图图片,注意必须是.bmp格式,因为这里我用的bmp格式的图片当的背景。当然你可以提前将普通图片转换为.bmp格式,然后再导入。这里的对话框会根据图片大小自动调整大小
第一步:在头文件中加入:
CBrush imageBrush;
第二步:在OnInitDialg()中添加
	CBitmap bitmap;
	HBitmap Bitmap;
	bitmap.LoadBitmap(IDB_BITMAP2);
	bitmap.GetBitmap(&Bitmap);
	
	imageBrush.CreatePatternBrush(&bitmap);
	SetWindowPos(NULL ,0,0,Bitmap.bmWidth,Bitmap.bmHeight,SWP_NOMOVE |SWP_NOZORDER );
//	SetWindowPos(NULL ,0,0,Bitmap.bmWidth,Bitmap.bmHeight,SWP_NOMOVE |SWP_NOZORDER |SWP_SHOWWINDOW ); //如果带
SWP_SHOWWINDOW会使图片显示在屏幕左上角
// 	MoveWindow(rect.left,rect.top,Bitmap.bmWidth,Bitmap.bmHeight);//这里的MoveWindow与SetWindowPos的效果是一样的

 第三步:重载OnCtlColor()函数:
HBRUSH CBimapDlgDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	
	// TODO: Return a different brush if the default is not desired
	if(pWnd==this)
		return imageBrush;
	return hbr;
}


你可能感兴趣的:(MFC,MFC,对话框,背景图片)