Print Active Window
Print Active Window
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
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
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)
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
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
- Fluid Byte
- Addict
- Posts: 2336
- Joined: Fri Jul 21, 2006 4:41 am
- Location: Berlin, Germany
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

Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
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)
Active - Window - Screenshot - Screencapture - Print
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!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)
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
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
An alternative (XP only):
Pass 1 as last parameter to the PrintWindow API for client area 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
BERESHEIT