Several possible solutions for font penetration and darkening in Duilib

Keywords: Attribute

Abnormal, font penetrating, bright and dark

 

Normal phenomenon

Recently, when using the Duilib lightweight UI library as the interface, there are some problems with fonts. I did the operation after laying a layer of mask. Here are several possible situations

I. wrong property setting

For example, the SetShortcut property of the label control will affect this effect. I don't know why

The transparent property of RichEditUI is transparent

If there are attribute problems, please carefully check each attribute exclusion, which may lead to this situation

2. Stacking and typesetting

Some people will use Control to occupy the space, Container container, and various stacking layout. In my opinion, as long as you use Control, you can meet the demand. If the Container is too high-end, it will introduce some other unnecessary problems. When stacking layout, you may pile up several layers, and the background color penetration will also greatly affect the possible situation

layer problem

There are many profound solutions on the Internet, from principle analysis, but talk is heap, show me your code

Duilib focuses on rendering and writing fonts

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 has calculation error. Add a pixel here
			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);
	}
}

Let's not talk about the specific principle. If you don't understand it, add Shenzhen programmer exchange group to ask me. Ha ha ha ha ha ha. Good luck

550846167

 

Posted by shivangp on Sun, 01 Dec 2019 12:12:12 -0800