Jump to content
uligerhardt

List&Label - HWND of preview form

Recommended Posts

Hello! I want to show a MessageBox from inside a TL21_.OnViewerButtonClicked event handler and need the window handle of the preview form for that. Any idea how to get there?

Share this post


Link to post

I don't know what kind of control "TL21_" is supposed to refer to, but if it does not expose access to its HWND, one option would be to use a thread-local WH_CBT hook via the Win32 SetWindowsHookEx() function to receive notifications of every HWND that is created by the thread.  For instance, set the hook just before displaying the preview Form, and then release the hook after the Form is ready/closed.  Then you will know every HWND that the preview creates.

Share this post


Link to post
Guest
On 11/20/2020 at 12:04 PM, uligerhardt said:

Hello! I want to show a MessageBox from inside a TL21_.OnViewerButtonClicked event handler and need the window handle of the preview form for that. Any idea how to get there?

The List & Label report generator extends your application with extensive and convenient print, export and preview functions. You remain independent of programming languages and database formats. You retain control over data transfer and create the interface.

did see this:  https://www.combit.com/  

https://forum.combit.net/t/programmatically-close-the-preview-window/2018

 

if possible, post your code for help us!

Edited by Guest

Share this post


Link to post
On 11/20/2020 at 4:27 PM, FPiette said:

What is it?

 

Sorry, forgot to mention it in the message body. It's the List&Label report component.

Edited by uligerhardt

Share this post


Link to post

Here is the relevant code, a bit condensed:

type
  TPrintLLReportEventHandler = class
  public
    procedure OnViewerButtonClicked(Sender: TObject; Button: TViewerButton; var PerformDefaultAction: Boolean);
  end;

{ TPrintLLReportEventHandler }

procedure TPrintLLReportEventHandler.OnViewerButtonClicked(Sender: TObject; Button: TViewerButton; var PerformDefaultAction: Boolean);
begin
  if Button = vbExit then
  begin
    MessageBox(AWnd, .....); // <= I'd like to get the preview form's window handle here for AWnd
  end;
end;
var
  evh: TPrintLLReportEventHandler;

    evh := TPrintLLReportEventHandler.Create;
    try
      MyLLReportInstance.OnViewerButtonClicked := evh.OnViewerButtonClicked;
      // Show preview here (using LL_PRINT_PREVIEW)
    finally
      evh.Free;
    end;

 

Share this post


Link to post

Did you try the WH_CBT hook, like I suggested?  For example:

uses
  ..., Windows;

type
  TPrintLLReportEventHandler = class
  private
    FHook: HHOOK;
  public
    constructor Create;
    destructor Destroy; override;
    procedure OnViewerButtonClicked(Sender: TObject; Button: TViewerButton; var PerformDefaultAction: Boolean);
  end;

{ TPrintLLReportEventHandler }

var
  gPrintReportWnd: HWND = 0;

function CBTProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
  if nCode = HCBT_ACTIVATE then
  begin
    if gPrintReportWnd = 0 then
      gPrintReportWnd := HWND(wParam);
  end;
  Result := CallNextHookEx(0, nCode, wParam, lParam);
end;

constructor TPrintLLReportEventHandler.Create;
begin
  inherited;
  gPrintReportWnd := 0;
  FHook := SetWindowsHookEx(WH_CBT, @CBTProc, HInstance, GetCurrentThreadId);
end;

destructor TPrintLLReportEventHandler.Destroy;
begin
  if FHook <> 0 then
    UnhookWindowsHookEx(FHook);
  gPrintReportWnd := 0;
  inherited;
end;

procedure TPrintLLReportEventHandler.OnViewerButtonClicked(Sender: TObject; Button: TViewerButton; var PerformDefaultAction: Boolean);
begin
  if Button = vbExit then
  begin
    MessageBox(gPrintReportWnd, ...);
  end;
end;

var
  evh: TPrintLLReportEventHandler;
begin
  evh := TPrintLLReportEventHandler.Create;
  try
    MyLLReportInstance.OnViewerButtonClicked := evh.OnViewerButtonClicked;
    // Show preview here (using LL_PRINT_PREVIEW)
  finally
    evh.Free;
  end;
end;

 

Share this post


Link to post
13 hours ago, Remy Lebeau said:

Did you try the WH_CBT hook, like I suggested?  For example:


uses
  ..., Windows;

type
  TPrintLLReportEventHandler = class
  private
    FHook: HHOOK;
  public
    constructor Create;
    destructor Destroy; override;
    procedure OnViewerButtonClicked(Sender: TObject; Button: TViewerButton; var PerformDefaultAction: Boolean);
  end;

{ TPrintLLReportEventHandler }

var
  gPrintReportWnd: HWND = 0;

function CBTProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
  if nCode = HCBT_ACTIVATE then
  begin
    if gPrintReportWnd = 0 then
      gPrintReportWnd := HWND(wParam);
  end;
  Result := CallNextHookEx(0, nCode, wParam, lParam);
end;

constructor TPrintLLReportEventHandler.Create;
begin
  inherited;
  gPrintReportWnd := 0;
  FHook := SetWindowsHookEx(WH_CBT, @CBTProc, HInstance, GetCurrentThreadId);
end;

destructor TPrintLLReportEventHandler.Destroy;
begin
  if FHook <> 0 then
    UnhookWindowsHookEx(FHook);
  gPrintReportWnd := 0;
  inherited;
end;

procedure TPrintLLReportEventHandler.OnViewerButtonClicked(Sender: TObject; Button: TViewerButton; var PerformDefaultAction: Boolean);
begin
  if Button = vbExit then
  begin
    MessageBox(gPrintReportWnd, ...);
  end;
end;

var
  evh: TPrintLLReportEventHandler;
begin
  evh := TPrintLLReportEventHandler.Create;
  try
    MyLLReportInstance.OnViewerButtonClicked := evh.OnViewerButtonClicked;
    // Show preview here (using LL_PRINT_PREVIEW)
  finally
    evh.Free;
  end;
end;

 

I tried it just now, and it works AFAICT - I used

GetWindowText(gPrintReportWnd, ...)

to verify. Thanks a lot.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×