ScreenPrint

Started by Martin Francom, March 28, 2011, 05:31:32 PM

Previous topic - Next topic

Martin Francom

I have need to do a print screen of the current program window.
Does anyone have some code that will do that?

Jean-pierre Leroy

Hi Marty,

You can use one these two functions for that:


'------------------------------------------------------------------
' Copy the current Window to the ClipBoard
Sub WindowToClipboard(hWnd As Long)
' credit to Adam J. Drake
' http://www.powerbasic.com/support/pbforums/showthread.php?t=14092
'------------------------------------------------------------------
   
    Local hDC       As Dword
    Local rc        As Rect
    Local nWidth    As Long
    Local nHeight   As Long
    Local hMemDC    As Dword
    Local hBmp      As Dword
    Local hBmpOld   As Dword
     
    GetWindowRect hWnd, rc
   
    nWidth=rc.nRight-rc.nLeft
    nHeight=rc.nBottom-rc.nTop
   
    hDC     = GetWindowDC(hWnd)   
    hMemDC  = CreateCompatibleDC(hDC)   
    hBmp    = CreateCompatibleBitmap(hDC, nWidth, nHeight)
    hBmpOld = SelectObject(hMemDC, hBmp)
   
    BitBlt hMemDC, 0, 0, nWidth, nHeight, hDC, 0, 0, %SRCCOPY
   
    ' clipboard
    If OpenClipboard(%HWND_DESKTOP) Then   
        EmptyClipboard
        SetClipboardData %CF_BITMAP, hBmp
        CloseClipboard
    Else
        MsgBox "Error opening clipboard.", %MB_ICONERROR, "Copy Screen"
    End If
   
    SelectObject hMemDC, hBmpOld
    DeleteDC hMemDC
    DeleteObject hBmp
    ReleaseDC %HWND_DESKTOP, hDC
           
End Sub           


or


'------------------------------------------------------------------
' Copy only the content of the current Window To the Clipboard
Sub WindowToClipboard2(hWnd As Long)
' credit to Gary Peek
' http://www.powerbasic.com/support/pbforums/showthread.php?t=10370
'------------------------------------------------------------------

  Local hDC      As Long
  Local rc       As Rect
  Local hImageDC As Long
  Local hBmp     As Long
  Local hBmpOld  As Long

  hDC      = GetDC(hWnd)              ' get device context handle to the window 
  hImageDC = CreateCompatibleDC(hDC)  ' create another device context
  GetClientRect hWnd, rc             ' get coordinates of client area
  hBmp    = CreateCompatibleBitmap (hDC, rc.nRight, rc.nBottom)  ' create a bitmap in memory
  hBmpOld = SelectObject (hImageDC, hBmp)  ' select the bitmap
  DeleteObject hBmpOld  ' delete old object
  BitBlt hImageDC, 0, 0, rc.nRight, rc.nBottom, hDC, 0, 0, %SRCCOPY  ' make a copy of it
 
  ' clipboard
  OpenClipboard ByVal %Null  ' open it identified with current task
  EmptyClipboard             ' open, free handles, associate with current task
  SetClipboardData %CF_BITMAP, hBmp
  CloseClipboard

  DeleteObject hBmp    ' delete memory handle
  DeleteDC hImageDC    ' delete additional device context
  ReleaseDC hWnd, hDC  ' release device context handle

End Sub

Martin Francom

This is great, Jean-Pierre,  Thank you...

Does any one know a way to send the content to the default printer
rather than to the ClipBoard ?

Martin Francom

I am trying to use  XPRINT COPY to print the bit map  but it is not working.  I get the "Choose Printer Dialog" to come up but the print job never gets sent to the printer.  What might I be doing wrong?
Here's the code:


'------------------------------------------------------------------
' Copy only the content of the current Window To the Clipboard
Sub WindowToClipboard2(hWnd As Long)
' credit to Gary Peek
' http://www.powerbasic.com/support/pbforums/showthread.php?t=10370
'------------------------------------------------------------------

  Local hDC      As Long
  Local rc       As Rect
  Local hImageDC As Long
  Local hBmp     As Long
  Local hBmpOld  As Long

  hDC      = GetDC(hWnd)              ' get device context handle to the window 
  hImageDC = CreateCompatibleDC(hDC)  ' create another device context
  GetClientRect hWnd, rc             ' get coordinates of client area
  hBmp    = CreateCompatibleBitmap (hDC, rc.nRight, rc.nBottom)  ' create a bitmap in memory
  hBmpOld = SelectObject (hImageDC, hBmp)  ' select the bitmap
  DeleteObject hBmpOld  ' delete old object
  BitBlt hImageDC, 0, 0, rc.nRight, rc.nBottom, hDC, 0, 0, %SRCCOPY  ' make a copy of it
 
  ' clipboard
  OpenClipboard ByVal %Null  ' open it identified with current task
  EmptyClipboard             ' open, free handles, associate with current task
  SetClipboardData %CF_BITMAP, hBmp
  CloseClipboard
 
  ' added code to print Bitmap to printer  (seems to be a problem here)
  '=====================
   XPrint Attach Choose
   XPrint Copy hBmp, 0
   XPrint FormFeed
   XPrint Close
  '=====================
   
  DeleteObject hBmp    ' delete memory handle
  DeleteDC hImageDC    ' delete additional device context
  ReleaseDC hWnd, hDC  ' release device context handle

End Sub

José Roca

XPRINT is DDT and DDT uses GRAPHIC BITMAP handles, not Windows bitmap handles.

Pat Dooley

This reminds me... will the the PB 10 "Print Preview" feature be useable in FF? Or will it be DDT?

Martin Francom

Quote from: Jose Roca on March 29, 2011, 03:16:47 AM
XPRINT is DDT and DDT uses GRAPHIC BITMAP handles, not Windows bitmap handles.

Jose,  How would you suggest building a Window Print routine?   
I suspect there must be some API methods.  ??

Douglas McDonald

Here's what I use to capture a dialog,copy to clipboard & print. Thanks to all that helped my with this.

  SetCursorPos 0, 0 'Move cursor out of the way
  Clipboard Reset 'clear the clipboard
  Keybd_event(%VK_SnapShot, 1, 0, 0) 'active dialog to clipboard
  Do
    FF_DoEvents
    Clipboard Get Bitmap To hImage, LL    'get handle to bitmap
  Loop Until LL
  Graphic Attach hImage, 0        'attach to handle
  Graphic Save Pict$ 'save the captured image
  Do
    Sleep 100
  Loop Until Isfile(Pict$) 'Wait till the save is completed
' Graphic Bitmap End

'------------------------Print bitmap-----------------------------
Local w,h As Long

  XPrint Attach Choose
  XPrint Set Orientation 2' landscape
  Dialog Get Size HWND_TCASFRM To w,h
  XPrint Stretch hImage, 0, (0,0)-(w-1,h-1) To (0,0)-(6*w-1,6*h-1)
  XPrint Close
 
  Graphic Bitmap End  'release graphic from mem
  Graphic DETACH
Doug McDonald
KD5NWK
www.redforksoftware.com
Is that 1's and 0's or 0's and 1's?

José Roca

Quote
Jose,  How would you suggest building a Window Print routine?   
I suspect there must be some API methods.  ??

1. Open the printer with OpenPrinter
2. Get the device context of the printer with GetPrinterDc.
3. Start a document with StartDoc.
4. Fill the contents using the appropriate GDI functions.
5. End the document with EndDoc.

This is the old GDI way. The new way will be to use XPS.

Martin Francom

Jose, Thank you for your help.  I suppose if I where half as knowledgeable about how to use the Windows API then what you suggested would make perfect sense to me. But alas I have not a clue.

I  didn't know there was a GDI way.  And now I find out there is a XPS way.  That's terrific! 
What's  the XPS way?   Are there any sample programs that demonstrate or compare the Old GDI way with the new XPS way?

Sometimes I can figure out what's going on when I can work through a sample program.

Martin Francom

Here's a little FF3 example program that I am using to work out
the bugs in the screen print routine.  There are 3 screen capture routines two routines captures to the ClipBoard then shells to Wordpad which you then have to paiste the captured image into Wordpad then you can use wordpads print function to print the image. 
What I would like to be able to do is print the image directly with out having to a copy/paste to wordpad.   But since I have no clue about the GDI or XPS ways I have not been able to get this to work.

Here's the example project that I have so far.  the program uses the F5, F6 and F7 keys to fire the ScreenPrint Functions. 

José Roca

Quote
What's  the XPS way?

XML Paper Specification Documents. A new technology introduced with Windows 7.
http://msdn.microsoft.com/en-us/magazine/cc163664.aspx

It requires good knowledge of low-level COM programming (as most new Microsoft technologies).

My new headers provide the interface declarations to allow to use it, but I don't have examples yet.

Roger Garstang

It is a Pre-Req for .NET 3.5 too, might have even been in 2.0.  So, if you have it you usually have XPS in Pre-Vista/7 OS too.  I print to it every once in a while on machines without Adobe Acrobat.  Sometimes it is tricky to open them.  There is a viewer, but it sometimes needs IE which seems to get in a loop of passing it to Firefox on me.