打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Lazarus?中文汉字解决方案

Lazarus 中文汉字解决方案

(2011-03-23 21:40:52)

使用Lazarus不得不面对编码问题,尤其中文。Lazarus使用的是UTF8编码,而很多windows程序使用的是ANSI编码,编码问题在此不多说大家可以google去。

ANSI数据库与Lazarus编程的解决方法:

1.全局设置一个isNeedANSI变量;

2.从数据库读取时:

function Tdmd.FromDBStr(str:string):string;
begin
       if IsNeedANSI then result:= ANSItoUTF8(str)
       else result:=str;
end;      

 

3.向数据库写入数据时:

function Tdmd.ToDBStr(dbstr:string):string;
begin

       if IsNeedANSI then result:= UTF8toANSI(dbstr)
       else result:=dbstr;

end;   

4.TDBGrid的修改:

  修改Grid文件里的TcustumerGrid类,添加一个非publish变量ConverType

修改如下过程:

procedure TCustomGrid.DrawCellText(aCol, aRow: Integer; aRect:TRect;

  aState: TGridDrawState; aText: String);

begin

 

  with ARect do begin

 

   dec(Right, 3);

    caseCanvas.TextStyle.Alignment of

     Classes.taLeftJustify: Inc(Left, 3);

     Classes.taRightJustify: Dec(Right, 1);

    end;

    caseCanvas.TextStyle.Layout of

     tlTop: Inc(Top, 3);

     tlBottom: Dec(Bottom, 3);

    end;

 

    ifRight<Left then

     Right:=Left;

    ifLeft>Right then

     Left:=Right;

    ifBottom<Top then

     Bottom:=Top;

    ifTop>Bottom then

     Top:=Bottom;

    //======www.aliyagoo.com========

   //根据不同的ConverType值可设置很多编码转换方法

    if self.ConverType=1 then

        aText:=ansitoutf8(aText);

    //==============================

    if(Left<>Right) and(Top<>Bottom) then

     Canvas.TextRect(aRect,Left,Top, aText);

 

  end;

end;      

附注:

lazarus能够支持的编码转换方式

function UnicodeToUtf8(Dest: PChar; Source:PWideChar; MaxBytes: SizeInt): SizeInt;{$ifdefSYSTEMINLINE}inline;{$endif} 
function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source:PWideChar; SourceChars: SizeUInt):SizeUInt; 
function Utf8ToUnicode(Dest: PWideChar; Source: PChar; MaxChars:SizeInt): SizeInt;{$ifdefSYSTEMINLINE}inline;{$endif} 
function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: SizeUInt;Source: PChar; SourceBytes: SizeUInt):SizeUInt; 
function UTF8Encode(const s : WideString) :UTF8String; 
function UTF8Decode(const s : UTF8String):WideString; 
function AnsiToUtf8(const s : ansistring): UTF8String;{$ifdefSYSTEMINLINE}inline;{$endif} 
function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdefSYSTEMINLINE}inline;{$endif} 
function WideStringToUCS4String(const s : WideString) :UCS4String; 
function UCS4StringToWideString(const s : UCS4String) :WideString;  

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
TMemoryStream、String与OleVariant互转
Delphi字符串函数大全
【翻译】NativeXml帮助(三)
Levenshtein distance
Dokan虚拟磁盘开发实战
Delphi 函数参考
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服