[Ray]How to use CString to create a font family

Keywords: GDI+ , VC++ ,  CString , WCHAR , FontFamily , Convert

 

As we all know,in GID plus, when we want to create a new font , we should do something like this :

      FontFamily  fontFamily(L"隶书");

      Font    font( &fontFamily, 24, FontStyleRegular, UnitPixel );  

The problem is , the declare of this function is const WCHAR *,   Maybe you will encount an compile error like this :

 error C2664: '__thiscall Gdiplus::FontFamily::Gdiplus::FontFamily(const unsigned short *,const class Gdiplus::FontCollection *)' : 

cannot convert parameter 1 from 'class CString' to 'const unsigned short *'

  

what if we want to create a new font with CString parameters like 

CString strMyFontName;

FontFamily fontFamily(strMyFontName);

Just follow these steps to make it work:

CString strMyFontName("微软雅黑");  

string fface=(string)strMyFontName;

WCHAR *wfface = new WCHAR[fface.length()+1];

if(!MultiByteToWideChar(CP_ACP,0,fface.c_str(),static_cast<int>(fface.length())+1,wfface,static_cast<int>(fface.length())+1))

{

TRACE0("Error creating text actor.");

}

else

{

FontFamily fontFmly(wfface);

Font  font(&fontFmly, 12, FontStyleRegular, UnitPixel);

And don't forget to add include <string> in the top of the cpp file.  

 

Ray

2009-7  

 

 

 

posted @ 2009-07-10 17:21  RayG  阅读(474)  评论(0编辑  收藏  举报