views:

717

answers:

2

How to retrieve last user selected printer when printing from a preview window in Fast Report (Basic Edition ver. 4.7.1)?

I tried

frxReport.PrintOptions.Printer

in OnAfterPrintReport event but it only returns the system default printer.

After the user prints the report, the program prints a few Word documents and I need to know which printer was used last.

+1  A: 

Hi,

You can store your default printer for your application in your registry and get it before you print.

Hugues Van Landeghem
Have you read the question?
mwore
and you ? (sometimes bad answer are caused by bad question !)With your answer, I finally understand what you want.Next time, buy the code of your component :P
Hugues Van Landeghem
+1  A: 

After much research in a completely different direction (API hooking) I came up with this:

var
  sLastUsedPrinter: String;

threadvar
  ghHook: Integer;

...

//set frxPrintDialog hook
ghHook := SetWindowsHookEx(WH_CBT, @PrintDialogHookProc, 0, GetCurrentThreadId);
//show prepared report
frxReport.ShowPreparedReport;
//unhook frxPrintDialog hook
UnhookWindowsHookEx(ghHook);

...

function PrintDialogHookProc(uMsg, wParam, lParam: Integer): Integer; stdcall;
var
  //15 chars in 'TfrxPrintDialog' + 1 for string terminator
  sClassName: array [0..15] of Char;
  frxPrintDialog: TForm;
  PrintersCB: TComboBox;
begin
  //when a windows gets activated
  if uMsg = HCBT_ACTIVATE then
  begin
    //get window class name
    GetClassName(wParam, sClassName, 16);
    //window class name is Fast Report's Print Dialog
    if String(sClassName) = 'TfrxPrintDialog' then
    begin
      frxPrintDialog := FindControl(wParam) as TForm;
      PrintersCB := frxPrintDialog.FindComponent('PrintersCB') as TComboBox;
      //remember currently selected printer
      sLastUsedPrinter := PrintersCB.Text;
      //OnChange event handler for the printer selection ComboBox
      PrintersCB.OnChange := PrintersCBChange;
    end;
  end;
  Result := CallNextHookEx(ghHook, uMsg, wParam, lParam);
end;

procedure PrintersCBChange(Sender: TObject);
begin
  //remember last user selected printer
  sLastUsedPrinter := (Sender as TComboBox).Text;
end;

In real code sLastUsedPrinter and PrintersCBChange are actually class members but I changed them to keep things short(er).

mwore