Lazarus 中文汉字解决方案

使用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);

case Canvas.TextStyle.Alignment of

Classes.taLeftJustify: Inc(Left, 3);

Classes.taRightJustify: Dec(Right, 1);

end;

case Canvas.TextStyle.Layout of

tlTop: Inc(Top, 3);

tlBottom: Dec(Bottom, 3);

end;



if Right<Left then

Right:=Left;

if Left>Right then

Left:=Right;

if Bottom<Top then

Bottom:=Top;

if Top>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;{$ifdef SYSTEMINLINE}inline;{$endif} 
function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PWideChar; SourceChars: SizeUInt): SizeUInt; 
function Utf8ToUnicode(Dest: PWideChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}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;{$ifdef SYSTEMINLINE}inline;{$endif} 
function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdef SYSTEMINLINE}inline;{$endif} 
function WideStringToUCS4String(const s : WideString) : UCS4String; 
function UCS4StringToWideString(const s : UCS4String) : WideString; 

posted @   Thenext  阅读(1201)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示