现在的位置: 首页 > 综合 > 正文

MFC 更换背景图片的方法

2013年01月17日 ⁄ 综合 ⁄ 共 775字 ⁄ 字号 评论关闭
void CRobotClientDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
	  //CDialog::OnPaint();把这个注释掉,不调用基类的OnPaint()

	   CPaintDC dc(this);

       CDC memdc;
       memdc.CreateCompatibleDC(&dc);//创建兼容DC

       CBitmap bkg;
       bkg.LoadBitmap(IDB_BG);//载入位图(这里用自己定义的位图ID)

       BITMAP bkginfo;
       bkg.GetBitmap(&bkginfo);//获取位图信息

       memdc.SelectObject(&bkg);

       RECT rect;
       GetWindowRect(&rect);//获取对话框信息

       dc.StretchBlt(0,0,rect.right-rect.left,rect.bottom-rect.top,&memdc,0,0,bkginfo.bmWidth,bkginfo.bmHeight,SRCCOPY);
	}
}

抱歉!评论已关闭.