视图里显示中文的问题

baboai 2007-06-22 02:26:37
我目前用的ide是Carbide c++ v1.2。我想做一个中文显示的程序。当然不是在.rls或.loc里面已经写好的中文,而是在视图中直接写中文并可以直接显示出来。可能这样说很不清楚,我举例一下:
例如:
const TInt KColorRed=35;
TRgb colorRed=AKN_LAF_COLOR(95);
gc.SetPenStyle( CGraphicsContext::ENullPen );
gc.SetPenColor( colorRed );
gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
//Draw Text
TBuf<16> text(_L("中文显示"));// 1.如果这边写的是英语那是一定显示正常的,但是
// 中文就会有错误
TPoint textPoint(2,50);

TZoomFactor devicemap(iCoeEnv->ScreenDevice());
// Set zoom factor at 200%
devicemap.SetZoomFactor(TZoomFactor::EZoomOneToOne*1.5);
TFontSpec fontSpec(_L("Times New Roman"),200);
// find the nearest font to the specified one
CFont* screenFont;
devicemap.GetNearestFontInTwips(screenFont,fontSpec);
// use it for this graphics context
gc.UseFont(screenFont);
gc.DrawText(text,textPoint);
// discard and release font
gc.DiscardFont();
devicemap.ReleaseFont(screenFont);

如果是这样写的话,那么是无法正常显示中文的,但有时候必需这样写中文。据了解,symbian os 都是使用unicode码的。但是我不知道如何转换才能正常显示。

补充一下:
在.rls里面写中文并在文件的最开始加了一句 CHARACTER_SET UTF8(同样,.rss文件也都加上这句)之后把这些文件保存成UTF8 - 无 BOM 格式。这样执行程序就可以正常显示中文了,而在编译器上看到是乱码。但当我在编译器上的:project->properties->info 把 Text file encoding 改成others->UTF-8 格式,那么之前的乱码变成正确的中文字了,而且在执行程序中也可以正常显示。试问,编译器在这种编码格式下,直接在视图下写中文为什么在执行程序下不能正常显示呢?而.rls里的汉字就能正常显示呢?是否CHARACTER_SET UTF8这句就已经把他们转换成Unicode码呢,不过看上去不像才是。
...全文
503 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
baboai 2007-06-22
  • 打赏
  • 举报
回复
失误,draw函数没有那段
CCnvCharacterSetConverter* converter=CCnvCharacterSetConverter::NewLC();

if(converter->PrepareToConvertToOrFromL(KCharacterSetIdentifierGbk,
iEikonEnv->FsSession())!=CCnvCharacterSetConverter::EAvailable)
User::Leave(KErrNotSupported);
baboai 2007-06-22
  • 打赏
  • 举报
回复
谢谢。
忘了说一下,我用文本标签也是可以正常显示的。这段是标签的代码(网上找到的)写在ConstructL里的。


CFont *font;

CWsScreenDevice *aDeviceMap = iEikonEnv->ScreenDevice();

TInt height = 200; // The font size in twips
TFontSpec fontSpec(_L( "System"), height );

// If the proper font libaray exist, can call the specific font
// TFontSpec fontSpec(_L("MS Song"), height );
User::LeaveIfError(aDeviceMap->GetNearestFontInTwips(font, fontSpec));

// Use system font
iLabel->SetFont( font );
aDeviceMap->ReleaseFont( font );


CCnvCharacterSetConverter* converter=CCnvCharacterSetConverter::NewLC();
// Check if there is conversion between GBK/GB2312 and unicode
// if(converter->PrepareToConvertToOrFromL(KCharacterSetIdentifierGb2312,
if(converter->PrepareToConvertToOrFromL(KCharacterSetIdentifierGbk,
iEikonEnv->FsSession())!=CCnvCharacterSetConverter::EAvailable)
User::Leave(KErrNotSupported);

// The following string is "Nokia 7650C" in Chinese
TText8 *str = (TText8*)"诺基亚7650C";
TInt state=CCnvCharacterSetConverter::KStateDefault;

HBufC* iInfoText;
TPtrC8 source( str );
iInfoText = HBufC::NewL( source.Length() );
TPtr16 ptr = iInfoText->Des();

if(CCnvCharacterSetConverter::EErrorIllFormedInput == converter->ConvertToUnicode(ptr, source, state))
User::Leave(KErrArgument); //Leave if error in conversion.

iLabel->SetTextL( ptr );

就是在Draw函数下没有办法正常。不正常表现是:程序编译不会有错,但是在打开执行程序的时候则无法打开而错误退出。
我在container里这样写的
ConstructlL()
{
...

converter=CCnvCharacterSetConverter::NewLC();
// Check if there is conversion between GBK/GB2312 and unicode
// if(converter->PrepareToConvertToOrFromL(KCharacterSetIdentifierGb2312,
if(converter->PrepareToConvertToOrFromL(KCharacterSetIdentifierGbk,
iEikonEnv->FsSession())!=CCnvCharacterSetConverter::EAvailable)
User::Leave(KErrNotSupported);
...
}

container::Draw( )
{
...
const TInt KColorRed=35;
TRgb colorRed=AKN_LAF_COLOR(KColorRed);
gc.SetPenStyle( CGraphicsContext::ENullPen );
gc.SetPenColor( colorRed );
gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
//Draw Text
TPoint textPoint(2,50);

CCnvCharacterSetConverter* converter=CCnvCharacterSetConverter::NewLC();

if(converter->PrepareToConvertToOrFromL(KCharacterSetIdentifierGbk,
iEikonEnv->FsSession())!=CCnvCharacterSetConverter::EAvailable)
User::Leave(KErrNotSupported);
HBufC* iInfoText;
TBuf<16> text(_L("诺基亚"));
TText8 *str = (TText8*)"诺基亚7650C";
TPtrC8 source( str );
TInt state=CCnvCharacterSetConverter::KStateDefault;
iInfoText = HBufC::NewL( source.Length() );
TPtr16 ptr = iInfoText->Des();
if(CCnvCharacterSetConverter::EErrorIllFormedInput == converter->ConvertToUnicode(ptr, source, state))
User::Leave(KErrArgument); //Leave if error in conversion.
TZoomFactor devicemap(iCoeEnv->ScreenDevice());
// Set zoom factor at 200%
devicemap.SetZoomFactor(TZoomFactor::EZoomOneToOne*1.5);
TFontSpec fontSpec(_L("Times New Roman"),200);
// find the nearest font to the specified one
CFont* screenFont;
devicemap.GetNearestFontInTwips(screenFont,fontSpec);
// use it for this graphics context
gc.UseFont(screenFont);
gc.DrawText(ptr,textPoint);
// discard and release font
gc.DiscardFont();
devicemap.ReleaseFont(screenFont);
}
这样还是无法正常显示,会有错误。请问是什么原因呢?
Casper1314 2007-06-22
  • 打赏
  • 举报
回复
需要调用CCnvCharacterSetConverter类的ConvertToUnicode方法将GBk/GBK2312转换为Unicode。
因为你在View输入的汉字是GBK的,转换位Unicode在手机中才能正常显示。
或者将你的rls文件编码格式转为Unicode,TResourceReader读到的字符就是Unicode,不用再转换

3,120

社区成员

发帖
与我相关
我的任务
社区描述
塞班系统(Symbian系统)是塞班公司为手机而设计的操作系统,它的前身是英国宝意昂公司的 EP ( Electronic Piece of cheese)操作系统。
社区管理员
  • Symbian社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧