Print rtf formatted text from editorgadget?

Just starting out? Need help? Post your questions and find answers here.
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Print rtf formatted text from editorgadget?

Post by RSBasic »

Hello Shardik
Thank you for the new information. Image
Image
Image
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Print rtf formatted text from editorgadget?

Post by Keya »

very awesome Shardik thankyou! :)
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Print rtf formatted text from editorgadget?

Post 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.
Amateur Radio, D-STAR/VK3HAF
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Print rtf formatted text from editorgadget?

Post 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
Last edited by RASHAD on Wed May 25, 2016 7:01 am, edited 2 times in total.
Egypt my love
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Print rtf formatted text from editorgadget?

Post 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:):)
Amateur Radio, D-STAR/VK3HAF
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Print rtf formatted text from editorgadget?

Post 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:
Egypt my love
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Print rtf formatted text from editorgadget?

Post 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?
Amateur Radio, D-STAR/VK3HAF
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Print rtf formatted text from editorgadget?

Post 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
Amateur Radio, D-STAR/VK3HAF
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Print rtf formatted text from editorgadget?

Post 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
Egypt my love
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Print rtf formatted text from editorgadget?

Post 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.
Amateur Radio, D-STAR/VK3HAF
User avatar
blueb
Addict
Addict
Posts: 1044
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Print rtf formatted text from editorgadget?

Post 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
- It was too lonely at the top.

System : PB 6.10 LTS (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Print rtf formatted text from editorgadget?

Post 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:):):)
Amateur Radio, D-STAR/VK3HAF
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Print rtf formatted text from editorgadget?

Post by RASHAD »

Try NCH Bolt PDF printer driver
Egypt my love
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Print rtf formatted text from editorgadget?

Post by Fangbeast »

RASHAD wrote:Try NCH Bolt PDF printer driver
Never heard of that one, will try it too, thanks!
Amateur Radio, D-STAR/VK3HAF
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Print rtf formatted text from editorgadget?

Post 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.
Amateur Radio, D-STAR/VK3HAF
Post Reply