L
lishding
Unregistered / Unconfirmed
GUEST, unregistred user!
我想自已写一个 存取JPG到数据库中的组件,已基本完工,
但安装后,碰到以下问题:
DBJPEG1联上数据后,数据集打开,关闭,或滚动,DBJPEG1的内容不
跟着更新,就和 数据集用了 DisableControls 的效果一样,
但把联着相同数据源的DBGrid的任一列的宽度调整一下就可正常使用.
完整代码如下:(复制到Delphi中就可用了,我用的是D6)
unit PowBitBtn;
interface
uses
Windows, Messages, SysUtils, Classes, StdCtrls, Graphics, Controls,
ExtCtrls, DB, DbCtrls, Variants,Jpeg;
type
TJPGFieldDataLink=class;
{ 显示JPEG的数据组件 }
TDBJPEG = class(TImage)
private
FDataLink: TJPGFieldDataLink;
function GetDataSource: TDataSource;
procedure SetDataSource(Value: TDataSource);
function GetDataField: String;
procedure SetDataField(const value:String);
protected
{ Protected declarations }
procedure DoUpdateImage;
procedure OnDataChange(Sender:TObject);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// procedure dbClick; override;
published
property DataSource: TDataSource read GetDataSource write SetDataSource;
property DataField: string read GetDataField write SetDataField;
end;
{ TJPGFieldDataLink }
TJPGFieldDataLink = class(TFieldDataLink)
private
FDBJPEG: TDBJPEG;
protected
procedure ActiveChanged; override;
procedure RecordChanged(Field: TField); override;
procedure FocusControl(Field: TFieldRef); override;
procedure UpdateData; override;
procedure DataSetScrolled(Distance:Integer); override;
public
constructor Create(DBJPEG: TDBJPEG);
destructor Destroy; override;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('MyPackage', [TPowBitBtn,TDBJPEG]);
end;
{*********************** 显示JPEG的数据组件 ********************}
constructor TDBJPEG.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDataLink := TJPGFieldDataLink.Create(Self);
DataSource:=nil;
DataField:='';
Center:=True;
end;
destructor TDBJPEG.Destroy;
begin
FDataLink.Free;
FDataLink := nil;
inherited Destroy;
end;
function TDBJPEG.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;
procedure TDBJPEG.SetDataSource(Value: TDataSource);
begin
FDataLink.DataSource := Value;
OnDataChange(self);
if Value <> nil then Value.FreeNotification(Self);
end;
function TDBJPEG.GetDataField: string;
begin
Result := FDataLink.FieldName;
end;
procedure TDBJPEG.SetDataField(const Value: string);
begin
FDataLink.FieldName := Value;
OnDataChange(self)
end;
{ 更新图片 }
procedure TDBJPEG.DoUpdateImage;
var BloField:TBlobField;
Strm:TMemoryStream;
JPG:TJPEGImage;
begin
if not Assigned(FDataLink.DataSet) then
exit;
if not (FDataLink.Field is TBlobField) then
exit;
if not(FDataLink.Editing) then
exit;
BloField:=FDataLink.Field as TBlobField;
Strm:=TMemoryStream.create();
JPG:=TJPEGImage.create();
try
if (Picture=nil) or (Picture.Graphic=Nil) then
BloField.Clear()
else
begin
JPG.Assign(Picture.Graphic);
JPG.SaveToStream(Strm);
Strm.position:=0;
BloField.loadFromStream(Strm);
end;
Finally
Strm.Free();
JPG.Free();
end;
end;
{ 显示图片 }
procedure TDBJPEG.OnDataChange(Sender:TObject);
var BloField:TBlobfield;
Strm:TMemoryStream;
Jpeg:TJPEGImage;
begin
if not(Assigned(FDataLink.DataSet)) then
exit;
if not(FDataLink.Field is TBlobField) then
exit;
if not(FDataLink.Active) then
begin
Picture:=nil;
exit;
end;
BloField:=FDataLink.Field as TBlobField;
Try
Strm:=TMemoryStream.Create();
Jpeg:=TJPEGImage.Create();
BloField.SaveToStream(Strm);
if not(strm.Size=0) then
begin
Strm.Position:=0;
Jpeg.LoadFromStream(Strm);
Picture.Assign(Jpeg);
end
else
Picture:=nil;
Finally
Strm.Free();
Jpeg.Free();
end;
end;
{****************** TJPGFieldDataLink *****************}
constructor TJPGFieldDataLink.Create(DBJPEG: TDBJPEG);
begin
inherited Create;
FDBJPEG := DBJPEG;
// VisualControl := True;
end;
destructor TJPGFieldDataLink.Destroy;
begin
FDBJPEG := nil;
inherited Destroy;
end;
procedure TJPGFieldDataLink.ActiveChanged ;
begin
if FDBJPEG<>nil then FDBJPEG.OnDataChange(self);
end;
procedure TJPGFieldDataLink.RecordChanged(Field: TField) ;
begin
if (FDBJPEG<>nil) then
FDBJPEG.OnDataChange(Self);
end;
procedure TJPGFieldDataLink.UpdateData;
begin
if FDBJPEG<>nil then FDBJPEG.DoUpdateImage;
end;
procedure TJPGFieldDataLink.FocusControl(Field: TFieldRef);
begin
if FDBJPEG<>nil then FDBJPEG.OnDataChange(self);
end;
procedure TJPGFieldDataLink.DataSetScrolled(Distance:Integer);
begin
if FDBJPEG<>nil then FDBJPEG.OnDataChange(self);
end;
end.
但安装后,碰到以下问题:
DBJPEG1联上数据后,数据集打开,关闭,或滚动,DBJPEG1的内容不
跟着更新,就和 数据集用了 DisableControls 的效果一样,
但把联着相同数据源的DBGrid的任一列的宽度调整一下就可正常使用.
完整代码如下:(复制到Delphi中就可用了,我用的是D6)
unit PowBitBtn;
interface
uses
Windows, Messages, SysUtils, Classes, StdCtrls, Graphics, Controls,
ExtCtrls, DB, DbCtrls, Variants,Jpeg;
type
TJPGFieldDataLink=class;
{ 显示JPEG的数据组件 }
TDBJPEG = class(TImage)
private
FDataLink: TJPGFieldDataLink;
function GetDataSource: TDataSource;
procedure SetDataSource(Value: TDataSource);
function GetDataField: String;
procedure SetDataField(const value:String);
protected
{ Protected declarations }
procedure DoUpdateImage;
procedure OnDataChange(Sender:TObject);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// procedure dbClick; override;
published
property DataSource: TDataSource read GetDataSource write SetDataSource;
property DataField: string read GetDataField write SetDataField;
end;
{ TJPGFieldDataLink }
TJPGFieldDataLink = class(TFieldDataLink)
private
FDBJPEG: TDBJPEG;
protected
procedure ActiveChanged; override;
procedure RecordChanged(Field: TField); override;
procedure FocusControl(Field: TFieldRef); override;
procedure UpdateData; override;
procedure DataSetScrolled(Distance:Integer); override;
public
constructor Create(DBJPEG: TDBJPEG);
destructor Destroy; override;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('MyPackage', [TPowBitBtn,TDBJPEG]);
end;
{*********************** 显示JPEG的数据组件 ********************}
constructor TDBJPEG.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDataLink := TJPGFieldDataLink.Create(Self);
DataSource:=nil;
DataField:='';
Center:=True;
end;
destructor TDBJPEG.Destroy;
begin
FDataLink.Free;
FDataLink := nil;
inherited Destroy;
end;
function TDBJPEG.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;
procedure TDBJPEG.SetDataSource(Value: TDataSource);
begin
FDataLink.DataSource := Value;
OnDataChange(self);
if Value <> nil then Value.FreeNotification(Self);
end;
function TDBJPEG.GetDataField: string;
begin
Result := FDataLink.FieldName;
end;
procedure TDBJPEG.SetDataField(const Value: string);
begin
FDataLink.FieldName := Value;
OnDataChange(self)
end;
{ 更新图片 }
procedure TDBJPEG.DoUpdateImage;
var BloField:TBlobField;
Strm:TMemoryStream;
JPG:TJPEGImage;
begin
if not Assigned(FDataLink.DataSet) then
exit;
if not (FDataLink.Field is TBlobField) then
exit;
if not(FDataLink.Editing) then
exit;
BloField:=FDataLink.Field as TBlobField;
Strm:=TMemoryStream.create();
JPG:=TJPEGImage.create();
try
if (Picture=nil) or (Picture.Graphic=Nil) then
BloField.Clear()
else
begin
JPG.Assign(Picture.Graphic);
JPG.SaveToStream(Strm);
Strm.position:=0;
BloField.loadFromStream(Strm);
end;
Finally
Strm.Free();
JPG.Free();
end;
end;
{ 显示图片 }
procedure TDBJPEG.OnDataChange(Sender:TObject);
var BloField:TBlobfield;
Strm:TMemoryStream;
Jpeg:TJPEGImage;
begin
if not(Assigned(FDataLink.DataSet)) then
exit;
if not(FDataLink.Field is TBlobField) then
exit;
if not(FDataLink.Active) then
begin
Picture:=nil;
exit;
end;
BloField:=FDataLink.Field as TBlobField;
Try
Strm:=TMemoryStream.Create();
Jpeg:=TJPEGImage.Create();
BloField.SaveToStream(Strm);
if not(strm.Size=0) then
begin
Strm.Position:=0;
Jpeg.LoadFromStream(Strm);
Picture.Assign(Jpeg);
end
else
Picture:=nil;
Finally
Strm.Free();
Jpeg.Free();
end;
end;
{****************** TJPGFieldDataLink *****************}
constructor TJPGFieldDataLink.Create(DBJPEG: TDBJPEG);
begin
inherited Create;
FDBJPEG := DBJPEG;
// VisualControl := True;
end;
destructor TJPGFieldDataLink.Destroy;
begin
FDBJPEG := nil;
inherited Destroy;
end;
procedure TJPGFieldDataLink.ActiveChanged ;
begin
if FDBJPEG<>nil then FDBJPEG.OnDataChange(self);
end;
procedure TJPGFieldDataLink.RecordChanged(Field: TField) ;
begin
if (FDBJPEG<>nil) then
FDBJPEG.OnDataChange(Self);
end;
procedure TJPGFieldDataLink.UpdateData;
begin
if FDBJPEG<>nil then FDBJPEG.DoUpdateImage;
end;
procedure TJPGFieldDataLink.FocusControl(Field: TFieldRef);
begin
if FDBJPEG<>nil then FDBJPEG.OnDataChange(self);
end;
procedure TJPGFieldDataLink.DataSetScrolled(Distance:Integer);
begin
if FDBJPEG<>nil then FDBJPEG.OnDataChange(self);
end;
end.