关于Duilib中字体穿透变暗的几种可能性解决方案

不正常,字体穿透,忽明忽暗
正常现象

近来用Duilib轻量级UI库做界面,字体出现了一些问题。我是铺了一层蒙版之后进行操作的,以下是几种可能情况

一.属性设置错误

比如label标签控件SetShortcut属性会影响到这个效果,具体原因我也不明

RichEditUI的transparent属性是透明的

如果属性问题,请认真检查各个属性排除,有可能会导致这方面情况

二.堆叠排版问题

有的人会用Control占位,Container容器,各种堆叠排版布局,依个人之见,只要用Control即可满足需求,Container太高端反而引入一些其他不必要的问题,堆叠排版的时候有可能你堆了几层,背景颜色穿透也会极大影响可能情况

三.layer层问题

网上有许多很高深的解法,从原理分析,但是talk is cheap, show me your code

Duilib主要在渲染写入字体这方面

void CRenderEngine::DrawText(HDC hDC, CPaintManagerUI* pManager, RECT& rc, LPCTSTR pstrText, DWORD dwTextColor, int iFont, UINT uStyle, CControlUI* pControl)
{
	ASSERT(::GetObjectType(hDC) == OBJ_DC || ::GetObjectType(hDC) == OBJ_MEMDC);

	typedef BOOL(WINAPI *LPALPHABLEND)(HDC, int, int, int, int, HDC, int, int, int, int, BLENDFUNCTION);
	static LPALPHABLEND lpAlphaBlend = (LPALPHABLEND) ::GetProcAddress(::GetModuleHandle(_T("msimg32.dll")), "AlphaBlend");


    ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
    if( pstrText == NULL || pManager == NULL ) return;

	CDuiString sText = pstrText;
	if (pControl)
	{
		CPaintManagerUI::ProcessMultiLanguageTokensWithControl(pControl->GetClass(),sText , pControl);
	}
	else
	{
		CPaintManagerUI::ProcessMultiLanguageTokens(sText);
	}
	
	pstrText = sText;

	if (pManager->IsLayered())
	{
		Gdiplus::Graphics graphics(hDC);

		Gdiplus::Font font(hDC, pManager->GetFont(iFont));
		Gdiplus::RectF rectF((Gdiplus::REAL)rc.left, (Gdiplus::REAL)rc.top, (Gdiplus::REAL)(rc.right - rc.left), (Gdiplus::REAL)(rc.bottom - rc.top));
		Gdiplus::SolidBrush brush(Gdiplus::Color(255, GetBValue(dwTextColor), GetGValue(dwTextColor), GetRValue(dwTextColor)));

		Gdiplus::StringFormat stringFormat = Gdiplus::StringFormat::GenericTypographic();

		if ((uStyle & DT_END_ELLIPSIS) != 0) {
			stringFormat.SetTrimming(Gdiplus::StringTrimmingEllipsisCharacter);
		}

		int formatFlags = 0;
		if ((uStyle & DT_NOCLIP) != 0) {
			formatFlags |= Gdiplus::StringFormatFlagsNoClip;
		}
		if ((uStyle & DT_SINGLELINE) != 0) {
			formatFlags |= Gdiplus::StringFormatFlagsNoWrap;
		}

		stringFormat.SetFormatFlags(formatFlags);

		if ((uStyle & DT_LEFT) != 0) {
			stringFormat.SetAlignment(Gdiplus::StringAlignmentNear);
		}
		else if ((uStyle & DT_CENTER) != 0) {
			stringFormat.SetAlignment(Gdiplus::StringAlignmentCenter);
		}
		else if ((uStyle & DT_RIGHT) != 0) {
			stringFormat.SetAlignment(Gdiplus::StringAlignmentFar);
		}
		else {
			stringFormat.SetAlignment(Gdiplus::StringAlignmentNear);
		}
		stringFormat.GenericTypographic();
		if ((uStyle & DT_TOP) != 0) {
			stringFormat.SetLineAlignment(Gdiplus::StringAlignmentNear);
		}
		else if ((uStyle & DT_VCENTER) != 0) {
			stringFormat.SetLineAlignment(Gdiplus::StringAlignmentCenter);
		}
		else if ((uStyle & DT_BOTTOM) != 0) {
			stringFormat.SetLineAlignment(Gdiplus::StringAlignmentFar);
		}
		else {
			stringFormat.SetLineAlignment(Gdiplus::StringAlignmentNear);
		}

		if ((uStyle & DT_CALCRECT) != 0)
		{
			Gdiplus::RectF bounds;
			graphics.MeasureString(pstrText, -1, &font, rectF, &stringFormat, &bounds);

			// MeasureString存在计算误差,这里加一像素
			rc.bottom = rc.top + (long)bounds.Height + 1;
			rc.right = rc.left + (long)bounds.Width + 1;
		}
		else
		{
			graphics.DrawString(pstrText, -1, &font, rectF, &stringFormat, &brush);
		}
	}
	else
	{
		::SetBkMode(hDC, TRANSPARENT);
		::SetTextColor(hDC, RGB(GetBValue(dwTextColor), GetGValue(dwTextColor), GetRValue(dwTextColor)));
		HFONT hOldFont = (HFONT)::SelectObject(hDC, pManager->GetFont(iFont));
		::DrawText(hDC, pstrText, -1, &rc, uStyle | DT_NOPREFIX);
		::SelectObject(hDC, hOldFont);
	}
}

具体原理就不讲了,不懂的加深圳程序员交流群来问我吧,哈哈哈哈哈哈,祝大家好运

550846167

发布了259 篇原创文章 · 获赞 67 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/linjingtu/article/details/88690295