cocos2d-x显示中文

转载说明:http://blog.csdn.net/jukaiblog/article/details/8770520


该方法不被原作者推荐,只是我推荐



bool HelloWorld::init()  
{  
    bool bRet = false;  
    do   
    {  
        //  
        // super init first  
        //  
  
        CC_BREAK_IF(! CCLayer::init());  
  
        //  
        // add your codes below...  
        //  
  
        // 1. Add a menu item with "X" image, which is clicked to quit the program.  
  
        // Create a "close" menu item with close icon, it's an auto release object.  
        CCMenuItemImage *pCloseItem = CCMenuItemImage::create(  
            "CloseNormal.png",  
            "CloseSelected.png",  
            this,  
            menu_selector(HelloWorld::menuCloseCallback));  
        CC_BREAK_IF(! pCloseItem);  
  
        // Place the menu item bottom-right conner.  
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));  
  
        // Create a menu with the "close" menu item, it's an auto release object.  
        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);  
        pMenu->setPosition(CCPointZero);  
        CC_BREAK_IF(! pMenu);  
  
        // Add the menu to HelloWorld layer as a child layer.  
        this->addChild(pMenu, 1);  
  
    //创建显示文本  
        CCLabelTTF * label=CCLabelTTF::create(FontToUTF8("这是“微软雅黑”字体类型的HelloWorld"),"微软雅黑",32);  
    //获取屏幕大小  
    CCSize size=CCDirector::sharedDirector()->getWinSize();  
    //设置文本大小和颜色  
    label->setPosition(ccp(size.width/2,size.height/2));  
    label->setColor(ccGREEN);  
    //添加进布景  
    addChild(label,1);  
  
        bRet = true;  
    } while (0);  
  
    return bRet;  
}  

我们还要在HelloWorld.h文件里声明用到的字符编码转换方法FontToUTF8,添加如下代码:

char* FontToUTF8(const char* font); 
最后实现此方法:

char* HelloWorld::FontToUTF8(const char* font)  
{  
    int len=MultiByteToWideChar(CP_ACP,0,font,-1,NULL,0);  
    wchar_t* wstr=new wchar_t[len+1];  
    memset(wstr,0,len+1);  
    MultiByteToWideChar(CP_ACP,0,font,-1,wstr,len);  
    len=WideCharToMultiByte(CP_UTF8,0,wstr,-1,NULL,0,NULL,NULL);  
    char* str=new char[len+1];  
    memset(str,0,len+1);  
    WideCharToMultiByte(CP_UTF8,0,wstr,-1,str,len,NULL,NULL);  
    if(wstr)delete[] wstr;  
    return str;  
}  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值