Re: wxPrint
- Posted by jacques deschĂȘnes <desja at globetro?t?r.net> Oct 03, 2007
- 703 views
Brian Clark wrote: > > > Can someone post a simple (non-win32lib) example of printing to a printer? > Also, I saw wxHtmlEasyPrinting in the wxWidgets doc. How can I get this > wrapped > for use in wxEuphoria? Hi Brian, Printing under windows is quite involved, it can't be done in a few lines as the following demo shows. And this demo does the very basic as it doesn't implement a cancel dialog as it should. It doesn't compute the text metrics and doesn't get all the usefull printer device capacities needed to format printed text proprerly. But it works.
-- printing demo include dll.e include machine.e constant gdi32 = open_dll("gdi32.dll") constant comdlg32 = open_dll("comdlg32.dll") --constant user32 = open_dll("user32.dll") constant iDeleteDC = define_c_func(gdi32,"DeleteDC",{C_UINT},C_UINT) -- argument = hdc constant iPrintDlg = define_c_func(comdlg32,"PrintDlgA",{C_POINTER},C_INT) -- argument is pointer PRINTDLG structure constant iStartDoc = define_c_func(gdi32,"StartDocA",{C_UINT,C_POINTER},C_INT) -- argument1 is hdc, argument2 is pointer to DOCINFO constant iStartPage = define_c_func(gdi32,"StartPage",{C_UINT},C_INT) -- argument is printer hdc constant iEndPage = define_c_func(gdi32,"EndPage",{C_UINT},C_INT) -- argument is printer hdc constant iEndDoc = define_c_func(gdi32,"EndDoc",{C_UINT},C_INT) -- argument is printer hdc constant iGetDeviceCaps = define_c_func(gdi32,"GetDeviceCaps",{C_UINT,C_INT},C_INT) -- argument1 is printer hdc, argument2 one of dev. cap. constant constant iTextOut = define_c_func(gdi32,"TextOutA",{C_UINT,C_INT,C_INT,C_POINTER,C_INT},C_INT) -- hdc, x,y, pString, nChar constant PDLG_SIZE = 66 -- PRINTDLG structure size constant PD_USEDEVMODECOPIESANDCOLLATE=262144,PD_RETURNDC= 256 constant DOCINFO_SIZE = 20 constant HORZRES =8 , VERTRES = 10, LOGPIXELSY = 90 function GetDeviceCaps(atom hdc, atom Cap) return c_func(iGetDeviceCaps,{hdc,Cap}) end function procedure StartDoc(atom hdc, sequence DocTitle) object fnVal, pDocInfo, pTitle pTitle = allocate_string(DocTitle) pDocInfo = allocate(DOCINFO_SIZE) mem_set(pDocInfo,0,DOCINFO_SIZE) poke4(pDocInfo,DOCINFO_SIZE) poke4(pDocInfo+4,pTitle) fnVal = c_func(iStartDoc,{hdc,pDocInfo}) free(pTitle) free(pDocInfo) end procedure procedure StartPage(atom hdc) object fnVal fnVal = c_func(iStartPage,{hdc}) end procedure procedure EndPage(atom hdc) object fnVal fnVal = c_func(iEndPage,{hdc}) end procedure procedure EndDoc(atom hdc) object fnVal fnVal = c_func(iEndDoc,{hdc}) end procedure procedure TextOut(atom hdc,integer x, integer y, sequence text) atom fnVal, pText pText = allocate_string(text) fnVal = c_func(iTextOut,{hdc,x,y,pText,length(text)}) free(pText) end procedure function PrinterSelect() -- this return the display context of printer. atom fnVal,pPRINTDLG, hPrinterHdc pPRINTDLG= allocate(PDLG_SIZE) mem_set(pPRINTDLG,0,PDLG_SIZE) poke4(pPRINTDLG,PDLG_SIZE) poke4(pPRINTDLG+20,or_bits(PD_USEDEVMODECOPIESANDCOLLATE,PD_RETURNDC)) -- Flags fnVal = c_func(iPrintDlg,{pPRINTDLG}) hPrinterHdc = 0 if fnVal then hPrinterHdc = peek4u(pPRINTDLG+16)--PRINTDLG.hdc end if free(pPRINTDLG) return hPrinterHdc end function constant TEXT="The quick brown fox jump over the lazy dog" object void integer vppi -- printer pixel per inch vertical atom hPrnDc -- 1) let the user select printer hPrnDc = PrinterSelect() if hPrnDc then -- 2) Get printer vertical resolution in pixel per inch. vppi = GetDeviceCaps(hPrnDc,LOGPIXELSY) -- 3) open document StartDoc(hPrnDc,"printing demo") StartPage(hPrnDc) -- 4) Begin a new page StartPage(hPrnDc) -- 5) send data to printer for i = 1 to 10 do -- print 10 lines of text 6 lines per inches TextOut(hPrnDc,10,i*vppi/6,TEXT) end for -- 6) end page EndPage(hPrnDc) -- 7) Close Document EndDoc(hPrnDc) -- 8) release printer DC void = c_func(iDeleteDC,{hPrnDc}) end if
regards, Jacques DeschĂȘnes