Solved

Print a Window

Posted on 2002-07-17
6
394 Views
Last Modified: 2010-04-04
How can I print the contents of a specific 'child-window' on a printer. I'm looking for something like:

Print(WHandle)
0
Comment
Question by:detempel
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:LukA_YJK
Comment Utility
You can use Print method of any Form. Actually it uses GetFormImage: TBitmap and draws that to the printer HDC.
0
 

Author Comment

by:detempel
Comment Utility
I see my question is not complete. I'm referring to a window outside the Delphi-environment/program. I want to print a (child)-window from ANY application using Delphi.

With Delphi I can look for the right WindowHandle by scanning for text in the title in all ChildWindows from the TopWindow of the application in question.
0
 
LVL 3

Accepted Solution

by:
LukA_YJK earned 200 total points
Comment Utility
OK... I got it, you can send WM_PRINT message to this Window. To specify Printer HDC as a parameter probably you should use CreateDC with appropriate parameters. Hope it will be helpfull...
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 7

Expert Comment

by:Cynna
Comment Utility
LukA_YJK,

WM_PRINT won't always work. Some system windows will do OK most of the time, but window of arbitrary application doesn't tipically handle WM_PRINT event for the owner draw area.
Therefore, using WM_PRINT will often lead to 'half-printed' windows, where system drawn elements (like caption bar, menu, etc...) are printed, but application drawn elements are not.

detempel,

I'm afraid you must bring window of interest in the foreground to be able to capture it's bitmap and print it. I pasted together a set of more or less well-known procedures from different NGs and FAQs, and used them in a demo. Copy/paste them and see if it works for you.
Flicker will be unavoidable, due to the pulling/pushing window to/from foreground. Also, window is streched to fit the size of the paper. Adjust this as you see fit.


function GetWindowBitmap(hWindow:hWnd):HBitmap;
var DC,MemDC:HDC;
    bmWidth,bmHeight:Integer;
    TR:tRect;
    bm,obm:HBitmap;
begin
   // full window bitmap
   GetWindowRect(hWindow,TR);
   { note the  +1 }
   bmWidth:=TR.Right-TR.Left+1;
   bmHeight:=TR.Bottom-TR.Top+1;
   // Window DC
   DC:=GetDC(GetDesktopWindow);
   MemDC:=CreateCompatibleDC(DC);
   bm:=CreateCompatibleBitmap(DC,bmWidth,bmHeight);
   // now select the bitmap to memDC
   obm:=SelectObject(MemDC,bm);
   // and copy the window
   BitBlt(MemDC,0,0,bmWidth,bmHeight, DC, TR.Left, TR.Top,SRCCOPY);
   // clean up
   SelectObject(MemDC,obm);
   DeleteDC(MemDC);
   ReleaseDC(GetDesktopWindow, DC);
   // here is your bitmap
   GetWindowBitmap:=bm;
end;

procedure DrawImage(Canvas: TCanvas; DestRect: TRect; ABitmap:
TBitmap);
var
  Header, Bits: Pointer;
  HeaderSize:  DWORD;
  BitsSize: DWORD;
begin
  GetDIBSizes(ABitmap.Handle, HeaderSize, BitsSize);
  Header := AllocMem(HeaderSize);
  Bits := AllocMem(BitsSize);
  try
    GetDIB(ABitmap.Handle, ABitmap.Palette, Header^, Bits^);
    StretchDIBits(Canvas.Handle, DestRect.Left, DestRect.Top,
        DestRect.Right, DestRect.Bottom,
        0, 0, ABitmap.Width, ABitmap.Height, Bits,TBitmapInfo(Header^),
        DIB_RGB_COLORS, SRCCOPY);
  finally
    FreeMem(Header, HeaderSize);
    FreeMem(Bits, BitsSize);
  end;
end;

{ Print a Bitmap using the whole Printerpage }
procedure PrintBitmap(ABitmap: TBitmap);
var
  relheight, relwidth: integer;
begin
  screen.cursor := crHourglass;
  Printer.BeginDoc;
  if ((ABitmap.width / ABitmap.height) > (printer.pagewidth /printer.pageheight)) then
  begin
    { Stretch Bitmap to width of Printerpage }
    relwidth := printer.pagewidth;
    relheight := MulDiv(ABitmap.height, printer.pagewidth,ABitmap.width);
  end else
  begin
    { Stretch Bitmap to height of Printerpage }
    relwidth := MulDiv(ABitmap.width, printer.pageheight, ABitmap.height);
    relheight := printer.pageheight;
  end;
  DrawImage(Printer.Canvas, Rect(0, 0, relWidth, relHeight), ABitmap);
  Printer.EndDoc;
  screen.cursor := crDefault;
end;


procedure PrintWindow(WinHandle: HWnd);
var currentWin: HWnd;
    b: TBitmap;
begin
  b:=TBitmap.Create;
  currentWin:=GetForegroundWindow;
  SetForegroundWindow(WinHandle);
  while GetForegroundWindow<>WinHandle do Sleep(0);
   b.Handle:=GetWindowBitmap(WinHandle);
  SetForegroundWindow(currentWin);
  PrintBitmap(b);
  b.Free;
end;




// DEMO:
//-----------
procedure TForm1.Button1Click(Sender: TObject);
begin // Prints the notepad window
  if FindWindow('Notepad',nil)<>0 then
     PrintWindow(FindWindow('Notepad',nil))
  else ShowMessage('Notepad window not found!');
end;
0
 
LVL 3

Expert Comment

by:LukA_YJK
Comment Utility
Cynna, thanks for interesting fact about WM_PRINT. But maybe my first comment is not a bad idea. We can Use the Source ;) in Forms.pas Actually the implementation of GetFormImage is different in different versions. But I liked Delphi 2.0 version, seems it is TForm independent. Let me quote the following piece of code:

function TForm.GetFormImage: TBitmap;
var
  ScreenDC, PrintDC: HDC;
  OldBits, PrintBits: HBITMAP;
  PaintLParam: Longint;

  procedure PrintHandle(Handle: HWND);
  var
    R: TRect;
    Child: HWND;
    SavedIndex: Integer;
  begin
    if IsWindowVisible(Handle) then
    begin
      SavedIndex := SaveDC(PrintDC);
      Windows.GetClientRect(Handle, R);
      MapWindowPoints(Handle, Self.Handle, R, 2);
      with R do
      begin
        SetWindowOrgEx(PrintDC, -Left, -Top, nil);
        IntersectClipRect(PrintDC, 0, 0, Right - Left, Bottom - Top);
      end;
      SendMessage(Handle, WM_ERASEBKGND, PrintDC, 0);
      SendMessage(Handle, WM_PAINT, PrintDC, PaintLParam);
      Child := GetWindow(Handle, GW_CHILD);
      if Child <> 0 then
      begin
        Child := GetWindow(Child, GW_HWNDLAST);
        while Child <> 0 do
        begin
          PrintHandle(Child);
          Child := GetWindow(Child, GW_HWNDPREV);
        end;
      end;
      RestoreDC(PrintDC, SavedIndex);
    end;
  end;

begin
  Result := nil;
  ScreenDC := GetDC(0);
  PaintLParam := 0;
  try
    PrintDC := CreateCompatibleDC(ScreenDC);
    try
      PrintBits := CreateCompatibleBitmap(ScreenDC, ClientWidth, ClientHeight);
      try
        OldBits := SelectObject(PrintDC, PrintBits);
        try
          { Clear the contents of the bitmap }
          FillRect(PrintDC, ClientRect, Brush.Handle);

          { Paint form into a bitmap }
          PrintHandle(Handle);
        finally
          SelectObject(PrintDC, OldBits);
        end;
        Result := TBitmap.Create;
        Result.Handle := PrintBits;
        PrintBits := 0;
      except
        Result.Free;
        if PrintBits <> 0 then DeleteObject(PrintBits);
        raise;
      end;
    finally
      DeleteDC(PrintDC);
    end;
  finally
    ReleaseDC(0, ScreenDC);
  end;
end;

Maybe we should omit coordinate mapping and printing Child windows. It seems the Force is strong with detempel, so it will not be a problem to print obtained bitmap... Hope it will be helpfull...
0
 

Author Comment

by:detempel
Comment Utility
Thanks for your efforts in helping me. For the moment I won't have to solve this problem so I want to give my points at this moment and close the question.

Erik.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now