PRINTING in a "screen" printer

Share your advanced PureBasic knowledge/code with the community.
AMpos
Enthusiast
Enthusiast
Posts: 128
Joined: Fri Jun 05, 2020 12:47 am

PRINTING in a "screen" printer

Post by AMpos »

I have made this little thing, so my code will "print" into a window when in debug mode, or on a real printer in final exe.

Ideal for testing printer output without wasting dead trees or unicorn blood.

(I made this for testing before printing on a ticket printer)

Code: Select all

Enumeration FormFont
  #Font_printer
EndEnumeration

Procedure init_printer()
	
	CompilerIf #PB_Compiler_Debugger
		UsePNGImageDecoder()
		Define printer_out=OpenWindow(-1, 0, 0, 500, 650, "Ticket", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
		Define s=576/38
		Define printer_font=LoadFont(-1,"Courier New",s)
		
		StartDrawing(WindowOutput(printer_out))
		FrontColor(RGB(0,0,0))
		BackColor(RGB(255,255,255))
 		DrawingFont(FontID(printer_font))
 	CompilerElse
 		DefaultPrinter()
 		Define s=PrinterPageWidth()/38
 		
		StartPrinting("KISS-TPV+")
		UsePNGImageDecoder()
		Define printer_font=LoadFont(-1,"Courier New",s)
		Global printer_out=PrinterOutput()
		
		StartDrawing(printer_out)
		
		BackColor(RGB(255, 255, 255)) ; Uses white as back color, usuful when printing on a white sheet
		FrontColor(RGB(0, 0, 0))			; Use black for standard text color
		
		DrawingFont(FontID(printer_font))
	CompilerEndIf	
	
 EndProcedure
 
 Procedure close_printer()
 	Define event
 	
 	CompilerIf #PB_Compiler_Debugger
 		StopDrawing()
 		Repeat
 			event=WaitWindowEvent()
 		Until event=#PB_Event_CloseWindow
 		CloseWindow(GetActiveWindow())	
 	CompilerElse
 		StopDrawing()
 		StopPrinting()
 	CompilerEndIf
 EndProcedure
	
 	init_printer()
 	
 	Define y=0,yd=22
 	Define logo=LoadImage(-1,"gfx/PrinterLogo.png")   ;HEADER LOGO
 	If logo<>0
 		DrawImage(ImageID(logo), 0, 0)
 		y=ImageHeight(logo)
 	EndIf
 	 	
 	DrawText(0,y," "+FormatDate("%dd-%mm-%yyyy %hh:%ii",Date())):y+yd
 	DrawText(0,y,"-----------------------------------------"):y+yd
 	DrawText(0,y,"    JUST TESTING"):y+yd
 	DrawText(0,y,"-----------------------------------------"):y+yd
		
 	close_printer()
 	
dige
Addict
Addict
Posts: 1256
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: PRINTING in a "screen" printer

Post by dige »

Nice idea! Thx for sharing!
"Daddy, I'll run faster, then it is not so far..."
Post Reply