CView OnDraw中显示位图,并在 resize 时使位图始终显示在客户区左下角

首先在 CMainFrame 中定义三个共有成员变量

public:
	HBITMAP m_hLogo;
	int m_nBmpHeight;
	int m_nBmpWidth;

在构造函数、析构函数中

CMainFrame::CMainFrame()
{
	m_hLogo = (HBITMAP)::LoadImage(NULL, "resource\\banner.bmp", IMAGE_BITMAP, 0,0, LR_LOADFROMFILE);
	if(m_hLogo)
	{
		BITMAP bitmap;
		GetObject(m_hLogo,sizeof(BITMAP),&bitmap);
		m_nBmpHeight = bitmap.bmHeight;
		m_nBmpWidth = bitmap.bmWidth;
	}
	else
	{
		m_nBmpHeight = 380;
		m_nBmpWidth = 658;
	}
}

CMainFrame::~CMainFrame()
{
	if(m_hLogo)
		::DeleteObject(m_hLogo);
}

在 CView OnDraw 中

void CCGUIView::OnDraw(CDC* pDC)
{
	CCGUIDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here

	CRect rect;
	GetWindowRect(&rect);

	HDC hdcDest = ::GetDC(m_hWnd);
	HDC hdcSrc = ::CreateCompatibleDC(hdcDest);
	CMainFrame * pMainFrm = (CMainFrame *)::AfxGetApp()->m_pMainWnd;
	::SelectObject(hdcSrc, pMainFrm->m_hLogo);
	::StretchBlt(hdcDest,0, rect.Height()-pMainFrm->m_nBmpHeight, pMainFrm->m_nBmpWidth, pMainFrm->m_nBmpHeight, hdcSrc, 0, 0, pMainFrm->m_nBmpWidth, pMainFrm->m_nBmpHeight, SRCCOPY); 
	::ReleaseDC(m_hWnd, hdcDest);	
	::DeleteDC(hdcSrc);				
}


原文地址:https://www.cnblogs.com/silyvin/p/9106911.html