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

📄 streamreadwrite_u1.~pas

📁 是一个delphi的流程制作软件
💻 ~PAS
字号:
unit StreamReadWrite_U1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Buttons,  ComCtrls,  ActiveX,  AxCtrls;
       
type
  TForm1 = class(TForm)
    mem_Text: TMemo;
    but_Save: TButton;
    but_Load: TButton;
    procedure but_SaveClick(Sender: TObject);
    procedure but_LoadClick(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


procedure TForm1.but_SaveClick(Sender: TObject);
var
   Hr : HResult;
   Stream : IStream;
   OleStream : TOleStream;
   RootStorage : IStorage;
begin
      {Try create the DocFile}
   Hr := StgCreateDocFile(  'c:\Temp\MyDocFile.ole',
                            STGM_CREATE or STGM_READWRITE or
                            STGM_DIRECT or STGM_SHARE_EXCLUSIVE,
                            0,
                            RootStorage
                          );

      {Was is created?}
   if(   not SUCCEEDED(  Hr  )   ) then
   begin
      (*  Fail  *)
      {D2}//RootStorage.Release;
      Exit;
   end;

      {Try to create the stream}
   Hr := RootStorage.CreateStream(  'MyStream',
                                    STGM_CREATE or STGM_READWRITE or
                                    STGM_DIRECT or STGM_SHARE_EXCLUSIVE,
                                    0,
                                    0,
                                    Stream
                                  );

      {Was is created?}
   if(   not SUCCEEDED(  Hr  )   ) then
   begin
      (*  Fail  *)
      {D2}//RootStorage.Release;
      Exit;
   end;


      {Create the OleStream}
   OleStream := TOleStream.Create(  Stream  );

      {Save the memo's text to the OleStream}
   mem_Text.Lines.SaveToStream(  OleStream  );

      {Finished with the OleStream stream}
   OleStream.Free;


   {D2}//Stream.Release;
   {D2}//RootStorage.Release;
end;



function GetStreamSize(  Stream : IStream  ) : LongInt;
var
   Hr : HResult;
   StatStg : TStatStg;
begin
      {Get the STATSTG info for the stream.
        Dont return the name (saves mem alloc & dealloc)}
   Hr := Stream.Stat(  StatStg,  STATFLAG_NONAME  );

      {Success?}
   if(   not SUCCEEDED(  Hr  )   ) then
   begin
      Result := -1;
      Exit;
   end;

      {Get the size as a LongInt}
   Result :=  StatStg.cbSize ;
end;



procedure TForm1.but_LoadClick(Sender: TObject);
var
   Hr : HResult;
   Stream : IStream;
   OleStream : TOleStream;
   RootStorage : IStorage;
begin
      {Try open the DocFile}
   Hr := StgOpenStorage(  'c:\Temp\MyDocFile.ole',
                          nil,
                          STGM_READWRITE or
                          STGM_DIRECT or STGM_SHARE_EXCLUSIVE,
                          nil,
                          0,
                          RootStorage
                        );

      {Was is opened?}
   if(   not SUCCEEDED(  Hr  )   ) then
   begin
      (*  Fail  *)
      Exit;
   end;

      {Try to open the stream}
   Hr := RootStorage.OpenStream(  'MyStream',
                                  nil,
                                  STGM_READWRITE or
                                  STGM_DIRECT or STGM_SHARE_EXCLUSIVE,
                                  0,
                                  Stream
                                );

      {Was is opened?}
   if(   not SUCCEEDED(  Hr  )   ) then
   begin
      (*  Fail  *)
      Exit;
   end;

      {Create the memory stream}
   OleStream := TOleStream.Create(  Stream  );

      {Load the data (if there is any)}
   if(  OleStream.Size > 0  ) then
      mem_Text.Lines.LoadFromStream(  OleStream  )
   else
      mem_Text.Clear; 

      {Finished with the OleStream}
   OleStream.Free;
end;




end.

⌨️ 快捷键说明

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