Using FindVCLWindow to call WinHelp32 (WinXP Pro S

2020-03-31 18:15发布

问题:

what is wrong there?

procedure TForm1.VCLHelpClick(Sender: TObject);
var Ctrl : TWinControl;
 begin
 Ctrl := FindVCLWindow(Mouse.CursorPos);
 if Ctrl <> nil then
  if Form2.Cursor = crHelp then begin
   if Ctrl = CreatorEdit then Application.HelpCommand(HELP_CONTEXT,001);
   if Ctrl = EditorEdit then Application.HelpCommand(HELP_CONTEXT,002);
   if Ctrl = UpdaterEdit then Application.HelpCommand(HELP_CONTEXT,003);
   if Ctrl = IdeaEdit then Application.HelpCommand(HELP_CONTEXT,004);
   if Ctrl = PorterEdit then Application.HelpCommand(HELP_CONTEXT,005);
  end;
 end;

The idea is simple - i have form border icons for Help button and when i click it, cursors changes to crHelp. If i click under control for any of IFs, it invokes Help System and Opens associated help file with context from command. But it doesnt work ... Is this because I have not added support for KLink / ELinks in Help file itself?

For help authoring and developing I am using ShalomHelpMaker Software.

回答1:

Have you tried debugging the code? And can you tell us what part went wrong.

Besides, why don't you use the helpcontext like:

procedure TForm1.VCLHelpClick(Sender: TObject);
var Ctrl : TWinControl;
begin
  if Form2.Cursor <> crHelp then   // Are you sure this is Form2???
    Exit;
  Ctrl := FindVCLWindow(Mouse.CursorPos);
  if Ctrl = nil then Exit;

  Application.HelpCommand(HELP_CONTEXT, Ctrl.HelpoContext);
end;

Looks like FindVCLControl does some other things. But the following code works:

procedure TForm1.Button1Click(Sender: TObject);
var
  ctrl : TControl;
  point : TPoint;
begin
  point := Mouse.CursorPos; // Mouse pos at screen
  Dec(point.X, Left); // Adjust for window.
  Dec(point.Y, Top);
  Dec(point.Y, GetSystemMetrics(SM_CYCAPTION)); // Adjust to client area.

  ctrl := ControlAtPos(point, True, True, True);

  // Do something with the control
end;

You probably need some more tweaking, but this works to get the control of a window from the position.



回答2:

Working code:

procedure TForm1.VCLHelpClick(Sender: TObject);
var WCtrl : TWinControl;
begin
  WCtrl := FindVCLWindow(Mouse.CursorPos);
  if WCtrl <> nil then
   Application.HelpCommand(HELP_CONTEXT, wCtrl.HelpContext);
end;

P.S. all previous code probobly was ok too, but i rechecked my event handlers and found that in one tlabel it was missing ( althought when I clicked to the ones that had onclick, it did not work). Plus ... problem probobly was the faulty cursor check.

Ok, thanks for support, guys!