最近做了一个小程序,用到了SQLite,后台用Python写的分析程序,将数据插入(更新)到SQLite数据库中,Delphi的程序周期显示数据库的内容。Delphi访问SQLite采用的Aducom组件。Python插入的数据编码都是采用的UTF-8,而Delphi的DBGrid、cxGrid控件显示的却是乱码,主要是因为Delphi7不支持Unicode造成的,因此要想办法让他支持。
尝试了多种方法,包括使用据说支持Unicode的TMS Unicode Component、SUIPack等,都不好使。最后还是用了简单的方法,在数据集组件的需要显示的字段的OnGetText事件,在事件处理中,对数据进行Unicode到GB的转换。
procedure unicode2gb(const unicodestr:string; var GbStr:String);
var SourceLength:integer;
DoneLength:integer;
AscNo:integer;
Byte1,Byte2,Byte3:integer;
begin
GbStr:='';
if Trim(unicodestr)='' then exit;
SourceLength:=Length(UnicodeStr);
DoneLength:=1;
repeat
AscNo:=ord(UnicodeStr[DoneLength]);
case (AscNo and $E0) of
$E0:begin
Byte1:=(AscNo and $0f) shl 12;
Inc(DoneLength);
if DoneLength>SourceLength then break;
AscNo:=ord(UnicodeStr[DoneLength]);
Byte2:=(AscNo and $3f) shl 6;
Inc(DoneLength);
if DoneLength>SourceLength then break;
AscNo:=ord(UnicodeStr[DoneLength]);
Byte3:=AscNo and $3f;
end;
$C0:begin
Byte1:=(AscNo and $1f) shl 6;
Inc(DoneLength);
if DoneLength>SourceLength then break;
AscNo:=ord(UnicodeStr[DoneLength]);
Byte2:=(AscNo and $3f);
Byte3:=0;
end;
0..$bf:begin
Byte1:=AscNo;
Byte2:=0;
Byte3:=0;
end;
end;//case;
GbStr:=GBStr+widechar(Byte1+Byte2+Byte3);
Inc(DoneLength);
if DoneLength>SourceLength then break;
until DoneLength>=SourceLength;
end;
另外,在用cxGrid进行显示的时候,要根据字段的值进行颜色的设置,这个可以在TableView的Styles的OnGetContentStyle事件中进行处理,如下所示:
procedure TFormMain.cxGrid1DBTableView1StylesGetContentStyle(
Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
attention : integer;
begin
attention := ARecord.Values[5];
if attention > 0 then
begin
AStyle := styleAttention;
exit;
end;
if ARecord.Values[4] = 0 then
begin
AStyle := styleRed;
end
else
begin
AStyle := styleDefault;
end;
end;
其中styleAttention、styleDefault等是放在cxStyleRepository1中的设定好的各种Style。
本文出自 “景元的技术专栏” 博客,请务必保留此出处http://kernel.blog.51cto.com/920538/197288
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。