GDI+在界面开发中遇到的问题汇总

来源:互联网 发布:上海希而科 知乎 编辑:程序博客网 时间:2024/05/16 17:17

字体相关

一、利用UpdateLayeredWindow,在GDI+绘制字体时,FontStyleRegular样式的字体会变成透明,而其它的样式都可以正常显示;

问题的原因是:
层窗口(WS_EX_LAYERED),用UpdateLayeredWindow输出的文字(用常规方法输出的:TextOut、DrawText、DrawString...),并且设置了AC_SRC_ALPHA和ULW_ALPHA,就会存在这种错误。
解决办法:

[cpp] view plaincopy
  1. void CGDIPlus_TextTestDlg::Redraw()  
  2. {  
  3.   CRect rect;  
  4.   GetClientRect(&rect);  
  5.   SolidBrush brush(Color(200,255,0,0));  
  6.   m_pGraphics->FillRectangle(&brush,0,0,rect.Width(),rect.Height());  
  7.   
  8.   CFont *pFont=GetFont();  
  9.   LOGFONT lf;  
  10.   pFont->GetLogFont(&lf);  
  11.   CComBSTR wszFN(lf.lfFaceName);  
  12.   int nHeight=-lf.lfHeight;  
  13.   FontFamily ff(wszFN,NULL);  
  14.   int nStyle=FontStyleRegular;  
  15.   Font font(&ff,nHeight,nStyle,UnitPixel);  
  16.   StringFormat format;  
  17.   format.SetAlignment(StringAlignmentNear);  
  18.   SolidBrush brush2(Color(255,0,0,0));  
  19.   int nStyle2=FontStyleBold;  
  20.   Font font2(&ff,nHeight,nStyle2,UnitPixel);  
  21.   
  22.   
  23.   //-----直接写字,变得穿透了  
  24.   CComBSTR wsz("文字Test: 直接写字,变得穿透了");  
  25.   m_pGraphics->DrawString(wsz,wsz.Length(),&font,PointF(50,50),&format,&brush2);  
  26.   
  27.   //-----文字加粗  
  28.   wsz="文字Test: 文字加粗";  
  29.   m_pGraphics->DrawString(wsz,wsz.Length(),&font2,PointF(50,70),&format,&brush2);  
  30.   
  31.   //-----SetTextRenderingHint(TextRenderingHintAntiAlias)  
  32.   wsz="文字Test: SetTextRenderingHint(TextRenderingHintAntiAlias)";  
  33.   GraphicsContainer gc=m_pGraphics->BeginContainer();  
  34.   m_pGraphics->SetTextRenderingHint(TextRenderingHintAntiAlias);  
  35.   m_pGraphics->DrawString(wsz,wsz.Length(),&font,PointF(50,90),&format,&brush2);  
  36.   m_pGraphics->EndContainer(gc);  
  37.   
  38.   //-----GraphicsPath  
  39.   wsz="文字Test: GraphicsPath";  
  40.   gc=m_pGraphics->BeginContainer();  
  41.   m_pGraphics->SetSmoothingMode(SmoothingModeHighQuality);  
  42.   GraphicsPath path(FillModeAlternate);  
  43.   path.AddString(wsz,wsz.Length(),&ff,nStyle,nHeight,PointF(50,110),&format);  
  44.   m_pGraphics->FillPath(&brush2,&path);  
  45.   m_pGraphics->EndContainer(gc);  
  46.   
  47.   SIZE sizeWindow = rect.Size();  
  48.   POINT ptSrc = { 0, 0 };  
  49.   BLENDFUNCTION blend;  
  50.   blend.BlendOp = AC_SRC_OVER;  
  51.   blend.BlendFlags = 0;  
  52.   blend.AlphaFormat = AC_SRC_ALPHA;  
  53.   blend.SourceConstantAlpha = 255;  
  54.   UpdateLayeredWindow(m_hWnd, hdcHWND, NULL, &sizeWindow, hdcHBMP, &ptSrc, 0, &blend, ULW_ALPHA);  
  55. }  
有三个方法避免,但是都不是真正的解决这个问题
第一个方法是字体加宽,把字体的Weight改成800就不会透
第二个方法是SetTextRenderingHint(TextRenderingHintAntiAlias)
第三个方法是把字串转换成GraphicsPath,然后用FillPath画出来,画之前SetSmoothingMode(SmoothingModeHighQuality)字会好看一点

不过,这三种方法都改变了字画出来的样子,第一种方法字被加粗了,第二三种方法字被抗锯齿了。找到一个简单的方法同时避免穿透和变形的问题,把Color的Alhpa值改成254就好了。使用DrawString + A->254 没问题。

二、直接在桌面写字,字体有阴影!

如下段代码,点(100,10)是在透明区,所以也就是直接将文字写在了桌面上:

[cpp] view plaincopy
  1. Graphics grpah(m_hDcBackground);  
  2. FontFamily ff(L"微软雅黑");  
  3. Font font(&ff,14,FontStyleBold ,UnitPixel);  
  4. StringFormat format;  
  5. format.SetAlignment(StringAlignmentCenter);  
  6. grpah.DrawString(L"不会出问题吧",-1,&font,PointF(100,10),&format,&SolidBrush(Color(254,0,255,0)));  
  7. grpah.ReleaseHDC(m_hDcBackground);  
效果:

看到啦,后面有黑色阴影;
解决办法:
加上一条语句:
[cpp] view plaincopy
  1. <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才丢失原保留的信息。

0 0
原创粉丝点击