博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lazarus 中文汉字解决方案
阅读量:4306 次
发布时间:2019-06-06

本文共 2153 字,大约阅读时间需要 7 分钟。

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

转载于:https://www.cnblogs.com/Thenext/p/9405930.html

你可能感兴趣的文章
Hive面试题干货(亲自跟着做了好几遍,会了的话对面试大有好处)
查看>>
力扣题解-230. 二叉搜索树中第K小的元素(递归方法,中序遍历解决)
查看>>
力扣题解-123. 买卖股票的最佳时机 III(动态规划)
查看>>
Django 源码阅读:服务启动(wsgi)
查看>>
Django 源码阅读:url解析
查看>>
Docker面试题(一)
查看>>
第一轮面试题
查看>>
2020-11-18
查看>>
Docker面试题(二)
查看>>
一、redis面试题及答案
查看>>
消息队列2
查看>>
C++ 线程同步之临界区CRITICAL_SECTION
查看>>
测试—自定义消息处理
查看>>
MFC中关于虚函数的一些问题
查看>>
根据图层名获取图层和图层序号
查看>>
规范性附录 属性值代码
查看>>
提取面狭长角
查看>>
Arcsde表空间自动增长
查看>>
Arcsde报ora-29861: 域索引标记为loading/failed/unusable错误
查看>>
记一次断电恢复ORA-01033错误
查看>>