Page 2 of 3

Re: Print rtf formatted text from editorgadget?

Posted: Sat May 21, 2016 10:45 am
by RSBasic
Hello Shardik
Thank you for the new information. Image

Re: Print rtf formatted text from editorgadget?

Posted: Sat May 21, 2016 12:34 pm
by Keya
very awesome Shardik thankyou! :)

Re: Print rtf formatted text from editorgadget?

Posted: Wed May 25, 2016 4:08 am
by Fangbeast
RASHAD, I have a question...

I incorporated your code and tested it with my recipe manager but when I launched it, I kept getting Memory Read Error with this command and it kept launching my printer driver instead of my chosen PDF driver.

PrintEditorGadgetText(WindowID(#Window_Help), GetModuleHandle_(0), GadgetID(#Gadget_Help_Help), 0, 0, 0, 0)

On a hunch, I uninstalled my pdf reader and installed FOXIT PDF reader as it has it's own PDF print driver, launched the code again and it worked after slecting foxit.

That parameter you feed to GetModuleHandle_(0), where does it come from? Is it a window id, a gadget id or something else? I was wondering if that was the cause of my memory read error because I fed it the wrong paramater.

Re: Print rtf formatted text from editorgadget?

Posted: Wed May 25, 2016 5:46 am
by RASHAD
Hi Fang
Check and report

Code: Select all

#PD_NONETWORKBUTTON = $200000

Global PrinterDC

Procedure  StartPrint(Doc.s)
  mDI.DOCINFO
  mDI\cbSize = SizeOf(DOCINFO)
  mDI\lpszDocName = @Doc
  mDI\lpszOutput = 0
  StartDoc_(PrinterDC,mDI)
EndProcedure

Procedure PrintRichText(hWnd, hInst, rtfEdit, LM, RM, TM, BM)
  
  fr.FORMATRANGE
  pd.PRINTDLG
  
  pd\lStructSize = SizeOf(PRINTDLG)
  pd\hwndOwner = hWnd
  pd\hDevMode = 0
  pd\hDevNames = 0
  pd\nFromPage = 0
  pd\nToPage = 0
  pd\nMinPage = 0
  pd\nMaxPage = 0
  pd\nCopies = 0
  pd\hInstance = hInst
  pd\Flags = #PD_HIDEPRINTTOFILE | #PD_NONETWORKBUTTON |#PD_RETURNDC | #PD_PRINTSETUP
  pd\lpfnSetupHook = 0
  pd\lpSetupTemplateName = 0
  pd\lpfnPrintHook = 0
  pd\lpPrintTemplateName = 0
   
  If PrintDlg_(pd)
    PrinterDC = pd\hDC
  Else
    PrinterDC = DefaultPrinter()
  EndIf
  
  If PrinterDC
         
  fr\hdc = PrinterDC
  fr\hdcTarget = PrinterDC
  fr\chrg\cpMin = 0
  fr\chrg\cpMax = -1
  fr\rc\top = TM*1440
  fr\rcPage\top = fr\rc\top
  fr\rc\left = LM*1440
  fr\rcPage\left = fr\rc\left
  
  iWidthTwips = Int((GetDeviceCaps_(PrinterDC, #HORZRES)/GetDeviceCaps_(PrinterDC, #LOGPIXELSX))*1440)
  iHeightTwips = Int((GetDeviceCaps_(PrinterDC, #VERTRES )/GetDeviceCaps_(PrinterDC, #LOGPIXELSY))*1440)
  fr\rc\right = iWidthTwips-RM*1440
  fr\rcPage\right = fr\rc\right
  fr\rc\Bottom = iHeightTwips-BM*1440
  fr\rcPage\bottom = fr\rc\bottom
  
  StartPrint("RTF Printing")
  StartPage_(PrinterDC)
  
  iTextOut = 0
  iTextAmt = SendMessage_(rtfEdit, #WM_GETTEXTLENGTH, 0, 0)
  While iTextOut<iTextAmt
     iTextOut = SendMessage_(rtfEdit, #EM_FORMATRANGE, 1, fr)           
     If iTextOut < iTextAmt                   
        ;EndPage_(PrinterDC)
        ;StartPage_(PrinterDC)
        fr\chrg\cpMin = iTextOut
        fr\chrg\cpMax = -1 
        iTextAmt = iTextAmt - iTextOut
        iTextOut = 0
     EndIf
  Wend
  SendMessage_(rtfEdit, #EM_FORMATRANGE, 1, 0)
  EndPage_(PrinterDC)
  EndDoc_(PrinterDC)
  DeleteDC_(PrinterDC)
  EndIf
EndProcedure

Procedure StreamFileInCallback(hFile, pbBuff, cb, pcb)
  ProcedureReturn ReadFile_(hFile, pbBuff, cb, pcb, 0)!1
EndProcedure

Procedure loadFile(pFilePath.s)
  If ReadFile(0, pFilePath)
    If GetExtensionPart(pFilePath)="rtf"
      uFormat = #SF_RTF
    Else
      uFormat = #SF_TEXT
    EndIf
    edstr.EDITSTREAM
    edstr\dwCookie = FileID(0)
    edstr\dwError = 0
    edstr\pfnCallback = @StreamFileInCallback()
    SendMessage_(GadgetID(0), #EM_STREAMIN, uFormat, edstr)
    CloseFile(0)
  Else
    MessageRequester("Error", "Error Occured While Opening File", #PB_MessageRequester_Ok)
  EndIf
EndProcedure

OpenWindow(0, 200, 50, 640, 400,"RTF PRINTING", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CreateMenu(0, WindowID(0))
  MenuTitle("&File")
    MenuItem(0, "&Open")
    MenuItem(1, "&Print")
    MenuItem(2, "&Quit")
EditorGadget(0, 0, 0, WindowWidth(0), WindowHeight(0))
SendMessage_(GadgetID(0), #EM_LIMITTEXT, -1, 0)

a.s="{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\colortbl ;\red255\green0\blue0;\red0\green0\blue0;}"
a.s=a.s+"{\*\generator Msftedit 5.41.15.1503;}\viewkind4\uc1\pard\f0\fs20 Hello, this is \cf1\b\fs32 RTF\cf2\b0\fs20 direct!\cf0\par}"
*MemoryBuffer = AllocateMemory(StringByteLength(a, #PB_Ascii) + 1)
PokeS(*MemoryBuffer, a.s, -1, #PB_Ascii)
SetGadgetText(0,PeekS(*MemoryBuffer))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
      
    Case #PB_Event_Menu
      Select EventMenu()
        Case 0
          FileName$ = OpenFileRequester("", "", "RTF (*.rtf|*.rtf|Text (*.txt)|*.txt;*.bat|All files (*.*)|*.*", 0)
          If FileName$
            loadFile(FileName$)
          EndIf
          
        Case 1
          PrintRichText(WindowID(0), GetModuleHandle_(0), GadgetID(0), 0, 0, 0, 0)
          
        Case 2
          Quit = 1
          
      EndSelect
  EndSelect 
Until Quit = 1

Re: Print rtf formatted text from editorgadget?

Posted: Wed May 25, 2016 6:02 am
by Fangbeast
I'll try it as soon as I cans top coughing but I'd still like to know GetModuleHandle_(0) where the 0 parameter for GetModuleHandle_() comes from if you don't mind?

I am trying to further my API education from the master:):)

Re: Print rtf formatted text from editorgadget?

Posted: Wed May 25, 2016 6:38 am
by RASHAD
Hi Fang

Previous post updated to hide the network button

GetModuleHandle_(0) = The Handle of the file used To create the calling process which will be used as hInstance in PRINTDLG Structure (used to Select the proper printing device)

MSDN :
hInstance
Type: HINSTANCE
If the PD_ENABLEPRINTTEMPLATE or PD_ENABLESETUPTEMPLATE flag is set in the Flags member, hInstance is a handle to the application or module instance that contains the dialog box template named by the lpPrintTemplateName or lpSetupTemplateName member.

It is not needed in our case only for completeness maybe you needed later :wink:

Re: Print rtf formatted text from editorgadget?

Posted: Wed May 25, 2016 8:58 am
by Fangbeast
RASHAD wrote:Hi Fang

Previous post updated to hide the network button

GetModuleHandle_(0) = The Handle of the file used To create the calling process which will be used as hInstance in PRINTDLG Structure (used to Select the proper printing device)

MSDN :
hInstance
Type: HINSTANCE
If the PD_ENABLEPRINTTEMPLATE or PD_ENABLESETUPTEMPLATE flag is set in the Flags member, hInstance is a handle to the application or module instance that contains the dialog box template named by the lpPrintTemplateName or lpSetupTemplateName member.

It is not needed in our case only for completeness maybe you needed later :wink:
Thank you for that. When I get out of this blasted cold, I will have other questions if that's okay?

Re: Print rtf formatted text from editorgadget?

Posted: Thu May 26, 2016 8:38 am
by Fangbeast
RASHAD, something has gone wrong with your latest example (and the previous one). It is not printing all the text in the help screen but starting a page and a bit further down and some of the text is getting mangled further down???

I have no idea what to do with this:(

Hang on a sec, I might have missed something.

Damn, no I didn't

Re: Print rtf formatted text from editorgadget?

Posted: Thu May 26, 2016 11:53 am
by RASHAD
Hi Fang
BTW : Most of your RTF Files are empty except Main & About
Tested with MS PDF deriver (Windows 10 x64) and it is OK now
Testing with HP are costly so you do it and report :P

Replace :

Code: Select all

Procedure PrintRichText(hWnd, hInst, rtfEdit, LM, RM, TM, BM)

  fr.FORMATRANGE
  pd.PRINTDLG
  
  pd\lStructSize = SizeOf(PRINTDLG)
  pd\hwndOwner = hWnd
  pd\hDevMode = 0
  pd\hDevNames = 0
  pd\nFromPage = 0
  pd\nToPage = 0
  pd\nMinPage = 0
  pd\nMaxPage = 0
  pd\nCopies = 0
  pd\hInstance = hInst
  pd\Flags = #PD_HIDEPRINTTOFILE | #PD_NONETWORKBUTTON |#PD_RETURNDC | #PD_PRINTSETUP
  pd\lpfnSetupHook = 0
  pd\lpSetupTemplateName = 0
  pd\lpfnPrintHook = 0
  pd\lpPrintTemplateName = 0
   
  If PrintDlg_(pd)
    PrinterDC = pd\hDC
  Else
    PrinterDC = DefaultPrinter()
  EndIf
  
  If PrinterDC
         
  fr\hdc = PrinterDC
  fr\hdcTarget = PrinterDC
  fr\chrg\cpMin = 0
  fr\chrg\cpMax = -1
  fr\rc\top = TM*1440
  fr\rcPage\top = fr\rc\top
  fr\rc\left = LM*1440
  fr\rcPage\left = fr\rc\left
  
  iWidthTwips = Int((GetDeviceCaps_(PrinterDC, #HORZRES)/GetDeviceCaps_(PrinterDC, #LOGPIXELSX))*1440)
  iHeightTwips = Int((GetDeviceCaps_(PrinterDC, #VERTRES )/GetDeviceCaps_(PrinterDC, #LOGPIXELSY))*1440)
  fr\rc\right = iWidthTwips-RM*1440
  fr\rcPage\right = fr\rc\right
  fr\rc\Bottom = iHeightTwips-BM*1440
  fr\rcPage\bottom = fr\rc\bottom
  
  StartPrint("RTF Printing")
  StartPage_(PrinterDC)
  
  iTextOut = 0
  iTextAmt = SendMessage_(rtfEdit, #WM_GETTEXTLENGTH, 0, 0)
  While iTextOut<iTextAmt
   iTextOut = SendMessage_(rtfEdit, #EM_FORMATRANGE, 1, fr)           
   If iTextOut<iTextAmt                   
      EndPage_(PrinterDC)
      StartPage_(PrinterDC)
      fr\chrg\cpMin = iTextOut
      fr\chrg\cpMax = -1 
      iTextAmt = iTextAmt - iTextOut
      iTextOut = 0
   EndIf
  Wend
  SendMessage_(rtfEdit, #EM_FORMATRANGE, 0, 0)
  EndDoc_(PrinterDC)
  DeleteDC_(PrinterDC)
  EndIf
EndProcedure

Re: Print rtf formatted text from editorgadget?

Posted: Thu May 26, 2016 12:31 pm
by Fangbeast
Hello RASHAD (Cough, cough, sneeze)
BTW : Most of your RTF Files are empty except Main & About
Yes, I know. They are for users to fill in, in their own way and language. This is a source code project and not an application for sale so each programmer must compile their own version of it for their users with their own help filled in. I made a proper PDF help file as well, that took forever.

The main form has a help page as an example.
Tested with MS PDF deriver (Windows 10 x64) and it is OK now
Thank you, I can see that.

My debt to you and InfraTec is rapidly going through the roof:)
Testing with HP are costly so you do it and report :P
I never waste paper but always print to virtual pdf driver. Have to complain to the folks at foxit, their product crashes at every document open now. Will have to find another free pdf driver and reader.

Re: Print rtf formatted text from editorgadget?

Posted: Thu May 26, 2016 1:39 pm
by blueb
Will have to find another free pdf driver and reader.
I've been using Sumatra PDF on 3 Windows 10 machines with no problems, and very light-weight, as well.

http://www.sumatrapdfreader.org/free-pdf-reader.html

Re: Print rtf formatted text from editorgadget?

Posted: Thu May 26, 2016 2:02 pm
by Fangbeast
blueb wrote:
Will have to find another free pdf driver and reader.
I've been using Sumatra PDF on 3 Windows 10 machines with no problems, and very light-weight, as well.

http://www.sumatrapdfreader.org/free-pdf-reader.html
Yes, I've tried that a few others thanks. I found a hint in the foxit forum that if you turn off the advert, foxit will no longer crash when launching or loading a document and it works.

Now it only crashes on exit:):):)

Re: Print rtf formatted text from editorgadget?

Posted: Thu May 26, 2016 2:52 pm
by RASHAD
Try NCH Bolt PDF printer driver

Re: Print rtf formatted text from editorgadget?

Posted: Thu May 26, 2016 9:35 pm
by Fangbeast
RASHAD wrote:Try NCH Bolt PDF printer driver
Never heard of that one, will try it too, thanks!

Re: Print rtf formatted text from editorgadget?

Posted: Fri May 27, 2016 9:20 am
by Fangbeast
RASHAD, i've set a left and right margin on my editorgadget of 10 pixels each as the text didn't look nice pressed right up against the gadget edges.

How do I adjust your printing code to include that in? At the moment, the text is printed right against the edges, no matter what the gadget settings are.