December 11, 2024, 10:34:30 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Print Window to Printer

Started by LarryMc, July 18, 2016, 01:29:07 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

LarryMc

Reinhard had a recent problem where he had a window with text and graphics that he wanted to print out.

He had several problems. Part were the flags he used in defining his window. When that was corrected  he was left with one major problem.

Although he had created his window proportional to an 8.5 x 11 page when it was printed it only was printed (properly scaled) on about 1/4 of a page.
He used the IWB PRINTWINDOW command.
Apparently what happened is that it scaled the SCREEN to 8.5 x 11 and then printed his window to that scale.
I couldn't find a way to get around that with the PRINTWINDOW command directly.

So, I took some code I used in the IWBasic IDE Print Preview,  stripped it down and modified and come up with a subroutine that you can use to print any window to the printer.

If you scale the size of window to 8.5 x 11 then when you print it should fit pretty close.  You can always fudge the scale if necessary.

You'll have to use $include "windowssdk.inc"

/*'==================================================================================================
printwindow2printer(window win)
by Larry McCaughn

'==================================================================================================*/
export printwindow2printer
TYPE SPRINTDLG,1
    DEF lStructSize as UINT
    DEF hwndOwner as UINT
    DEF hDevMode as UINT
    DEF hDevNames as UINT
    DEF hDC as UINT
    DEF Flags as UINT
    DEF nFromPage as WORD
    DEF nToPage as WORD
    DEF nMinPage as WORD
    DEF nMaxPage as WORD
    DEF nCopies as WORD
    DEF hInstance as UINT
    DEF lCustData as UINT
    DEF lpfnPrintHook as POINTER
    DEF lpfnSetupHook as POINTER
    DEF lpPrintTemplateName as POINTER
    DEF lpSetupTemplateName as POINTER
    DEF hPrintTemplate as UINT
    DEF hSetupTemplate as UINT
ENDTYPE

DECLARE EXTERN _fpreset()
DECLARE EXTERN PRINTBITMAP(hdc as UINT,hBmp as UINT,rcBitmap as WINRECT),INT
DECLARE EXTERN IBDispatch(win as POINTER,uMsg as UINT, wParam as UINT, lParam as UINT)
DECLARE IMPORT,EndPageA alias EndPage(hdc as UINT),INT
':::::::::::::::::::::::::::::::::::::::::::::::::::::::


global sub printwindow2printer(window win)
DEF pd as SPRINTDLG
STRING strText
INT screenx,screeny,sizex,sizey
DOCINFO di
WINRECT rectClient,rectPrint
FLOAT s1,s2,scale
POINTER pMode=null,pstring=null,ptemp2=null
IF win.hwnd
ZeroMemory(pd,LEN(PRINTDLG))
pd.lStructSize = LEN(PRINTDLG)
pd.nMinPage = 1
pd.nMaxPage = 1
pd.nFromPage = 1
pd.nToPage = 1
pd.nCopies = 1
pd.Flags = PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION | PD_RETURNDC |PD_RETURNDEFAULT
pd.hwndOwner = win.hwnd
IF PrintDlg(pd) <> 0
_fpreset()
IF pd.hDevMode
pMode = GlobalLock(pd.hDevMode)
ENDIF
IF pd.hDevNames
pString = GlobalLock(pd.hDevNames)
pTemp2 = pString
pString += #<DEVNAMES>pString.wOutputOffset
string strPrinter = #<STRING>pString
if(strPrinter[0] <> "\\")
pTemp2 += #<DEVNAMES>pTemp2.wDeviceOffset
strPrinter = #<STRING>pTemp2
endif
endif
#<DEVMODE>pMode.dmDeviceName = strPrinter
#<DEVMODE>pMode.dmOrientation = 1
#<DEVMODE>pMode.dmCopies =1
#<DEVMODE>pMode.dmScale=100
#<DEVMODE>pMode.dmPaperSize = 1
#<DEVMODE>pMode.dmDefaultSource = 7
#<DEVMODE>pMode.dmColor=2
#<DEVMODE>pMode.dmDuplex=1
#<DEVMODE>pMode.dmCollate=0
#<DEVMODE>pMode.dmFormName="Letter"
#<DEVMODE>pMode.dmMediaType=1
         #<DEVMODE>pMode.dmFields = #<DEVMODE>pMode.dmFields | DM_ORIENTATION
         ResetDC(pd.hdc,pMode)
screenx = GetSystemMetrics(SM_CXSCREEN)
screeny = GetSystemMetrics(SM_CYSCREEN)
sizex = GetDeviceCaps(pd.hdc,HORZRES)
sizey = GetDeviceCaps(pd.hdc,VERTRES)
ZeroMemory (di, LEN(DOCINFO))
di.cbSize = LEN(DOCINFO)
GetWindowTextA(win.hwnd,strText,254)
di.lpszDocName = strText
IF StartDocA(pd.hdc,di) > 0
GetClientRect(win.hwnd,rectClient)
s1 = sizex/(rectClient.right+0.0f)
s2 = sizey/(rectClient.bottom+0.0f)
IF (s1 < s2) THEN scale = s1 ELSE scale = s2
rectPrint.left = 0 : rectPrint.top = 0
rectPrint.right = screenx * scale : rectPrint.bottom = screeny * scale
StartPage(pd.hdc)
PRINTBITMAP(pd.hdc,win.hBitmap,rectPrint)
EndPageA(pd.hdc)
EndDoc(pd.hdc)
IF pd.hDevMode
GlobalUnlock(pd.hDevMode)
GlobalFree(pd.hDevMode)
ENDIF
GlobalFree(pd.hDevNames)
ENDIF
win.m_hPrintDC = NULL
ENDIF
ENDIF
RETURN
ENDSUB

LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

ckoehn

LarryMc,

The window that you want to print can be no bigger than the screen resolution, right?  Is it possible to make a hidden window bigger than the screen, print text and graphics on it and then use your printwindow2printer sub?

Later,
Clint

LarryMc

February 25, 2017, 12:20:02 PM #2 Last Edit: February 25, 2017, 12:24:36 PM by LarryMc
You can make a hidden window any size you want.
But you still have to scale it to print on the paper



LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

ckoehn

I did that, but I made it big to get better resolution.  That put some of the HIDDEN window out side of the screen, and couldn't be printed on.  My screen is 1366 x 768 and it would print nothing on the window over 768, which made for a very low res printout.

It looks like the window should be made persistant no matter how big it is, like 1600 x 2000 pixels.  Then when it scales to print it would have good resolution.

Hope this makes sense.  This is my chemo week and am in a little bit of a brain fog.

Later,
Clint

ckoehn

February 25, 2017, 03:09:28 PM #4 Last Edit: February 25, 2017, 04:11:17 PM by ckoehn
This should show what I'm talking about.  You need windowssdk.inc at the top like always, then printer.inc. Place it in the same directory as MyPrintertest.iwb.  It will only print part of the page even though it is scaled to 8.5 x 11.  If you make it so the height is no bigger than the height of the screen, it will work but al least my printer looks like a low res printout.

Later,
Clint

Brian

Larry,

Could you give me a heads-up on how you implement or call this code in a program, please?

Brian

LarryMc

Brian
the posted example by Clint just before yours has a working example.

LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Brian

Got it now, Larry - thank you. Will give it a whirl

Brian

ckoehn

You could check out this thread on the printer routines I worked on last year.  There is a lot that could probably be improved on.  The disadvantage is that you need to set up seperate routines to print/draw to your window and to print/draw to your printer.  The advantage is you get high resolution printouts.

http://www.ionicwind.com/forums/index.php?topic=6162.msg45357#msg45357

Later,
Clint