MFC背景自适应大小

首先加载位图

在对话框中添加WM_PAINT,WM_SIZE消息函数

在OnPaint函数中的else语句部分添加

CBitmap bmp;
bmp.LoadBitmap(IDB_BMP_MAIN); //载入位图


int nBmpWidth, nBmpHeight;
BITMAP bmInfo;
bmp.GetBitmap(&bmInfo);
nBmpWidth=bmInfo.bmWidth;
nBmpHeight=bmInfo.bmHeight;  //获取位图的实际大小


CRect clientRC;
GetClientRect(clientRC);  //获取客户区域的大小


CDC *pDC=GetDC();  //屏幕DC
CDC memDC;
memDC.CreateCompatibleDC(pDC); //内存DC
memDC.SelectObject(&bmp);
pDC->StretchBlt(0,0,clientRC.Width(),clientRC.Height(),&memDC,0,0, nBmpWidth, nBmpHeight, SRCCOPY);  //在窗口绘图


memDC.DeleteDC();  
bmp.DeleteObject();
ReleaseDC(pDC);//释放
CDialogEx::OnPaint();

添加自定义变量old_width,old_height,然后在函数OnInitDialog当中添加

CRect rect;
GetClientRect(&rect);
old_width = rect.Width();    //将初始的宽和高保存起来,当窗口大小改变的时候用得上
old_height = -rect.Height();

在OnSize中添加

Invalidate(FALSE);

猜你喜欢

转载自blog.csdn.net/AlphaPoseidon/article/details/87858808