首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi OLE 容器開發:拖放釋放 OLE 對象、文件保存載入、剪貼板註冊格式、粘貼對話框

     編輯:Delphi

8.3.5 拖放釋放 OLE 對象

從 OLE 服務端拖動對象放入 OLE 應用程序,無需插入/粘貼對話框,僅需鼠標拖放。釋放時觸發窗體 OnDragDrop 事件,Source 參數為 TOLEDropNotify 派生類型,其 PInitInfo 屬性對應 OLE 容器 PInitInfo。賦值即實現釋放功能:

procedure TOLEFrameForm.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
var NewChild: TOLEObjectForm;
begin
  if Source is TOLEDropNotify then
  begin
    NewChild := CreateChild;
    with Source as TOLEDropNotify do
      NewChild.OLEContainer.PInitInfo := PInitInfo;
  end;
end;

注意:不要 用 ReleaseOLEInitInfo 釋放內存,Delphi 自動處理。

8.3.6 文件中的 OLE 對象

  • 鏈接對象:數據保存在服務端文件,修改自動同步
  • 嵌入對象:數據保存在應用程序窗體,需存入特殊 OLE 文件(.OLE)

OLEContainer.SaveToFile('C:\SALES.OLE') 保存;OLEContainer.LoadFromFile('C:\SALES.OLE') 載入。例程用保存/打開對話框實現。

對話框屬性:

屬性保存對話框打開對話框
NameSaveAsDialogOpenDialog
DefaultExtoleole
FileName.OLE.OLE
FilterOLE files (*.OLE)|*.OLEOLE files (*.OLE)|*.OLE
// 文件|保存
procedure TOLEObjectForm.SaveAs1Click(Sender: TObject);
begin
  if SaveAsDialog.Execute then
    OLEContainer.SaveToFile(SaveAsDialog.FileName);
end;

// 文件|打開
procedure TOLEFrameForm.Open1Click(Sender: TObject);
var NewChild: TOLEObjectForm;
begin
  if OpenDialog.Execute then
  begin
    NewChild := CreateChild;
    NewChild.OLEContainer.LoadFromFile(OpenDialog.FileName);
  end;
end;

8.3.4 粘貼 OLE 對象

8.3.4.1 粘貼對話框

Delphi 無粘貼對話框組件,用 PasteSpecialDlg 函數顯示:

function PasteSpecialDlg(Form: TForm; const Formats: array; HelpContext: THelpContext;
  var Format: Word; var Handle: THandle; var PInitInfo: Pointer): Boolean;

參數:Form(擁有對話框窗體)、Formats(註冊對象格式數組,BOLEFormat 類型)、HelpContext(在線幫助,0 為無)、Format(返回剪貼板格式)、Handle(數據句柄)、PInitInfo(初始化信息指針)。

8.3.4.2 剪貼板註冊 OLE 格式

主窗體 OnCreate 註冊格式:

procedure TOLEFrameForm.FormCreate(Sender: TObject);
begin
  FEmbedClipFmt := RegisterClipboardFormat('Embedded Object');
  FLinkClipFmt := RegisterClipboardFormat('Link Source');
  Fmts[0].fmtId := FEmbedClipFmt;
  Fmts[0].fmtMedium := BOLEMediumCalc(FEmbedClipFmt);
  Fmts[0].fmtIsLinkable := False;
  StrPCopy(Fmts[0].fmtName, '%s');
  StrPCopy(Fmts[0].fmtResultName, '%s');
  Fmts[1].fmtId := FLinkClipFmt;
  Fmts[1].fmtMedium := BOLEMediumCalc(FLinkClipFmt);
  Fmts[1].fmtIsLinkable := True;
  StrPCopy(Fmts[1].fmtName, '%s');
  StrPCopy(Fmts[1].fmtResultName, '%s');
  RegisterFormAsOleDropTarget(Self, Fmts);
end;

RegisterClipboardFormat 返回 Word 唯一標識新格式。FEmbedClipFmt/FLinkClipFmt 為私有成員。BOLEFormat 定義於 BOLEDefs,BOLEMediumCalc 於 ToCtrl,需在 uses 引入。

粘貼菜單實現

procedure TOLEObjectForm.PasteSpecial1Click(Sender: TObject);
var ClipFmt: Word; DataHand: THandle; Info: Pointer;
begin
  if PasteSpecialEnabled(Self, OLEFrameForm.Fmts) then
    if PasteSpecialDlg(Self, OLEFrameForm.Fmts, 0, ClipFmt, DataHand, Info) then
      InitializeOLEObject(Info);
end;

// 編輯菜單點擊啟用粘貼項
procedure TOLEObjectForm.Edit1Click(Sender: TObject);
begin
  PasteSpecial1.Enabled := PasteSpecialEnabled(Self, OLEFrameForm.Fmts);
end;

PasteSpecialEnabled 判斷剪貼板是否有 Fmts 定義格式,有則啟用粘貼對話框。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved