字体相关
一、利用UpdateLayeredWindow,在GDI+绘制字体时,FontStyleRegular样式的字体会变成透明,而其它的样式都可以正常显示;
问题的原因是:
层窗口(WS_EX_LAYERED),用UpdateLayeredWindow输出的文字(用常规方法输出的:TextOut、DrawText、DrawString...),并且设置了AC_SRC_ALPHA和ULW_ALPHA,就会存在这种错误。
解决办法:
- void CGDIPlus_TextTestDlg::Redraw()
- {
- CRect rect;
- GetClientRect(&rect);
- SolidBrush brush(Color(200,255,0,0));
- m_pGraphics->FillRectangle(&brush,0,0,rect.Width(),rect.Height());
- CFont *pFont=GetFont();
- LOGFONT lf;
- pFont->GetLogFont(&lf);
- CComBSTR wszFN(lf.lfFaceName);
- int nHeight=-lf.lfHeight;
- FontFamily ff(wszFN,NULL);
- int nStyle=FontStyleRegular;
- Font font(&ff,nHeight,nStyle,UnitPixel);
- StringFormat format;
- format.SetAlignment(StringAlignmentNear);
- SolidBrush brush2(Color(255,0,0,0));
- int nStyle2=FontStyleBold;
- Font font2(&ff,nHeight,nStyle2,UnitPixel);
- //-----直接写字,变得穿透了
- CComBSTR wsz("文字Test: 直接写字,变得穿透了");
- m_pGraphics->DrawString(wsz,wsz.Length(),&font,PointF(50,50),&format,&brush2);
- //-----文字加粗
- wsz="文字Test: 文字加粗";
- m_pGraphics->DrawString(wsz,wsz.Length(),&font2,PointF(50,70),&format,&brush2);
- //-----SetTextRenderingHint(TextRenderingHintAntiAlias)
- wsz="文字Test: SetTextRenderingHint(TextRenderingHintAntiAlias)";
- GraphicsContainer gc=m_pGraphics->BeginContainer();
- m_pGraphics->SetTextRenderingHint(TextRenderingHintAntiAlias);
- m_pGraphics->DrawString(wsz,wsz.Length(),&font,PointF(50,90),&format,&brush2);
- m_pGraphics->EndContainer(gc);
- //-----GraphicsPath
- wsz="文字Test: GraphicsPath";
- gc=m_pGraphics->BeginContainer();
- m_pGraphics->SetSmoothingMode(SmoothingModeHighQuality);
- GraphicsPath path(FillModeAlternate);
- path.AddString(wsz,wsz.Length(),&ff,nStyle,nHeight,PointF(50,110),&format);
- m_pGraphics->FillPath(&brush2,&path);
- m_pGraphics->EndContainer(gc);
- SIZE sizeWindow = rect.Size();
- POINT ptSrc = { 0, 0 };
- BLENDFUNCTION blend;
- blend.BlendOp = AC_SRC_OVER;
- blend.BlendFlags = 0;
- blend.AlphaFormat = AC_SRC_ALPHA;
- blend.SourceConstantAlpha = 255;
- UpdateLayeredWindow(m_hWnd, hdcHWND, NULL, &sizeWindow, hdcHBMP, &ptSrc, 0, &blend, ULW_ALPHA);
- }
第一个方法是字体加宽,把字体的Weight改成800就不会透
第二个方法是SetTextRenderingHint(TextRenderingHintAntiAlias)
第三个方法是把字串转换成GraphicsPath,然后用FillPath画出来,画之前SetSmoothingMode(SmoothingModeHighQuality)字会好看一点
不过,这三种方法都改变了字画出来的样子,第一种方法字被加粗了,第二三种方法字被抗锯齿了。
二、直接在桌面写字,字体有阴影!
如下段代码,点(100,10)是在透明区,所以也就是直接将文字写在了桌面上:
- Graphics grpah(m_hDcBackground);
- FontFamily ff(L"微软雅黑");
- Font font(&ff,14,FontStyleBold ,UnitPixel);
- StringFormat format;
- format.SetAlignment(StringAlignmentCenter);
- grpah.DrawString(L"不会出问题吧",-1,&font,PointF(100,10),&format,&SolidBrush(Color(254,0,255,0)));
- grpah.ReleaseHDC(m_hDcBackground);
看到啦,后面有黑色阴影;
解决办法:
加上一条语句:
- <span style="color:#000000;">grpah.SetTextRenderingHint(TextRenderingHintAntiAlias);</span>
其它
2、HBITMAP与DC的关系?
DC与HBITMAP的关系就好比是叫一个人去某目的地进行演出,而HBITMAP就好比是为这个人演出提供的设备,搭建演出舞台供DC去演绎,展示,同时HBITMAP保存演绎的信息直到调用 DeleteObject(m_hBitmap)并将m_hBitmapBk = NULL;为止;
DC:表示演出目的地,在确定好舞台后才能演绎,展示。
HBITMAP:表示演出舞台,保存演绎的信息,直到调用DeleteObject(m_hBitmap)并将m_hBitmapBk = NULL才丢失原保留的信息。