VC++ CDialog自绘(续3)

自绘标题栏和菜单栏,需要处理的消息有:
    ON_WM_NCPAINT()
    ON_WM_NCCALCSIZE()
    ON_WM_NCACTIVATE()
    ON_WM_NCLBUTTONDOWN()
    ON_WM_NCHITTEST()

核心代码:

void CSWDialog::DoNcPaint()
{
	if ((!m_bHasTitleBar) || (m_brBkgr.GetSafeHandle() == NULL && m_pBkgrBitmap == NULL))
		return;

	if (::IsRectEmpty(&m_rcImageClient) || ::IsRectEmpty(&m_rcImageWindow))
		return;

	CDC* pDC = GetWindowDC();
	ASSERT_VALID(pDC);

	CRect rcClient;
	GetClientRect(rcClient);

	// 获取位置
	CRect rcWindow;
	GetWindowRect(&rcWindow);
	rcWindow.OffsetRect(-rcWindow.left, -rcWindow.top);

	// 剪除掉客户区
	int cx = ::GetSystemMetrics(SM_CXFRAME);
	int cy = ::GetSystemMetrics(SM_CYFRAME);
	int ret = pDC->ExcludeClipRect(cx, m_rcImageClient.top - m_rcImageWindow.top, rcWindow.right - cx, rcWindow.bottom - cy);

	if (m_pBkgrBitmap == NULL)
	{
		if (m_brBkgr.GetSafeHandle() != NULL)
		{
			pDC->FillRect(rcWindow, &m_brBkgr);
		}
		else
		{
			return;
		}
	}

	if (m_pBkgrBitmap == NULL)
		return;

	ASSERT(m_sizeBkgrBitmap != CSize(0, 0));
	sw::CMemDC dc(*pDC, rcWindow);
	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.left = m_rcImageClient.right;
		rcClipImage.bottom = m_rcImageClient.top;
		CRect rcShowImage = rcClipImage;
		rcShowImage.left = rcWindow.right - (m_rcImageWindow.right - m_rcImageClient.right);
		rcShowImage.right = rcWindow.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.left = m_rcImageClient.left;
		rcClipImage.right = m_rcImageClient.right;
		rcClipImage.bottom = m_rcImageClient.top;
		CRect rcShowImage = rcClipImage;
		rcShowImage.right = rcWindow.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_rcImageWindow;
		rcClipImage.right = rcClipImage.left + cx;
		rcClipImage.bottom = m_rcImageClient.top;
		CRect rcShowImage = rcClipImage;
		rcShowImage.top = m_rcImageClient.top - m_rcImageWindow.top;
		rcShowImage.bottom = rcWindow.bottom + 100;
		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_rcImageWindow.right - cx;
		rcClipImage.bottom = m_rcImageClient.top;
		CRect rcShowImage = rcClipImage;
		rcShowImage.top = m_rcImageClient.top - m_rcImageWindow.top;
		rcShowImage.left = rcWindow.right - cx;
		rcShowImage.bottom = rcWindow.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_rcImageWindow.bottom - cy;
		rcClipImage.bottom = m_rcImageClient.top;
		CRect rcShowImage = rcClipImage;
		rcShowImage.right = rcWindow.right;
		rcShowImage.bottom = rcWindow.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);
	}

	//
	/*
	 *	绘制标题栏图标
	*/
	int xPos = 10;
	int yPos = 10;
	HICON hIcon = (HICON)::SendMessage(m_hWnd, WM_GETICON, FALSE, 0);
	if (hIcon)
	{
		DrawIconEx(dc.GetSafeHdc(), xPos, yPos, hIcon, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 0, 0, DI_NORMAL);
		xPos += GetSystemMetrics(SM_CXSMICON);
		xPos += 5;
	}

	//
	/*
	 *	绘制标题栏文本
	*/
	CString strText;
	int nLen = ::GetWindowTextLength(m_hWnd);
	::GetWindowText(m_hWnd, strText.GetBufferSetLength(nLen), nLen + 1);
	strText.ReleaseBuffer();
	if (!strText.IsEmpty())
	{
		LOGFONT lfFont;
		memset(&lfFont, 0, sizeof(lfFont));
		lfFont.lfHeight = -16;
		lfFont.lfWeight |= FW_BOLD;
		lstrcpy(lfFont.lfFaceName, _T("微软雅黑"));
		Gdiplus::Font font(dc.GetSafeHdc(), &lfFont);

		StringFormat stringFormat;
		stringFormat.SetFormatFlags(StringFormatFlagsNoWrap);
		stringFormat.SetAlignment(StringAlignmentNear);
		stringFormat.SetLineAlignment(StringAlignmentNear);
		std::wstring wsTitle = CStringHelper::Ansi2Unicode(strText);
		SolidBrush brush((ARGB)Color::White);
		PointF point;
		point.X = (Gdiplus::REAL)xPos;
		point.Y = (Gdiplus::REAL)yPos - 4;
		g.SetTextRenderingHint(TextRenderingHintAntiAlias);
		g.DrawString(wsTitle.c_str(), wsTitle.size(), &font, point, &stringFormat, &brush);
	}

	//
	/*
	 *	绘制系统功能按钮
	*/
	DrawSysMenu(g);
}

下一篇介绍如何来实现客户区自绘,请继续关注......

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值