Question about #WM_PRINT Message

Everything else that doesn't fall into one of the other PB categories.
ebs
Enthusiast
Enthusiast
Posts: 530
Joined: Fri Apr 25, 2003 11:08 pm

Question about #WM_PRINT Message

Post by ebs »

I use the following code to capture an image of a window for printing:

Code: Select all

  ; print window to image
  hdc.L = StartDrawing(ImageOutput(PrintImage)) 
    If hdc
      SendMessage_(WindowID(Window), #WM_PRINT, hdc, #PRF_CLIENT|#PRF_NONCLIENT|#PRF_CHILDREN|#PRF_ERASEBKGND|#PRF_OWNED)
    EndIf
  StopDrawing()
I then send the image to the printer using PrinterLib.
This works fine for almost all windows, but there's a problem with EditorGadgets, which don't show up in the image.

Does anyone have a suggestion as to how to fix this?
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: Question about #WM_PRINT Message

Post by chi »

ebs wrote:Does anyone have a suggestion as to how to fix this?
BitBlt_(...)

Code: Select all

CompilerIf #PB_Compiler_Debugger = 0
  MessageRequester("", "Enable the Debugger...")
  End
CompilerEndIf

OpenWindow(0, 0, 0, 320, 200, "", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
SetWindowColor(0, #Gray)

ButtonGadget(0, 10, 10, 100, 30, "capture")

EditorGadget(1, 10, 50, 300, 140, #PB_Editor_WordWrap)
SetGadgetText(1, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")

OpenWindow(1, 0, 0, 100, 100, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered, WindowID(0))

Repeat 
  event = WaitWindowEvent()
  Select event
    Case #PB_Event_Gadget
      Select EventGadget()
          
        Case 0
          CreateImage(0, 320, 200)
          wdc = GetDC_(WindowID(0))
          idc = StartDrawing(ImageOutput(0))
          BitBlt_(idc, 0, 0, 320, 200, wdc, 0, 0, #SRCCOPY)
          StopDrawing()
          ReleaseDC_(WindowID(0), wdc)
          ShowLibraryViewer("Image", 0)
          
      EndSelect
  EndSelect
Until event = #PB_Event_CloseWindow
[/size]
Last edited by chi on Sat Aug 31, 2019 2:15 am, edited 2 times in total.
Et cetera is my worst enemy
ebs
Enthusiast
Enthusiast
Posts: 530
Joined: Fri Apr 25, 2003 11:08 pm

Re: Question about #WM_PRINT Message

Post by ebs »

You're right. I changed the #WM_PAINT message to a BitBlt_ call and the image now includes the EditorGadgets.
I think there's a chance that the image might contain portions of any overlapping windows, but in my case there aren't any. Thanks!
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: Question about #WM_PRINT Message

Post by chi »

ebs wrote:I think there's a chance that the image might contain portions of any overlapping windows, but in my case there aren't any.
No overlapping windows with BitBlt ;) (check out the code above)
Et cetera is my worst enemy
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4637
Joined: Sun Apr 12, 2009 6:27 am

Re: Question about #WM_PRINT Message

Post by RASHAD »

You can use PrintWindow
Run first

Code: Select all

OpenWindow(0, 0, 0, 400, 200, "Test", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget| #PB_Window_ScreenCentered)
SetWindowColor(0, #Gray)

ButtonGadget(0, 10, 10, 100, 30, "capture")

EditorGadget(1, 10, 50, 300, 140, #PB_Editor_WordWrap)
SetGadgetText(1, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
ImageGadget(2,10,210,400,400,0)
Repeat
  event = WaitWindowEvent()

Until event = #PB_Event_CloseWindow
Then run

Code: Select all

Prototype.i ptPrintWindow(hWnd, hdc, flags)

OpenLibrary(0, "User32.dll")
PrintWindow.ptPrintWindow = GetFunction(0, "PrintWindow")
Repeat
  hwnd = FindWindow_(0,"Test")
Until hwnd
OpenWindow(0, 0, 0, 510, 310, "", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget| #PB_Window_ScreenCentered)
SetWindowColor(0, #Gray)
ButtonGadget(0, 10, 10, 100, 30, "capture")
ImageGadget(2,10,50,410,210,0)
Repeat
  event = WaitWindowEvent()
  Select event
    Case #PB_Event_Gadget
      Select EventGadget()
         
        Case 0
          CreateImage(0, 406, 230)
          hdc = StartDrawing(ImageOutput(0))
              PrintWindow(hwnd, hdc,0)
          StopDrawing()
          SetGadgetState(2,ImageID(0))
         
      EndSelect
  EndSelect
Until event = #PB_Event_CloseWindow
Egypt my love
Post Reply