⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmwblob.pas

📁 一套及时通讯的原码
💻 PAS
字号:
{========================================================================}
{=                (c) 1995-98 SwiftSoft Ronald Dittrich                 =}
{========================================================================}
{=                          All Rights Reserved                         =}
{========================================================================}
{=  D 01099 Dresden             = Fax.: +49 (0)351-8037944              =}
{=  Loewenstr.7a                = info@swiftsoft.de                     =}
{========================================================================}
{=  Actual versions on http://www.swiftsoft.de/mmtools.html             =}
{========================================================================}
{=  This code is for reference purposes only and may not be copied or   =}
{=  distributed in any format electronic or otherwise except one copy   =}
{=  for backup purposes.                                                =}
{=                                                                      =}
{=  No Delphi Component Kit or Component individually or in a collection=}
{=  subclassed or otherwise from the code in this unit, or associated   =}
{=  .pas, .dfm, .dcu, .asm or .obj files may be sold or distributed     =}
{=  without express permission from SwiftSoft.                          =}
{=                                                                      =}
{=  For more licence informations please refer to the associated        =}
{=  HelpFile.                                                           =}
{========================================================================}
{=  $Date: 10.03.98 - 19:17:57 $                                        =}
{========================================================================}
unit MMWBlob;

{$I COMPILER.INC}

{.$DEFINE _APOLLO}

interface

uses
{$IFDEF WIN32}
    Windows,
{$ELSE}
    WinTypes,
    WinProcs,
{$ENDIF}
    SysUtils,
    Messages,
    Classes,
    Controls,
    DB,
    DBTables,
    DBCtrls,
    MMSystem,
    MMUtils,
    MMObj,
    MMWave
    {$IFDEF _APOLLO}
    ,ApoDSet
    {$ENDIF}
    ;

type
    TMMWaveBlob = class(TMMMemoryWave)
    private
       FUpdateCount: integer;
       FDataLink   : TFieldDataLink;
       FWaveLoaded : Boolean;

       procedure DataChange(Sender: TObject);
       function  GetDataField: string;
       function  GetDataSource: TDataSource;
       function  GetField: TField;
       function  GetReadOnly: Boolean;
       procedure SetDataField(const Value: string);
       procedure SetDataSource(Value: TDataSource);
       procedure SetReadOnly(Value: Boolean);
       procedure UpdateData(Sender: TObject);
       procedure LoadWave;

    protected
       procedure DoChanged; override;
       procedure Notification(AComponent: TComponent; Operation: TOperation); override;

    public
       constructor Create(AOwner: TComponent); override;
       destructor Destroy; override;

       property Field: TField read GetField;

    published
       property DataField: string read GetDataField write SetDataField;
       property DataSource: TDataSource read GetDataSource write SetDataSource;
       property ReadOnly: Boolean read GetReadOnly write SetReadOnly default False;
    end;

implementation

{== TMMWaveBlob ===============================================================}
constructor TMMWaveBlob.Create(AOwner: TComponent);
begin
   inherited Create(AOwner);

   FUpdateCount := 0;
   FDataLink := TFieldDataLink.Create;
   FDataLink.Control := Self;
   FDataLink.OnDataChange := DataChange;
   FDataLink.OnUpdateData := UpdateData;
end;

{-- TMMWaveBlob ---------------------------------------------------------------}
destructor TMMWaveBlob.Destroy;
begin
   FDataLink.Free;
   FDataLink := nil;

   inherited Destroy;
end;

{-- TMMWaveBlob ---------------------------------------------------------------}
procedure TMMWaveBlob.Notification(AComponent: TComponent; Operation: TOperation);
begin
   inherited Notification(AComponent, Operation);

   if (Operation = opRemove) and (FDataLink <> nil) and
      (AComponent = DataSource) then DataSource := nil;
end;

{-- TMMWaveBlob ---------------------------------------------------------------}
procedure TMMWaveBlob.SetDataSource(Value: TDataSource);
begin
   if not (FDataLink.DataSourceFixed and (csLoading in ComponentState)) then
      FDataLink.DataSource := Value;
   {$IFDEF WIN32}
   if (Value <> nil) then Value.FreeNotification(Self);
   {$ENDIF}
end;

{-- TMMWaveBlob ---------------------------------------------------------------}
function TMMWaveBlob.GetDataSource: TDataSource;
begin
   Result := FDataLink.DataSource;
end;

{-- TMMWaveBlob ---------------------------------------------------------------}
procedure TMMWaveBlob.SetDataField(const Value: string);
begin
   FDataLink.FieldName := Value;
end;

{-- TMMWaveBlob ---------------------------------------------------------------}
function TMMWaveBlob.GetDataField: string;
begin
   Result := FDataLink.FieldName;
end;

{-- TMMWaveBlob ---------------------------------------------------------------}
procedure TMMWaveBlob.SetReadOnly(Value: Boolean);
begin
   FDataLink.ReadOnly := Value;
end;

{-- TMMWaveBlob ---------------------------------------------------------------}
function TMMWaveBlob.GetReadOnly: Boolean;
begin
   Result := FDataLink.ReadOnly;
end;

{-- TMMWaveBlob ---------------------------------------------------------------}
function TMMWaveBlob.GetField: TField;
begin
   Result := FDataLink.Field;
end;

{-- TMMWaveBlob ---------------------------------------------------------------}
procedure TMMWaveBlob.DoChanged;
begin
   inherited DoChanged;

   inc(FUpdateCount);
   try
      if (FUpdateCount = 1) then
      begin
         if (FDataLink.Field is TBlobField) then
         begin
            FDataLink.Edit;
            FDataLink.Modified;
            FWaveLoaded := True;
         end;
      end;

   finally
      dec(FUpdateCount);
   end;
end;

{-- TMMWaveBlob ---------------------------------------------------------------}
procedure TMMWaveBlob.LoadWave;
var
  {$IFDEF _APOLLO}
  BlobStream: TStream;
  {$ELSE}
  BlobStream: TBlobStream;
  {$ENDIf}
begin
   if not FWaveLoaded and Assigned(FDataLink.Field) and (FDataLink.Field.IsBlob) then
   begin
      {$IFDEF _APOLLO}
      BlobStream := TApolloDataSet(FDataLink.Field.DataSet).CreateBlobStream(TBlobField(FDataLink.Field), bmRead);
      {$ELSE}
      BlobStream := TBlobStream.Create(TBlobField(FDataLink.Field), bmRead);
      {$ENDIf}
      try
         if (BlobStream.Size > 0) then Wave.LoadFromStream(BlobStream);
      finally
         BlobStream.Free;
      end;
  end;
end;

{-- TMMWaveBlob ---------------------------------------------------------------}
procedure TMMWaveBlob.DataChange(Sender: TObject);
begin
   inc(FUpdateCount);
   try
      if (FUpdateCount = 1) then
      begin
         Wave.FreeWave;
         FWaveLoaded := False;
         LoadWave;
      end;
   finally
      dec(FUpdateCount);
   end;
end;

{-- TMMWaveBlob ---------------------------------------------------------------}
procedure TMMWaveBlob.UpdateData(Sender: TObject);
var
  {$IFDEF _APOLLO}
  BlobStream: TStream;
  {$ELSE}
  BlobStream: TMemoryStream;
  {$ENDIf}
begin
   if FDataLink.Field.IsBlob then
   begin
      {$IFDEF _APOLLO}
      BlobStream := TApolloDataSet(FDataLink.Field.DataSet).CreateBlobStream(TBlobField(FDataLink.Field), bmRead);
      try
         Wave.SaveToStream(BlobStream);
      finally
         BlobStream.Free;
      end;
      {$ELSE}
      BlobStream := TMemoryStream.Create;
      try
         Wave.SaveToStream(BlobStream);
         Blobstream.Position := 0;
         (FDataLink.Field as TBlobField).LoadFromStream(BlobStream);
      finally
         BlobStream.Free;
      end;
      {$ENDIf}
   end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -