以下源碼演示如何在ReportMachine報表中顯示對應記錄指定文件名稱的照片,並且照片從指定的URL中下載。其原理是,在報表顯示前(OnBeforePrint事件),找到要顯示照片的TRMPictureView控件,再從揚hoto?#23383;段得到文件,從指定的URL下載該文件名稱的照片(jpg文件)到臨時文件夾,再將臨時文件夾中的照片顯示到TRMPictureView控件中。
需要注意的是,首先要在uses 子句中加入URLMon單元的引用。在窗口創建時得到臨時文件,當然可以是其它具體的文件,只不過每次報表預覽積累下來的照片會佔用不少的磁盤空間。
主要代碼在RMReport1的OnBeforePrint事件中,首先找到要顯示的TRMPictureView控件,這個控件在設計ReportMachine報表時加入,並賦予名稱(這裏爲損icPhoto?#65289;,代碼中的名稱要與報表中的名稱對應,否則永遠找不到。在找到控件後,讀取保存照片文件名稱的字段(這裏爲揚hoto?#65289;,再將指定的URL(這裏定義爲常數URL)加上文件名稱作爲完整的下載URL,必須是有效的URL,可以在服務器中指定對應的虛擬目錄,事先在IE中測試是否有效。將文件下載到臨時目錄下相同名稱的文件,再將該文件顯示到picPhoto控件中。
該程序在Windows2000 Server + Delphi6 + ReportMachine3.0中測試過。已知的BUG:下載URL無效時響應會變得很慢,且DownLoadFile()函數的返回結果總爲True。
unit u_frmURLReport;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, Grids, DBGrids, ADODB, ExtCtrls, RM_Dataset,
RM_Class, RM_Designer, RM_Common, ComCtrls, URLMon; // Use "URLMon" first!
type
TfrmURLReport = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
ADOQuery1EmpNo: TStringField;
ADOQuery1EmpName: TStringField;
ADOQuery1Company: TStringField;
ADOQuery1ClassName: TStringField;
ADOQuery1CardNo: TIntegerField;
ADOQuery1Photo: TStringField;
Panel1: TPanel;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
RMReport1: TRMReport;
RMDesigner1: TRMDesigner;
RMDBDataSet1: TRMDBDataSet;
StatusBar1: TStatusBar;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure RMReport1BeforePrint(Memo: TStrings; View: TRMReportView);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
PhotoPath: String;
public
{ Public declarations }
end;
var
frmURLReport: TfrmURLReport;
implementation
uses u_Download;
{$R *.dfm}
{ Get the temporary folder }
function GetTempPathName: String;
var
Buf: PChar;
begin
Result:= '';
GetMem(Buf, 255);
try
if GetTempPath(255, Buf) <> 0 then
Result:= String(Buf);
finally
FreeMem(Buf, 255);
end;
end;
{ Download file from the specified URL }
function DownloadFile(Source, Dest: string): Boolean;
begin
try
Result := UrlDownloadToFile(nil, PChar(source), PChar(Dest),
0, nil) = 0;
except
Result := False;
end;
end;
{ Filter the records }
procedure TfrmURLReport.Button1Click(Sender: TObject);
begin
ADOQuery1.Filtered:= False;
ADOQuery1.Filter:= Edit1.Text;
ADOQuery1.Filtered:= True;
end;
{ Preview the report }
procedure TfrmURLReport.Button2Click(Sender: TObject);
begin
RMReport1.LoadFromFile(ChangeFileExt(Application.ExeName, '.rmf'));
RMReport1.ShowReport;
end;
{ Design the report }
procedure TfrmURLReport.Button3Click(Sender: TObject);
begin
RMReport1.LoadFromFile(ChangeFileExt(Application.ExeName, '.rmf'));
RMReport1.DesignReport;
end;
{ Download the specified photo and show on the report }
procedure TfrmURLReport.RMReport1BeforePrint(Memo: TStrings;
View: TRMReportView);
const
URL = 'http://infoserver/gkpic/gkpic/Ext/';
var
picPhoto: TRMPictureView;
FileName: String;
begin
picPhoto:= (RMReport1.FindObject('picPhoto') as TRMPictureView);
if picPhoto <> nil then
begin
FileName:= RMReport1.Dataset.GetFieldValue('Photo');
StatusBar1.SimpleText:= 'Downloading photos...';
StatusBar1.Update;
DownloadFile(URL + FileName, PhotoPath + FileName);
if FileExists(PhotoPath + FileName) then
picPhoto.Picture.LoadFromFile(PhotoPath + FileName)
else
picPhoto.Picture.Graphic:= nil;
end;
end;
{ Get the temporary folder to save the photos first }
procedure TfrmURLReport.FormCreate(Sender: TObject);
begin
PhotoPath:= GetTempPathName;
//PhotoPath:= ExtractFilePath(Application.ExeName);
end;
end.