VC++ CDialog自绘(续1)

整个界面设计例程以英特尔显卡控制面板的界面为例,在前一节里面提到了2个参数,

    RECT m_rcImageWindow;        // 背景图片相对于源图片文件中裁剪下来的区域
    RECT m_rcImageClient;        // 9宫格中心客户区相对于源图片文件中的裁剪区域

这2个参数究竟是什么意思呢?如下图:

VC++ CDialog自绘(续1)_第1张图片

这整张图片就是软件加载进来的底图,其中蓝色方框内的就是我们真正需要的软件界面底图图rcWindow,而中间红色方框内的就是我们软件的中心区域(9宫格最中间部分)rcClient。红色方框边角线出头正好把蓝色区域分割为9份。理解了分割,那么下面的代码实现方式就不难理解了:

BOOL CSWDialog::OnEraseBkgnd(CDC* pDC)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (m_brBkgr.GetSafeHandle() == NULL && m_pBkgrBitmap == NULL)
	{
		return CDialog::OnEraseBkgnd(pDC);
	}

	ASSERT_VALID(pDC);

	CRect rcClient;
	GetClientRect(rcClient);

	if (m_pBkgrBitmap == NULL)
	{
		if (m_brBkgr.GetSafeHandle() != NULL)
		{
			pDC->FillRect(rcClient, &m_brBkgr);
		}
		else
		{
			CDialog::OnEraseBkgnd(pDC);
		}
	}

	if (m_pBkgrBitmap == NULL)
	{
		return TRUE;
	}

	ASSERT(m_sizeBkgrBitmap != CSize(0, 0));
	
	sw::CMemDC dc(*pDC, this);
	Gdiplus::Graphics g(dc);
	
	// 左上
	{
		CRect rcClipImage = m_rcImageWindow;
		rcClipImage.right = m_rcImageClient.left;
		rcClipImage.bottom = m_rcImageClient.top;
		CRect rcShowImage = rcClipImage;
		if (!rcShowImage.IsRectEmpty())
			g.DrawImage(m_pBkgrBitmap, RectF(rcShowImage.left, rcShowImage.top, rcShowImage.Width(), rcShowImage.Height()), rcClipImage.left, rcClipImage.top, rcClipImage.Width(), rcClipImage.Height(), UnitPixel, NULL, NULL, NULL);
	}
	// 左中
	{
		CRect rcClipImage = m_rcImageWindow;
		rcClipImage.top = m_rcImageClient.top;
		rcClipImage.right = m_rcImageClient.left;
		rcClipImage.bottom = m_rcImageClient.bottom;
		CRect rcShowImage = rcClipImage;
		rcShowImage.bottom = rcClient.bottom  - (m_rcImageWindow.bottom - m_rcImageClient.bottom);
		if (!rcShowImage.IsRectEmpty())
			g.DrawImage(m_pBkgrBitmap, RectF(rcShowImage.left, rcShowImage.top, rcShowImage.Width(), rcShowImage.Height()), rcClipImage.left, rcClipImage.top, rcClipImage.Width(), rcClipImage.Height(), UnitPixel, NULL, NULL, NULL);
	}
	// 左下
	{
		CRect rcClipImage = m_rcImageWindow;
		rcClipImage.top = m_rcImageClient.bottom;
		rcClipImage.right = m_rcImageClient.left;
		rcClipImage.bottom = m_rcImageWindow.bottom;
		CRect rcShowImage = rcClipImage;
		rcShowImage.top = rcClient.bottom - (m_rcImageWindow.bottom - m_rcImageClient.bottom);
		rcShowImage.bottom = rcClient.bottom ;
		if (!rcShowImage.IsRectEmpty())
			g.DrawImage(m_pBkgrBitmap, RectF(rcShowImage.left, rcShowImage.top, rcShowImage.Width(), rcShowImage.Height()), rcClipImage.left, rcClipImage.top, rcClipImage.Width(), rcClipImage.Height(), UnitPixel, NULL, NULL, NULL);
	}

	// 右上
	{
		CRect rcClipImage = m_rcImageWindow;
		rcClipImage.left = m_rcImageClient.right;
		rcClipImage.bottom = m_rcImageClient.top;
		CRect rcShowImage = rcClipImage;
		rcShowImage.left = rcClient.right - (m_rcImageWindow.right - m_rcImageClient.right);
		rcShowImage.right = rcClient.right;
		rcShowImage.bottom = m_rcImageClient.top;
		if (!rcShowImage.IsRectEmpty())
			g.DrawImage(m_pBkgrBitmap, RectF(rcShowImage.left, rcShowImage.top, rcShowImage.Width(), rcShowImage.Height()), rcClipImage.left, rcClipImage.top, rcClipImage.Width(), rcClipImage.Height(), UnitPixel, NULL, NULL, NULL);
	}
	// 右中
	{
		CRect rcClipImage = m_rcImageWindow;
		rcClipImage.top = m_rcImageClient.top;
		rcClipImage.left = m_rcImageClient.right;
		rcClipImage.bottom = m_rcImageClient.bottom;
		CRect rcShowImage = rcClipImage;
		rcShowImage.top = m_rcImageClient.top;
		rcShowImage.left = rcClient.right - (m_rcImageWindow.right - m_rcImageClient.right);
		rcShowImage.right = rcClient.right;
		rcShowImage.bottom = rcClient.bottom - (m_rcImageWindow.bottom - m_rcImageClient.bottom);
		if (!rcShowImage.IsRectEmpty())
			g.DrawImage(m_pBkgrBitmap, RectF(rcShowImage.left, rcShowImage.top, rcShowImage.Width(), rcShowImage.Height()), rcClipImage.left, rcClipImage.top, rcClipImage.Width(), rcClipImage.Height(), UnitPixel, NULL, NULL, NULL);
	}
	// 右下
	{
		CRect rcClipImage = m_rcImageWindow;
		rcClipImage.left = m_rcImageClient.right;
		rcClipImage.top = m_rcImageClient.bottom;
		rcClipImage.bottom = m_rcImageWindow.bottom;
		CRect rcShowImage = rcClipImage;
		rcShowImage.left = rcClient.right - (m_rcImageWindow.right - m_rcImageClient.right);
		rcShowImage.right = rcClient.right;
		rcShowImage.top = rcClient.bottom - (m_rcImageWindow.bottom - m_rcImageClient.bottom);
		rcShowImage.bottom = rcClient.bottom;
		if (!rcShowImage.IsRectEmpty())
			g.DrawImage(m_pBkgrBitmap, RectF(rcShowImage.left, rcShowImage.top, rcShowImage.Width(), rcShowImage.Height()), rcClipImage.left, rcClipImage.top, rcClipImage.Width(), rcClipImage.Height(), UnitPixel, NULL, NULL, NULL);
	}

	// 中上
	{
		CRect rcClipImage = m_rcImageWindow;
		rcClipImage.left = m_rcImageClient.left;
		rcClipImage.right = m_rcImageClient.right;
		rcClipImage.bottom = m_rcImageClient.top;
		CRect rcShowImage = rcClipImage;
		rcShowImage.right = rcClient.right - (m_rcImageWindow.right - m_rcImageClient.right);
		rcShowImage.bottom = m_rcImageClient.top;
		if (!rcShowImage.IsRectEmpty())
			g.DrawImage(m_pBkgrBitmap, RectF(rcShowImage.left, rcShowImage.top, rcShowImage.Width(), rcShowImage.Height()), rcClipImage.left, rcClipImage.top, rcClipImage.Width(), rcClipImage.Height(), UnitPixel, NULL, NULL, NULL);
	}
	// 中中
	{
		CRect rcClipImage = m_rcImageClient;
		CRect rcShowImage = rcClipImage;
		rcShowImage.right = rcClient.right - (m_rcImageWindow.right - m_rcImageClient.right);
		rcShowImage.bottom = rcClient.bottom - (m_rcImageWindow.bottom - m_rcImageClient.bottom);
		if (!rcShowImage.IsRectEmpty())
			g.DrawImage(m_pBkgrBitmap, RectF(rcShowImage.left, rcShowImage.top, rcShowImage.Width(), rcShowImage.Height()), rcClipImage.left, rcClipImage.top, rcClipImage.Width(), rcClipImage.Height(), UnitPixel, NULL, NULL, NULL);
	}
	// 中下
	{
		CRect rcClipImage = m_rcImageClient;
		rcClipImage.top = m_rcImageClient.bottom;
		rcClipImage.bottom = m_rcImageWindow.bottom;
		CRect rcShowImage = rcClipImage;
		rcShowImage.right = rcClient.right - (m_rcImageWindow.right - m_rcImageClient.right);
		rcShowImage.top = rcClient.bottom - (m_rcImageWindow.bottom - m_rcImageClient.bottom);
		rcShowImage.bottom = rcClient.bottom;
		if (!rcShowImage.IsRectEmpty())
			g.DrawImage(m_pBkgrBitmap, RectF(rcShowImage.left, rcShowImage.top, rcShowImage.Width(), rcShowImage.Height()), rcClipImage.left, rcClipImage.top, rcClipImage.Width(), rcClipImage.Height(), UnitPixel, NULL, NULL, NULL);
	}

	return TRUE;
}

 

你可能感兴趣的:(VC++(基础篇),VC++(控件篇),VC++(图像处理篇))