(五)TMainForm的事件和函數 和 TPrinter打印應用
////////// 打印知識
[菜單項file打印] 事件:
procedure TMainForm.filePrintItemClick(Sender: TObject);
begin
{kh1}print;{kh2}
Screen.Cursor := crHourglass;
try
if MDIChildCount<>0 then
with ActiveMDIChild as TMDIChild do begin
with Printer do begin
BeginDoc; //開始打印
Canvas.Draw(0,0,Image1.Picture.Graphic); //圖形輸出
EndDoc; //結束打印
end;
end;
finally
Screen.Cursor := crDefault;
end;
end;
[菜單項file打印設置] 事件:
procedure TMainForm.filePrintSetClick(Sender: TObject);
begin
Screen.Cursor:=crHourglass;
PrinterSetupDialog1.Execute ;
Screen.Cursor:=crDefault;
end;
[菜單項editPreferencesToolBar] 事件:
procedure TMainForm.toolbarItemClick(Sender: TObject);
begin
toolbarItem.Checked := not toolbarItem.Checked ;
ToolBar2.Visible := toolbarItem.Checked;
end;
[MainMenu1.OnChange] 事件
procedure TMainForm.MainMenu1Change(...);
begin
filePrintItem.Enabled := MDIChildCount>0;
DrawItem.Enabled := MDIChildCount>0;
end;
[TMainForm.FormCreate] 事件
procedure TMainForm.FormCreate(Sender: TObject);
begin
CaptureNum:=0;
DefaultDirectory:=ExtractFilePath(Application.ExeName);
end;
[TMainForm.FileOpen] 事件
procedure TMainForm.OpenDialogExecute(Sender: TObject);
begin
OpenDialog.InitialDir := DefaultDirectory;
if OpenDialog.Execute then
begin
DefaultDirectory:=ExtractFileName(OpenDialog.FileName);
CreateMDIChild(OpenDialog.FileName,false);
end;;
end;
[TMainForm.FileSave、TMainForm.FileSaveAs] 事件
procedure TMainForm.FileSave1Execute(Sender: TObject);
begin
with ActiveMDIChild as TMDIChild do begin
FileName:=ExtractFileName(caption);
FileName:=DefaultDirectory+FileName;
SaveDialog.FileName:=FileName;
if not SaveDialog.Execute then exit;
FileName:=SaveDialog.FileName ;
DefaultDirectory:=ExtractFileName(FileName);
if FileExists(FileName)then begin
if MessageDlg(FileName+' 已存在,替換它嗎?',mtInformation,[mbYES,mbNO],0)=mrYES then
DeleteFile(FileName)
else exit;
end;
Image1.Picture.saveToFile(FileName);
Caption:=FileName;
StatusBar.SimpleText := FileName;
end;
end;