Print Active Window

Just starting out? Need help? Post your questions and find answers here.
SP
User
User
Posts: 33
Joined: Wed Aug 16, 2006 2:32 pm
Location: BCN-SPAIN

Print Active Window

Post by SP »

Hello,

I am trying to find a routine (it more simple possible) to send the active window to the printer. Someone can help me?

Thanks in advance
Greetings
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Code: Select all

hActiveWnd = GetForegroundWindow_()
hDC = GetWindowDC_(hActiveWnd)
DefaultPrinter()
StartPrinting("Window")
hPrinterDC = StartDrawing(PrinterOutput())
  GetWindowRect_(hActiveWnd, @R.RECT)
  With R
    \right - \left
    \bottom - \top
    BitBlt_(hPrinterDC, 0, 0, \right, \bottom, hDC, 0, 0, #SRCCOPY)
  EndWith
StopDrawing()

StopPrinting()

ReleaseDC_(hActiveWnd, hDC)
SP
User
User
Posts: 33
Joined: Wed Aug 16, 2006 2:32 pm
Location: BCN-SPAIN

Post by SP »

Thanks Trond!
SP
User
User
Posts: 33
Joined: Wed Aug 16, 2006 2:32 pm
Location: BCN-SPAIN

Post by SP »

Trond:
I have tested the routine that you have sent me.
It prints the active window, but the area that draws in the paper is very little. I tried to correct it, modifying the value of wide and high of the BitBlt function, but the output print is the same one. I don't believe to have introduced errors.
That may be happening? I'm working with XP Pro

Thanks
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Code: Select all

OpenWindow(0,0,0,400,300,"untitled",#WS_OVERLAPPEDWINDOW | 1)

CreateGadgetList(WindowID(0))

Text$ = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut "
Text$ + "laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation "
Text$ + "ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Sed diam nonummy nibh euismod "
Text$ + "tincidunt ut laoreet dolore magna aliquam erat volutpat, duis autem vel eum iriure dolor in hendrerit "
Text$ + "in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero et "
Text$ + "accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te "
Text$ + "feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy "
Text$ + "nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, "
Text$ + "quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat."

*hwndEdit = CreateWindowEx_(#WS_EX_CLIENTEDGE,"EDIT",Text$,#WS_CHILD | #WS_VISIBLE | #ES_MULTILINE,5,5,390,180,WindowID(0),0,0,0)

SendMessage_(*hwndEdit,#WM_SETFONT,LoadFont(0,"Arial",8),0)

ButtonGadget(1,5,190,190,25,"CAPTURE WINDOW")
ButtonGadget(2,205,190,190,25,"PRINT WINDOW CONTENT",#WS_DISABLED)

Repeat
	EventID = WaitWindowEvent()
	
	If EventID = #PB_Event_Gadget
		Select EventGadget()
			Case 1
			DisableGadget(2,0)
			
			GetWindowRect_(WindowID(0),wrc.RECT)
			
			WW = wrc\right-wrc\left : WH = wrc\bottom-wrc\top
			
			hOutput = CreateImage(0,WW,WH)
			
			hdcImg = StartDrawing(ImageOutput(0))
			
			hdcWin = GetWindowDC_(WindowID(0))			
			BitBlt_(hdcImg,0,0,WW,WH,hdcWin,0,0,#SRCCOPY)
			ReleaseDC_(WindowID(0),hdcWin)
						
			StopDrawing()
			
			Case 2
			Result = PrintRequester()
			
			If Result
				StartPrinting("PureBasic Print Job")
				  
				StartDrawing(PrinterOutput())
				DrawImage(hOutput,0,0,WW,WH)            
				StopDrawing()
				    
				StopPrinting()
			EndIf
		EndSelect
	EndIf	
Until EventID = 16
I haven't tested this code yet because I don't have a printer anymore (my dad actually "borrowed" it... :D ) but in theory it should work.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Code: Select all

hActiveWnd = GetForegroundWindow_()
hDC = GetWindowDC_(hActiveWnd)
DefaultPrinter()
StartPrinting("Window")
hPrinterDC = StartDrawing(PrinterOutput())
  GetWindowRect_(hActiveWnd, @R.RECT)
  With R
    \right - \left
    \bottom - \top
    StretchBlt_(hPrinterDC, 0, 0, PrinterPageWidth(), PrinterPageHeight(), hDC, 0, 0, \right, \bottom, #SRCCOPY)
  EndWith
StopDrawing()

StopPrinting()

ReleaseDC_(hActiveWnd, hDC)
SP
User
User
Posts: 33
Joined: Wed Aug 16, 2006 2:32 pm
Location: BCN-SPAIN

Post by SP »

@ Fluid Byte: This sample works. Thank you
P.S: We (the dads) are terrible!

@ Trond: I'm continue having problems with this code.
Not prints with the dimensions that I need. But thanks for your time!

Greetings!
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

How exactly do you want it? As wide as the paper but without changing aspect ratio?
SP
User
User
Posts: 33
Joined: Wed Aug 16, 2006 2:32 pm
Location: BCN-SPAIN

Post by SP »

Yes. I need to print in a DIN A4 (Horizontal or vertical) with the same aspect ratio that fits on the page.
Don't worry Trond. I try to implement others solutions.
Thanks!
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

You only have to change PrinterPageHeight() and Width.
SP
User
User
Posts: 33
Joined: Wed Aug 16, 2006 2:32 pm
Location: BCN-SPAIN

Post by SP »

OK
Thank you!
Intrigued
Enthusiast
Enthusiast
Posts: 501
Joined: Thu Jun 02, 2005 3:55 am
Location: U.S.A.

Active - Window - Screenshot - Screencapture - Print

Post by Intrigued »

Trond wrote:

Code: Select all

hActiveWnd = GetForegroundWindow_()
hDC = GetWindowDC_(hActiveWnd)
DefaultPrinter()
StartPrinting("Window")
hPrinterDC = StartDrawing(PrinterOutput())
  GetWindowRect_(hActiveWnd, @R.RECT)
  With R
    \right - \left
    \bottom - \top
    StretchBlt_(hPrinterDC, 0, 0, PrinterPageWidth(), PrinterPageHeight(), hDC, 0, 0, \right, \bottom, #SRCCOPY)
  EndWith
StopDrawing()

StopPrinting()

ReleaseDC_(hActiveWnd, hDC)
Ah! I have created something in a different language I use, but this is more like what I want... having no file paths, page numbers print with the image!

Thanks for this Trond. This will come in handy again and again!

ps. The only other option I can think I would need, would be the ability to print just the 'client area' (if I have it right). In essence I want to also be able to print the inner area of a window (not the titlebar or outline).
Intrigued - Registered PureBasic, lifetime updates user
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Just replace GetWindowDC_( with GetDC_(.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

An alternative (XP only):

Code: Select all

OpenLibrary(0,"user32.DLL") 
win=GetForegroundWindow_()
GetWindowRect_(win, @rc.rect)
width=rc\right - rc\left
height = rc\bottom - rc\top
CreateImage(0,width,height,#PB_Image_DisplayFormat)
hdc = StartDrawing(ImageOutput(0))
  CallFunction(0,"PrintWindow",win,hdc,0) 
StopDrawing()

If PrintRequester()
  If StartPrinting("TestJob")
    StartDrawing(PrinterOutput()) 
      outwidth.d = PrinterPageWidth()* 0.9
      vmargin.d = 0.05*PrinterPageHeight()
      hmargin.d = 0.05*PrinterPageWidth()
      factor.d = outwidth / width
      DrawImage(ImageID(0),hmargin,vmargin,Int(outwidth), Int(height * factor))
    StopDrawing() 
    StopPrinting()
  EndIf
EndIf
Pass 1 as last parameter to the PrintWindow API for client area only.
BERESHEIT
Intrigued
Enthusiast
Enthusiast
Posts: 501
Joined: Thu Jun 02, 2005 3:55 am
Location: U.S.A.

Post by Intrigued »

Thanks netmaestro!
Intrigued - Registered PureBasic, lifetime updates user
Post Reply