gpt4 book ai didi

delphi - 将 HTML 帮助作为单独的进程启动

转载 作者:行者123 更新时间:2023-12-03 19:51:55 27 4
gpt4 key购买 nike

我正在使用 XE7 64 并且我正在寻找一种策略来解决我在从我的应用程序中显示 HTMLHelp 文件时遇到的几个问题(我已将 HTMLHelpViewer 添加到我的使用子句中)。问题如下: 1) Ctrl-c 不从主题复制文本; 2) 当模式对话框处于事件状态时,无法访问帮助查看器。

问题的根源可能是由于 htmlhelpviewer 在与应用程序相同的进程中运行。有没有办法让内置的 htmlhelpviewer 启动一个新进程?如果没有,那么我需要使用 Createprocess 启动 HH.EXE 吗?

最佳答案

您可以将帮助文件查看器作为一个单独的进程启动,但我认为这会使控制它变得更加复杂。我的猜测是提供的 HTML 帮助查看器代码是您的问题的根本原因。我一直发现该代码质量极低。

我通过实现 OnHelp 来解决这个问题。我附加到 Application 的事件处理程序目的。此事件处理程序调用 HtmlHelp直接API。我当然没有遇到您描述的任何问题。

我的代码如下所示:

unit Help;

interface

uses
SysUtils, Classes, Windows, Messages, Forms;

procedure ShowHelp(HelpContext: THelpContext);
procedure CloseHelpWindow;

implementation

function RegisterShellHookWindow(hWnd: HWND): BOOL; stdcall; external user32;
function DeregisterShellHookWindow(hWnd: HWND): BOOL; stdcall; external user32;

procedure ShowHelp(HelpContext: THelpContext);
begin
Application.HelpCommand(HELP_CONTEXTPOPUP, HelpContext);
end;

type
THelpWindowManager = class
private
FMessageWindow: HWND;
FHelpWindow: HWND;
FHelpWindowLayoutPreference: TFormLayoutPreference;
function ApplicationHelp(Command: Word; Data: THelpEventData; var CallHelp: Boolean): Boolean;
protected
procedure WndProc(var Message: TMessage);
public
constructor Create;
destructor Destroy; override;
procedure RestorePosition;
procedure StorePosition;
procedure StorePositionAndClose;
end;

{ THelpWindowManager }

constructor THelpWindowManager.Create;

function DefaultRect: TRect;
var
i, xMargin, yMargin: Integer;
Monitor: TMonitor;
begin
Result := Rect(20, 20, 1000, 700);
for i := 0 to Screen.MonitorCount-1 do begin
Monitor := Screen.Monitors[i];
if Monitor.Primary then begin
Result := Monitor.WorkareaRect;
xMargin := Monitor.Width div 20;
yMargin := Monitor.Height div 20;
inc(Result.Left, xMargin);
dec(Result.Right, xMargin);
inc(Result.Top, yMargin);
dec(Result.Bottom, yMargin);
break;
end;
end;
end;

begin
inherited;
FHelpWindowLayoutPreference := TFormLayoutPreference.Create('Help Window', DefaultRect, False);
FMessageWindow := AllocateHWnd(WndProc);
RegisterShellHookWindow(FMessageWindow);
Application.OnHelp := ApplicationHelp;
end;

destructor THelpWindowManager.Destroy;
begin
StorePositionAndClose;
Application.OnHelp := nil;
DeregisterShellHookWindow(FMessageWindow);
DeallocateHWnd(FMessageWindow);
FreeAndNil(FHelpWindowLayoutPreference);
inherited;
end;

function THelpWindowManager.ApplicationHelp(Command: Word; Data: THelpEventData; var CallHelp: Boolean): Boolean;
var
hWndCaller: HWND;
HelpFile: string;
DoSetPosition: Boolean;
begin
CallHelp := False;
Result := True;

//argh, WinHelp commands
case Command of
HELP_CONTEXT,HELP_CONTEXTPOPUP:
begin
hWndCaller := GetDesktopWindow;
HelpFile := Application.HelpFile;

DoSetPosition := FHelpWindow=0;//i.e. if the window is not currently showing
FHelpWindow := HtmlHelp(hWndCaller, HelpFile, HH_HELP_CONTEXT, Data);
if FHelpWindow=0 then begin
//the topic may not have been found because the help file isn't there...
if FileExists(HelpFile) then begin
ReportError('Cannot find help topic for selected item.'+sLineBreak+sLineBreak+'Please report this error message to Orcina.');
end else begin
ReportErrorFmt(
'Cannot find help file (%s).'+sLineBreak+sLineBreak+'Reinstalling the program may fix this problem. '+
'If not then please contact Orcina for assistance.',
[HelpFile]
);
end;
end else begin
if DoSetPosition then begin
RestorePosition;
end;
HtmlHelp(hWndCaller, HelpFile, HH_DISPLAY_TOC, 0);//ensure that table of contents is showing
end;
end;
end;
end;

procedure THelpWindowManager.RestorePosition;
begin
if FHelpWindow<>0 then begin
RestoreWindowPosition(FHelpWindow, FHelpWindowLayoutPreference);
end;
end;

procedure THelpWindowManager.StorePosition;
begin
if FHelpWindow<>0 then begin
StoreWindowPosition(FHelpWindow, FHelpWindowLayoutPreference);
end;
end;

procedure THelpWindowManager.StorePositionAndClose;
begin
if FHelpWindow<>0 then begin
StorePosition;
SendMessage(FHelpWindow, WM_CLOSE, 0, 0);
FHelpWindow := 0;
end;
end;

var
WM_SHELLHOOKMESSAGE: UINT;

procedure THelpWindowManager.WndProc(var Message: TMessage);
begin
if (Message.Msg=WM_SHELLHOOKMESSAGE) and (Message.WParam=HSHELL_WINDOWDESTROYED) then begin
//need cast to HWND to avoid range errors
if (FHelpWindow<>0) and (HWND(Message.LParam)=FHelpWindow) then begin
StorePosition;
FHelpWindow := 0;
end;
end;
Message.Result := DefWindowProc(FMessageWindow, Message.Msg, Message.wParam, Message.lParam);
end;

var
HelpWindowManager: THelpWindowManager;

procedure CloseHelpWindow;
begin
HelpWindowManager.StorePositionAndClose;
end;

initialization
if not ModuleIsPackage then begin
Application.HelpFile := ChangeFileExt(Application.ExeName, '.chm');
WM_SHELLHOOKMESSAGE := RegisterWindowMessage('SHELLHOOK');
HelpWindowManager := THelpWindowManager.Create;
end;

finalization
FreeAndNil(HelpWindowManager);

end.

将该单元包含在您的项目中,您将被连接到处理帮助上下文请求。对代码的一些评论:
  • OnHelp的执行事件处理程序仅限于我的需要。如果您需要更多功能,则必须自己添加。
  • 你不会有 TFormLayoutPrefernce .这是我管理每个用户偏好的偏好类之一。它存储了窗口的边界矩形,以及窗口是否被最大化。这用于确保帮助窗口显示在与上一个 session 中相同的位置。如果你不想要这样的功能,就把它去掉。
  • ReportErrorReportErrorFmt是我显示错误对话框的辅助函数。您可以通过调用 MessageBox 来替换它们。或类似的。
  • 关于delphi - 将 HTML 帮助作为单独的进程启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30336018/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com