سوال اخریتون این بود :
میخوام برنامه notepad رو داخل برنامم اجرا کنم (چسبیده به خود برنامه... مثل memo...)
ole هم یه روش بود من از کجا بدونم واسه شما پیش پا افتادست؟
اینم یه یونیت که راهنمایی میکنه
کد:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, OleCtnrs, StdCtrls;
type
TForm1 = class(TForm)
OleContainer1: TOleContainer;
Panel1: TPanel;
Panel2: TPanel;
procedure FormShow(Sender: TObject);
end;
// Properties changed from defaults:
// Panel1.Align : alTop
// Panel2.Align : alClient
// OleContainer1.Align : alClient
// OleContainer1.AutoActivate : aaManual
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormShow(Sender: TObject);
const
Filename = 'C:\Documents and Settings\Gebruiker\Bureaublad\OleEmbed2\helloworld.odt';
begin
OleContainer1.CreateObjectFromFile(Filename, False);
OleContainer1.Run;
OleContainer1.DoVerb(ovPrimary);
OleContainer1.DoVerb(ovInPlaceActivate);
OleContainer1.DoVerb(ovShow);
end;
end.
یه سمپل هم واسه اکسل :
کد:
uses ComObj;
procedure TForm1.Button1Click(Sender: TObject);
var
objExcel, Sheet: OleVariant;
begin
objExcel := CreateOleObject('Excel.Application');
ObjExcel.Visible := True;
objExcel.WorkBooks.OpenText('C:\Docs\Test1.csv');
Sheet := objExcel.WorkSheets[1];
Caption := Sheet.Name;
end;
یه ole واسه ورد
کد:
function OpenWordFile(const FName: string): string;
var wordText: string;
begin
openWord := CreateOleObject('Word.Application');
openWord.Visible := False;
openWord.Documents.Open(FName);
openWord.ActiveDocument.Select;
wordText:= openWord.Selection.Text;
openWord.ActiveDocument.Close;
openWord.Quit;
openWord := unassigned;
/////
Result:= wordText;
end;
یه سمپل دیگه :
کد:
procedure TMainForm.ButtonClick(Sender: TObject);
begin
UpdateDataSource();
//Here you have just updated test.txt, so you should wait for it, not text.doc!
//May be it is clearner if you move this line to the end of UpdateDataSource()
WaitForFile('C:\test.txt'); //Changed .doc to .txt
OleContainer1.CreateObjectFromFile('c:\test.doc',false);
RefreshOle(OleContainer1);
OleContainer1.DoVerb(ovShow);
//Word has both files in use, you need to release them!
//May be this line was the missing one: If you use a nw createobjectfromfile each time, several word instances will remain running in the background (check it with task manager) so files remain in use until system is rebooted.
//Wait until word closes (user will do it) the file and it is totally released...
WaitForFile('test.doc');
//Kill word object to be sure txt file is also released...
OleContainer1.DestroyObject;
//Wait and see if the file is released...
WaitForFile('test.txt');
end;
و یک یونیت بسار کامل :
کد:
{ This sample application demonstrates the following features of the
TOLEContainer:
- Toolbar negotiation
- Status bar hints while inplace editing
- Using the TOLEContainer's dialogs including InsertObject,
ObjectProperties and PasteSpecial.
- Using the TOLEContainer's constructors CreateLinkToFile,
CreateObjectFromFile.
- Menu merging during in-place activation
}
unit sdimain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, Buttons, ExtCtrls, Menus, OleCtnrs, StdCtrls;
type
TMainForm = class(TForm)
MainMenu1: TMainMenu;
File1: TMenuItem;
Exit1: TMenuItem;
Help1: TMenuItem;
About1: TMenuItem;
Toolbar: TPanel;
SpeedButton1: TSpeedButton;
LinkButton: TSpeedButton;
CopyButton: TSpeedButton;
CutButton: TSpeedButton;
PasteButton: TSpeedButton;
OpenButton: TSpeedButton;
OpenDialog1: TOpenDialog;
StatusPanel: TPanel;
StatusBar: TStatusBar;
Save1: TMenuItem;
SaveAs1: TMenuItem;
SaveDialog1: TSaveDialog;
Open1: TMenuItem;
N2: TMenuItem;
SaveButton: TSpeedButton;
Edit1: TMenuItem;
Object1: TMenuItem;
N4: TMenuItem;
PasteSpecial1: TMenuItem;
Paste1: TMenuItem;
Copy1: TMenuItem;
Cut1: TMenuItem;
New1: TMenuItem;
Panel1: TPanel;
OleContainer1: TOleContainer;
procedure Exit1Click(Sender: TObject);
procedure Copy1Click(Sender: TObject);
procedure Paste1Click(Sender: TObject);
procedure Object2Click(Sender: TObject);
procedure LinkButtonClick(Sender: TObject);
procedure About1Click(Sender: TObject);
procedure Save1Click(Sender: TObject);
procedure File1Click(Sender: TObject);
procedure Open1Click(Sender: TObject);
procedure CutButtonClick(Sender: TObject);
procedure New1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Object1Click(Sender: TObject);
procedure Cut1Click(Sender: TObject);
procedure PasteSpecial1Click(Sender: TObject);
procedure Edit1Click(Sender: TObject);
private
ObjectFileName: TFileName;
InsertCanceled: Boolean;
procedure ShowHint(Sender: TObject);
end;
var
MainForm: TMainForm;
implementation
uses about;
{$R *.DFM}
procedure TMainForm.ShowHint(Sender: TObject);
begin
Statusbar.Panels[0].Text := Application.Hint;
end;
procedure TMainForm.Exit1Click(Sender: TObject);
begin
Close;
end;
procedure TMainForm.Copy1Click(Sender: TObject);
begin
OleContainer1.Copy;
PasteButton.Enabled := True;
end;
procedure TMainForm.Paste1Click(Sender: TObject);
begin
if (OleContainer1.State = osEmpty) or
(MessageDlg('Replace existing object?', mtConfirmation, mbOkCancel, 0) = mrOk) then
begin
OleContainer1.Paste;
CopyButton.Enabled := True;
CutButton.Enabled := True;
end;
end;
procedure TMainForm.Object2Click(Sender: TObject);
begin
if OleContainer1.State <> osEmpty then
OleContainer1.ObjectPropertiesDialog;
end;
procedure TMainForm.LinkButtonClick(Sender: TObject);
begin
if (OleContainer1.State = osEmpty) or
(MessageDlg('Replace existing object?', mtConfirmation, mbOkCancel, 0) = mrOk) then
with OpenDialog1 do
if OpenDialog1.Execute then
begin
OleContainer1.CreateLinkToFile(FileName, False);
ObjectFileName := FileName;
CutButton.Enabled := True;
CopyButton.Enabled := True;
PasteButton.Enabled := OleContainer1.CanPaste;
end;
end;
procedure TMainForm.About1Click(Sender: TObject);
begin
AboutBox := TAboutBox.Create(Self);
try
AboutBox.ShowModal;
finally
AboutBox.Free;
end;
end;
procedure TMainForm.Save1Click(Sender: TObject);
begin
with SaveDialog1 do
begin
SaveDialog1.FileName := ObjectFileName;
if (Length(FileName) = 0) or (Sender = SaveAs1) then
begin
if Execute then
begin
OleContainer1.SaveToFile(FileName);
ObjectFileName := FileName;
end
end
else
OleContainer1.SaveToFile(FileName);
end;
end;
procedure TMainForm.File1Click(Sender: TObject);
begin
with OleContainer1 do
begin
Save1.Enabled := Modified;
SaveAs1.Enabled := Modified;
end;
end;
procedure TMainForm.Open1Click(Sender: TObject);
begin
with OpenDialog1 do
if Execute then
begin
OleContainer1.CreateObjectFromFile(FileName, False);
ObjectFileName := FileName;
CutButton.Enabled := True;
CopyButton.Enabled := True;
PasteButton.Enabled := True;
end;
end;
procedure TMainForm.CutButtonClick(Sender: TObject);
begin
if OleContainer1.State <> osEmpty then
with OleContainer1 do
begin
Copy;
DestroyObject;
CopyButton.Enabled := False;
PasteButton.Enabled := OleContainer1.CanPaste;
ObjectFilename := '';
end;
end;
procedure TMainForm.New1Click(Sender: TObject);
begin
if (OleContainer1.State = osEmpty) or
(MessageDlg('Delete existing object?', mtConfirmation, mbOkCancel, 0) = mrOk) then
begin
InsertCanceled := false;
with OleContainer1 do
begin
DestroyObject;
Object1Click(Sender);
if not InsertCanceled then
begin
DoVerb(PrimaryVerb);
ObjectFileName := '';
end;
end
end;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
ObjectFileName := '';
Application.OnHint := ShowHint;
end;
procedure TMainForm.Object1Click(Sender: TObject);
begin
if (OleContainer1.State = osEmpty) or
(MessageDlg('Delete current OLE object?', mtConfirmation, mbOkCancel, 0) = mrOk) then
if OleContainer1.InsertObjectDialog then
begin
CutButton.Enabled := True;
CopyButton.Enabled := True;
PasteButton.Enabled := OleContainer1.CanPaste;
with OleContainer1 do
DoVerb(PrimaryVerb);
end
else
InsertCanceled := true;
end;
procedure TMainForm.Cut1Click(Sender: TObject);
begin
OleContainer1.Copy;
OleContainer1.DestroyObject;
CutButton.Enabled := False;
CopyButton.Enabled := False;
PasteButton.Enabled := OleContainer1.CanPaste;
end;
procedure TMainForm.PasteSpecial1Click(Sender: TObject);
begin
if (OleContainer1.State = osEmpty) or
(MessageDlg('Delete current OLE object?', mtConfirmation, mbOkCancel, 0) = mrOk) then
begin
if OleContainer1.PasteSpecialDialog then
begin
CutButton.Enabled := True;
CopyButton.Enabled := True;
PasteButton.Enabled := OleContainer1.CanPaste;
end;
end;
end;
procedure TMainForm.Edit1Click(Sender: TObject);
begin
with OleContainer1 do
begin
Cut1.Enabled := State <> osEmpty;
Copy1.Enabled := State <> osEmpty;
Paste1.Enabled := CanPaste;
PasteSpecial1.Enabled := CanPaste;
end;
end;
end.