fdemunit.pas
来自「查看html文件的控件」· PAS 代码 · 共 921 行 · 第 1/2 页
PAS
921 行
end;
procedure TForm1.SubmitEvent(Sender: TObject; const AnAction, Target, EncType, Method: String;
Results: TStringList);
begin
with SubmitForm do
begin
ActionText.Text := AnAction;
MethodText.Text := Method;
ResultBox.Items := Results;
Results.Free;
Show;
end;
end;
procedure TForm1.ProcessingHandler(Sender: TObject; ProcessingOn: Boolean);
begin
if ProcessingOn then
begin {disable various buttons and menuitems during processing}
FwdButton.Enabled := False;
BackButton.Enabled := False;
ReloadButton.Enabled := False;
Print1.Enabled := False;
PrintPreview1.Enabled := False;
Find1.Enabled := False;
SelectAll1.Enabled := False;
Open1.Enabled := False;
CloseAll; {in case hint window is open}
end
else
begin
FwdButton.Enabled := FrameViewer.FwdButtonEnabled;
BackButton.Enabled := FrameViewer.BackButtonEnabled;
ReloadButton.Enabled := FrameViewer.CurrentFile <> '';
Print1.Enabled := (FrameViewer.CurrentFile <> '') and (FrameViewer.ActiveViewer <> Nil);
PrintPreview1.Enabled := Print1.Enabled;
Find1.Enabled := Print1.Enabled;
SelectAll1.Enabled := Print1.Enabled;
Open1.Enabled := True;
end;
end;
procedure TForm1.FwdButtonClick(Sender: TObject);
begin
FrameViewer.GoFwd;
end;
procedure TForm1.BackButtonClick(Sender: TObject);
begin
FrameViewer.GoBack;
end;
procedure TForm1.WindowRequest(Sender: TObject; const Target,
URL: string);
var
S, Dest: string[255];
I: integer;
PC: array[0..255] of char;
begin
S := URL;
I := Pos('#', S);
if I >= 1 then
begin
Dest := System.Copy(S, I, 255); {local destination}
S := System.Copy(S, 1, I-1); {the file name}
end
else
Dest := ''; {no local destination}
S := FrameViewer.HTMLExpandFileName(S);
if FileExists(S) then
WinExec(StrPCopy(PC, ParamStr(0)+' "'+S+Dest+'"'), sw_Show);
end;
procedure TForm1.wmDropFiles(var Message: TMessage);
var
S: string[200];
Count: integer;
begin
Count := DragQueryFile(Message.WParam, 0, @S[1], 200);
Length(S) := Count;
DragFinish(Message.WParam);
if Count >0 then
FrameViewer.LoadFromFile(S);
Message.Result := 0;
end;
procedure TForm1.CopyImagetoclipboardClick(Sender: TObject);
begin
Clipboard.Assign(FoundObject.Bitmap);
end;
procedure TForm1.ViewImageClick(Sender: TObject);
var
AForm: TImageForm;
begin
AForm := TImageForm.Create(Self);
with AForm do
begin
ImageFormBitmap := FoundObject.Bitmap;
Caption := '';
Show;
end;
end;
procedure TForm1.MediaPlayerNotify(Sender: TObject);
begin
try
With MediaPlayer do
if NotifyValue = nvSuccessful then
begin
if MediaCount > 0 then
begin
Play;
Dec(MediaCount);
end
else
Begin
Close;
ThePlayer := Nil;
end;
end;
except
end;
end;
procedure TForm1.SoundRequest(Sender: TObject; const SRC: String;
Loop: Integer; Terminate: Boolean);
begin
try
with MediaPlayer do
if Terminate then
begin
if (Sender = ThePlayer) then
begin
Close;
ThePlayer := Nil;
end;
end
else if ThePlayer = Nil then
begin
if Sender is ThtmlViewer then
Filename := ThtmlViewer(Sender).HTMLExpandFilename(SRC)
else Filename := (Sender as TFrameViewer).HTMLExpandFilename(SRC);
Notify := True;
Open;
ThePlayer := Sender;
if Loop < 0 then MediaCount := 9999
else if Loop = 0 then MediaCount := 1
else MediaCount := Loop;
end;
except
end;
end;
procedure TForm1.FrameViewerObjectClick(Sender, Obj: TObject;
const OnClick: String);
var
S: string;
begin
if OnClick = 'display' then
begin
if Obj is TFormControlObj then
with TFormControlObj(Obj) do
begin
if TheControl is TCheckBox then
with TCheckBox(TheControl) do
begin
S := Value + ' is ';
if Checked then S := S + 'checked'
else S := S + 'unchecked';
MessageDlg(S, mtCustom, [mbOK], 0);
end
else if TheControl is TRadioButton then
with TRadioButton(TheControl) do
begin
S := Value + ' is checked';
MessageDlg(S, mtCustom, [mbOK], 0);
end;
end;
end
else if OnClick <> '' then
MessageDlg(OnClick, mtCustom, [mbOK], 0);
end;
procedure TForm1.FrameViewerInclude(Sender: TObject; const Command: String;
Params: TStrings; var S: string);
{OnInclude handler}
var
Filename: string;
I: integer;
MS: TMemoryStream;
begin
if CompareText(Command, 'Date') = 0 then
S := DateToStr(Date) { <!--#date --> }
else if CompareText(Command, 'Time') = 0 then
S := TimeToStr(Time) { <!--#time --> }
else if CompareText(Command, 'Include') = 0 then
begin {an include file <!--#include FILE="filename" --> }
if (Params.count >= 1) then
begin
I := Pos('file=', Lowercase(Params[0]));
if I > 0 then
begin
Filename := copy(Params[0], 6, Length(Params[0])-5);
MS := TMemoryStream.Create;
try
try
MS.LoadFromFile(Filename);
SetString(S, PChar(MS.Memory), MS.Size);
finally
MS.Free;
end;
except
end;
end;
end;
end;
Params.Free;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
HintWindow.Free;
end;
procedure TForm1.FrameViewerRightClick(Sender: TObject; Parameters: TRightClickParameters);
var
Pt: TPoint;
S, Dest: string;
I: integer;
Viewer: ThtmlViewer;
HintWindow: THintWindow;
ARect: TRect;
begin
Viewer := Sender as ThtmlViewer;
with Parameters do
begin
FoundObject := Image;
ViewImage.Enabled := (FoundObject <> Nil) and (FoundObject.Bitmap <> Nil);
CopyImageToClipboard.Enabled := (FoundObject <> Nil) and (FoundObject.Bitmap <> Nil);
if URL <> '' then
begin
S := URL;
I := Pos('#', S);
if I >= 1 then
begin
Dest := System.Copy(S, I, 255); {local destination}
S := System.Copy(S, 1, I-1); {the file name}
end
else
Dest := ''; {no local destination}
if S = '' then S := Viewer.CurrentFile
else S := Viewer.HTMLExpandFileName(S);
NewWindowFile := S+Dest;
OpenInNewWindow.Enabled := FileExists(S);
end
else OpenInNewWindow.Enabled := False;
GetCursorPos(Pt);
if Length(CLickWord) > 0 then
begin
HintWindow := THintWindow.Create(Self);
try
ARect := Rect(0,0,0,0);
DrawTextW(HintWindow.Canvas.Handle, @ClickWord[1], Length(ClickWord), ARect, DT_CALCRECT);
with ARect do
HintWindow.ActivateHint(Rect(Pt.X+20, Pt.Y-(Bottom-Top)-15, Pt.x+30+Right, Pt.Y-15), ClickWord);
PopupMenu.Popup(Pt.X, Pt.Y);
finally
HintWindow.Free;
end;
end
else PopupMenu.Popup(Pt.X, Pt.Y);
end;
end;
procedure TForm1.OpenInNewWindowClick(Sender: TObject);
var
PC: array[0..255] of char;
begin
WinExec(StrPCopy(PC, ParamStr(0)+' "'+NewWindowFile+'"'), sw_Show);
end;
procedure TForm1.PrintPreview1Click(Sender: TObject);
var
pf: TPreviewForm;
Viewer: ThtmlViewer;
Abort: boolean;
begin
Viewer := FrameViewer.ActiveViewer;
if Assigned(Viewer) then
begin
pf := TPreviewForm.CreateIt(Self, Viewer, Abort);
try
if not Abort then
pf.ShowModal;
finally
pf.Free;
end;
end;
end;
procedure TForm1.FrameViewerMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
TitleStr: string;
begin
if not Timer1.Enabled and Assigned(ActiveControl) and ActiveControl.Focused
and (Sender is ThtmlViewer) then
begin
TitleViewer := ThtmlViewer(Sender);
TitleStr := TitleViewer.TitleAttr;
if TitleStr = '' then
OldTitle := ''
else if TitleStr <> OldTitle then
begin
TimerCount := 0;
Timer1.Enabled := True;
OldTitle := TitleStr;
end;
end;
end;
procedure TForm1.CloseAll;
begin
Timer1.Enabled := False;
HintWindow.ReleaseHandle;
HintVisible := False;
TitleViewer := Nil;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
const
StartCount = 2; {timer counts before hint window opens}
EndCount = 20; {after this many timer counts, hint window closes}
var
Pt, Pt1: TPoint;
ARect: TRect;
TitleStr: string;
begin
if not Assigned(TitleViewer) then
begin
CloseAll;
Exit;
end;
Inc(TimerCount);
GetCursorPos(Pt);
try {in case TitleViewer becomes corrupted}
Pt1 := TitleViewer.ScreenToClient(Pt);
TitleStr := TitleViewer.TitleAttr;
if (TitleStr = '') or not PtInRect(TitleViewer.ClientRect, Pt1)then
begin
OldTitle := '';
CloseAll;
Exit;
end;
if TitleStr <> OldTitle then
begin
TimerCount := 0;
OldTitle := TitleStr;
HintWindow.ReleaseHandle;
HintVisible := False;
Exit;
end;
if TimerCount > EndCount then
CloseAll
else if (TimerCount >= StartCount) and not HintVisible then
begin
{$ifdef ver90} {Delphi 2}
ARect := Rect(0,0,0,0);
DrawText(HintWindow.Canvas.Handle, PChar(TitleStr), Length(TitleStr), ARect, DT_CALCRECT);
{$else}
ARect := HintWindow.CalcHintRect(300, TitleStr, Nil);
{$endif}
with ARect do
HintWindow.ActivateHint(Rect(Pt.X, Pt.Y+18, Pt.X+Right, Pt.Y+18+Bottom), TitleStr);
HintVisible := True;
end;
except
CloseAll;
end;
end;
procedure TForm1.FrameViewerProgress(Sender: TObject;
Stage: TProgressStage; PercentDone: Integer);
begin
ProgressBar.Position := PercentDone;
case Stage of
psStarting:
ProgressBar.Visible := True;
psRunning:;
psEnding:
ProgressBar.Visible := False;
end;
ProgressBar.Update;
end;
procedure TForm1.SetPrintScaleClick(Sender: TObject);
var
S: string;
begin
S := FloatToStr(FrameViewer.PrintScale);
try
if InputQuery('PrintScale', 'Enter desired print scale value', S) then
FrameViewer.PrintScale := StrToFloat(S);
except
end;
end;
{HTML for print header and footer}
const
HFText: string = '<html><head><style>'+
'body {font: Arial 8pt;}'+
'</style></head>'+
'<body marginwidth="0">'+
'<table border="0" cellspacing="2" cellpadding="1" width="100%">'+
'<tr>'+
'<td>#left</td><td align="right">#right</td>'+
'</tr>'+
'</table></body></html>';
function ReplaceStr(Const S, FromStr, ToStr: string): string;
{replace FromStr with ToStr in string S.
for Delphi 6, 7, AnsiReplaceStr may be used instead.}
var
I: integer;
begin
I := Pos(FromStr, S);
if I > 0 then
begin
Result := S;
Delete(Result, I, Length(FromStr));
Insert(ToStr, Result, I);
end;
end;
procedure TForm1.ViewerPrintHTMLHeader(Sender: TObject;
HFViewer: THTMLViewer; NumPage: Integer; LastPage: boolean; var XL, XR: integer; var StopPrinting: Boolean);
var
S: string;
begin
S := ReplaceStr(HFText, '#left', FrameViewer.DocumentTitle);
S := ReplaceStr(S, '#right', FrameViewer.CurrentFile);
HFViewer.LoadFromString(S);
end;
procedure TForm1.ViewerPrintHTMLFooter(Sender: TObject;
HFViewer: THTMLViewer; NumPage: Integer; LastPage: boolean; var XL, XR: integer; var StopPrinting: Boolean);
var
S: string;
begin
S := ReplaceStr(HFText, '#left', DateToStr(Date));
S := ReplaceStr(S, '#right', 'Page '+IntToStr(NumPage));
HFViewer.LoadFromString(S);
end;
end.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?