熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Delphi編程 >> 正文

復雜的結構化存取(二)

2022-06-13   來源: Delphi編程 

  本例效果圖

  

  代碼文件

unit Unit;

  interface

  uses
 Windows Messages SysUtils Variants Classes Graphics Controls Forms
 Dialogs StdCtrls;

  type
 TForm = class(TForm)
  Button: TButton;
  Button: TButton;
  procedure FormCreate(Sender: TObject);
  procedure ButtonClick(Sender: TObject);
  procedure ButtonClick(Sender: TObject);
 end;

  var
 Form: TForm;

  implementation

  {$R *dfm}

  Uses Activex;

  type
 TRec = record
  Name: string[];
  Age: Word;
 end;

  const FileName = C:TempTestdat;

  procedure TFormFormCreate(Sender: TObject);
begin
 ButtonCaption := 寫復合文件;
 ButtonCaption := 讀復合文件;
 Position := poDesktopCenter;
end;

  procedure TFormButtonClick(Sender: TObject);
const
 Mode = STGM_CREATE or STGM_READWRITE or STGM_SHARE_EXCLUSIVE;
var
 StgRoot StgSub: IStorage;
 Stm: IStream;
 Rec: TRec;
begin
 {建立根 IStorage: StgRoot}
 StgCreateDocfile(FileName Mode  StgRoot);

  {建立子 IStorage: StgSub}
 StgRootCreateStorage(StgSub Mode   StgSub);

  {在子 IStorage: StgSub 中建立 IStream: Stm}
 StgSubCreateStream(Stm Mode   Stm);

  {寫入數據}
 RecName := 張三;
 RecAge := ;
 StmWrite(@Rec SizeOf(TRec) nil);
end;

  procedure TFormButtonClick(Sender: TObject);
const
 Mode = STGM_READ or STGM_SHARE_EXCLUSIVE;
Var
 StgRoot StgSub :IStorage;
 Stm: IStream;
 Rec: TRec;
Begin
 {如果不是結構化存儲文件則退出}
 if StgIsStorageFile(FileName) <> S_OK then Exit;

  {獲取根 IStorage: StgRoot}
 StgOpenStorage(FileName nil Mode nil  StgRoot);

  {獲取子 IStorage: StgSub; 注意: 第一個參數的名稱必須和保存時一致}
 StgRootOpenStorage(StgSub nil Mode nil  StgSub);

  {獲取 IStream: Stm; 注意: 第一個參數的名稱必須和保存時一致}
 StgSubOpenStream(Stm nil Mode  Stm);

  {讀出數據}
 StmRead(@Rec SizeOf(TRec) nil);
 ShowMessageFmt(%s %d [RecName RecAge]);
end;

  end

    窗體文件

  object Form: TForm
 Left = 
 Top = 
 Caption = Form
 ClientHeight = 
 ClientWidth = 
 Color = clBtnFace
 FontCharset = DEFAULT_CHARSET
 FontColor = clWindowText
 FontHeight = 
 FontName = Tahoma
 FontStyle = []
 OldCreateOrder = False
 OnCreate = FormCreate
 PixelsPerInch = 
 TextHeight = 
 object Button: TButton
  Left = 
  Top = 
  Width = 
  Height = 
  Caption = Button
  TabOrder = 
  OnClick = ButtonClick
 end
 object Button: TButton
  Left = 
  Top = 
  Width = 
  Height = 
  Caption = Button
  TabOrder = 
  OnClick = ButtonClick
 end
end


From:http://tw.wingwit.com/Article/program/Delphi/201311/24675.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.