大哥们好, 我在使用DBGrid撷取MySQL的columns时, MySQL里皆用utf8编码, 但截出来后所有的中文都变成乱码(如图), 我用的是delphi 2009 DBE 的TDatabase, 请问这如何解决….底下是程序代码
unit Unit2;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, Grids, DBGrids, StdCtrls, WideStrings, DBXMySql
;type
TForm2 = class(TForm)
Database1: TDatabase;
Table1: TTable;
DataSource1: TDataSource;
Button1: TButton;
DBGrid1: TDBGrid;
Query1: TQuery;
Query1type: TMemoField;
Query1id: TMemoField;
Query1filename: TMemoField;
Query1size: TIntegerField;
Query1time: TIntegerField;
Query1description: TMemoField;
Query1author: TMemoField;
Query1ipnr: TMemoField;
procedure Button1Click(Sender: TObject);
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState); private
{ Private declarations }
public
{ Public declarations }
end;var
Form2: TForm2;implementation{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
begin
close();
end;procedure TForm2.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
bmp: TBitmap;
OutRect: TRect;begin
with DbGrid1 do
beginCanvas.FillRect(Rect);
OutRect := Rect;
InflateRect(OutRect, -2, -2);
if column.Field is TGraphicField then
begin
bmp := TBitmap.Create;
try
bmp.Assign(Column.Field);
Canvas.StretchDraw(OutRect, Bmp);
finally
bmp.Free;
end;
end
else if Column.Field is TMemoField then
begin
DrawText(Canvas.Handle, Pchar(Column.Field.AsString),
Length(Column.Field.AsString), OutRect, dt_WordBreak or dt_NoPrefix);
end
else
DrawText(Canvas.Handle, Pchar(Column.Field.DisplayText),
Length(Column.Field.DisplayText),
OutRect, dt_WordBreak or dt_NoPrefix);
end;end;end.
unit Unit2;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, Grids, DBGrids, StdCtrls, WideStrings, DBXMySql
;type
TForm2 = class(TForm)
Database1: TDatabase;
Table1: TTable;
DataSource1: TDataSource;
Button1: TButton;
DBGrid1: TDBGrid;
Query1: TQuery;
Query1type: TMemoField;
Query1id: TMemoField;
Query1filename: TMemoField;
Query1size: TIntegerField;
Query1time: TIntegerField;
Query1description: TMemoField;
Query1author: TMemoField;
Query1ipnr: TMemoField;
procedure Button1Click(Sender: TObject);
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState); private
{ Private declarations }
public
{ Public declarations }
end;var
Form2: TForm2;implementation{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
begin
close();
end;procedure TForm2.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
bmp: TBitmap;
OutRect: TRect;begin
with DbGrid1 do
beginCanvas.FillRect(Rect);
OutRect := Rect;
InflateRect(OutRect, -2, -2);
if column.Field is TGraphicField then
begin
bmp := TBitmap.Create;
try
bmp.Assign(Column.Field);
Canvas.StretchDraw(OutRect, Bmp);
finally
bmp.Free;
end;
end
else if Column.Field is TMemoField then
begin
DrawText(Canvas.Handle, Pchar(Column.Field.AsString),
Length(Column.Field.AsString), OutRect, dt_WordBreak or dt_NoPrefix);
end
else
DrawText(Canvas.Handle, Pchar(Column.Field.DisplayText),
Length(Column.Field.DisplayText),
OutRect, dt_WordBreak or dt_NoPrefix);
end;end;end.
解决方案 »
- 散分,奸问个问题,不可视控件的问题
- ◆难道就没有人能回答这个问题吗◆
- excel在delphi中如何转换为html
- flyingworm兄进来接分~~~`:)
- 与报表有关
- 在局域网中,如何获得所有代理服务器的IP
- pc→单片机的串口通讯,使用spcomm控件,发送时好时坏,怀疑是pc与单片机不同步,应该怎么调?
- 数据隐藏
- 我想把DBlistBox控件与ADOQuery1绑定,DBlistBox =: ADOQuery1,这样写对吗?
- 一个让我痛苦已久的问题,望各路高手鼎立相助!!!
- 线程类中使用其他类的变量不能赋值
- 招delphi程序员(三个月短期项目,深圳福田区),熟悉网络应用开发、精通数据库应用开发
设置下DBGird Font属性的 选择宋体或者其他的中国字体后 右下角有个字符集 一般有 西方 和 chinese_GB2323 这2个属性设置看看