Announcement

Collapse
No announcement yet.

Page to graphic font size ??

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Page to graphic font size ??

    Have 8.5 X 11 page with 58 rows of characters with font 12PT

    convert to graphic page which is 953 pixels in height . 953/58 = 16.43

    WHAT is the formula to set font size GRAPHIC window ?

    Just plugging in font size numbers got 10.2pt to fit 58 rows of text in graphic window

    used a formula : lFNT = font size , lGRH = 953 graphic height size , .75 = 12/72

    lFNT=lGRH/60: lFNT=lFNT *.75 = 12pt , to big should be around 10.2pt
    Robert

  • #2
    From where did you get "953" and "58"?
    Walt Decker

    Comment


    • #3
      16.43 pixels per line (of text) calculation looks okay. That is 12 pixels for upper case letters, 2 pixels above letters for white space, and 4 pixels below (4 for white space or 3 for lower case "gjqpy" and 1 white. (the original font being 12pt and target font being 12px is a coincidence)

      Next you need pixels per inch for current screen. There is API for that, but I've never used it. Then you calculate point size that is close to 12 pixels in inches.

      Cheers,
      Dale

      Comment


      • #4
        953 pixel is my computer screen graphic height that displays for a scale down (8.5" x 11" paper ) 11" height which you can print 58 rows of text with font 12
        Robert

        Comment


        • #5
          GRAPHIC TEXT SIZE (or with less accuracy GRAPHIC CHR SIZE) can be used to determine the maximum pitch of a given font and text string which will fit inside a target box. You could thus determine the pitch to use for your screen.

          Something like this, code cut from a function which caclulates the largest pitch for text to fit in a box:

          Code:
              n = %smallestpitch
              ' assume no font selected
              ' target box dimensions boxX, boxY
              do
                  font new sfont, n to hfont
                  graphic set font hfont
                  graphic text size stext to rw, rh
                  if (rW > boxX) or (rH > boxY) then
                      decr n
                      graphic set font 0
                      font end hfont
                      exit loop
                  end if
                  incr n
                  graphic set font 0
                  font end hfont
              loop
          '    now n has the pitch of the font which will fit in the target box

          Comment


          • #6
            then 953 must be the desktop client Y * the PPI Y value (e.g. dty * ppiy). I doubt if you can calculate the font point size from that. I think you will have to use the pixels per inch value and convert points per inch to that value.
            Walt Decker

            Comment


            • #7
              Looks like I did not give enough info for my problem that I want to solve.
              So here in more info and a sample code.

              Have my desktop computer window 7 pro 1920 x 1080 res. 23" diagonal PPI 95.78 pitch .2652
              and
              a 2 in 1 notebook/tablet window 10 home 1920 x 1200 res. 12.2 diagonal PPI 185.59 pitch .1369
              (got some info from PPI calculator at https://calculator-online.net/ppi-calculator/ )

              I will print out a document on 8 1/2 x 11 paper having 50 rows of text. A top margin
              and bottom margin which would add 8 blank rows of 12pt font = 58 rows.

              The dialog and the graphic window I have done , and scaled it for both computers to display
              with a correct scale that shows a full page.

              The problem is scaling the correct size font for both computer monitors.
              Using the info have gone around in circles and can not find the right formula that
              would work for both.

              One way was to use 12pt font = .1666 and scale that from the 11" height to height of the dialog
              graphic in inches and that did not work.

              Do not know if GRAPHIC TEXT SIZE would work here , have not tried it yet.

              code below:

              Code:
              #COMPILE EXE
              #DIM ALL
              #INCLUDE "WIN32API.INC"
              
              %IDC_GRAPHIC  = 2160
              GLOBAL hDlg4 AS LONG
              
              SUB PRINT_RPT_GText()
               LOCAL I AS LONG, T1,T2 AS STRING
              
               GRAPHIC ATTACH hDlg4, %IDC_GRAPHIC: GRAPHIC CLEAR ': GRAPHIC COLOR -1,RGB(181,189,172)
               'GRAPHIC PRINT "":GRAPHIC PRINT "":GRAPHIC PRINT "": GRAPHIC PRINT ""':  GRAPHIC PRINT POS(60) "DDD": GRAPHIC PRINT ""
              
               T2="1234567890123456789012345678901234567890123456789012345678901234567890123456789012345"
              
                GRAPHIC SET POS (55,50)    ': ? STR$(58*.5)     55   50
              
               FOR I=1 TO 50: T1=STR$(I)+"   ": T1=MID$(T1,1,4)
              
               GRAPHIC PRINT POS(55) T1+T2:
               NEXT
              
               GRAPHIC DETACH
              END SUB
              
              
              CALLBACK FUNCTION PRINT_RPT_CANCEL()
               DIALOG END CBHNDL, 1
              END FUNCTION
              
              FUNCTION PBMAIN() AS LONG
               LOCAL Tp1, ncWidth, ncHeight, LRSLT, lSCRW, lSCRH, lDLGH, iDLGW, lGRH, lGRW  AS LONG, fhnd4, fhnd5 AS DWORD, lFNT AS SINGLE
              
               DESKTOP GET CLIENT TO iDLGW, lDLGH         ':   ? STR$(iDLGW)+"   "+STR$(lDLGH) '1536   920
               lDLGH=lDLGH * .9 : iDLGW=lDLGH * 1.3401    ':   ? STR$(iDLGW)+"   "+STR$(lDLGH) '1110   828
              
               lGRH= lDLGH *.98 : lGRW=lGRH * .7727      ':   ? STR$(lGRW) +"   "+STR$(lGRH)  '1050   811
                                                 '? STR$(iDLGW)    '1303
              
               'remark: some numbers I used hope its not confusing.
               'MINMUM SCREEN SIZE 1366px X 768px  14.22" X 8" (/96) ( lSCRW , lSCRH )  1.77865>H
               'DIALOG 820.8px X 672px  ,  8.55" X 7"                                   1.341>H
               'GRAPHIC WINDOW 739.2pxW X 633.6pxH  ,  7.7" X 6.6"     ( lGRW , lGRH )  1.1590>W
               '8X11 FULL PAGE 465.6px X 601.92px  , 4.85" X 6.27"                      1.2941>w
              
               'EXAMPLE: SCREEN 1920pxW 1080pxH = 20"X11.25" (lSCRW X lSCRH)
               'DIALOG HGT AT .9 OF SCREEN WIDTH 1080pxH X .98 = 1058.4pxH
               'DIALOG WDT AT .9 OF SCREEN HGT 820.8 / 672 = 1.22 , 1.22 X 1058 = 1290.76pxW DIALOG
              
              ''' 'GRAPHIC SCREEN MAIN 1290.76pxH X .98 = 874.8pxH , 874.8/633.6 = 1.318 X 874.8 = 1153.2pxW
               'GRAPHIC SCREEN MAIN 1290.76pxH X .98 = 874.8pxH , 874.8/633.6 = 1.318 X 874.8 = 1153.2pxW
              
              
               DIALOG NEW PIXELS, 0, "TEST REPORT", , , iDLGW,lDLGH, _                 '  =1075-50=1025
               %WS_THICKFRAME OR %WS_CAPTION OR %WS_SYSMENU OR %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX, TO hDlg4
              
               FONT NEW "Times New Roman"   ,12,      0,       1,       0,       0         TO fhnd4  ': ? STR$(lGRW)+"   "+STR$(lGRH)
              
                'lFNT=lGRH/19.2: lFNT=lFNT *.75  ': ? STR$(lFNT)+"  "+STR$(lGRH) '58 lines per page font 12  , 72/96=.75
                lFNT=.16666 * .902045454: lFNT=lFNT *72 ': ? STR$(lFNT)+"  "+STR$(lGRH) '58 lines per page font 12  , 72/96=.75
                                           ' ? STR$(lFNT) +"  x"
               FONT NEW "Times New Roman"   ,lFNT,      0,       1,       0,       0         TO fhnd5  ': ? STR$(lGRW)+"   "+STR$(lGRH) graphic
              
              
              
              
               CONTROL ADD GRAPHIC, hDlg4, %IDC_GRAPHIC, "", iDLGW-(iDLGW*.825),10,lGRW,lGRH,%SS_SUNKEN OR %WS_BORDER,      '1105 953
              
               GRAPHIC ATTACH hDlg4, %IDC_GRAPHIC:   GRAPHIC SET FONT fhnd5
              
              
               GRAPHIC COLOR -1 , %WHITE  'FOREGROUND BLUE, BACKGROUND %RGB_GAINSBORO , -1 DEFAULT BLACK FOREGROUDN  , BACKGROUND %WHITE
              
               PRINT_RPT_GText:    FONT END fhnd4
              
               CONTROL ADD BUTTON, hDlg4, 100, "&ZOOM"    ,iDLGW-125, 40, 90, 26, 'CALL PRINT_RPT_PText
               CONTROL ADD BUTTON, hDlg4, 101, "&PRINT" ,iDLGW-125, 80, 90, 26, 'CALL PRINT_RPT_PText
               CONTROL ADD BUTTON, hDlg4, 110, "&EXIT " ,iDLGW-125,120, 90, 26,  CALL PRINT_RPT_CANCEL
               CONTROL SET FOCUS   hDlg4, 110
              
               CONTROL SET FONT hDlg4, 100, fhnd4   '13
               CONTROL SET FONT hDlg4, 101, fhnd4   '13
               CONTROL SET FONT hDlg4, 110, fhnd4   '13
              
               'DIALOG SHOW MODAL hDlg4,  CALL PRINT_PREVIEW_DlgProc TO LRSLT
               DIALOG SHOW MODAL hDlg4,   TO LRSLT
              END FUNCTION
              Robert

              Comment


              • #8
                Robert,
                I'm sorry but I'm still a bit confused about your need, but let me make try to help.

                You have 2 monitors of different resolution and you want to display the same 58 lines of text on each monitor. Correct?

                So all you need is the font size to use for each monitor size, such that the 58 lines will fit in each monitor/resolution, yes?

                Comment


                • #9
                  This code implements what I just described. You input a multiline text string and the needed font size is computed, then used to display the text string centered on a Graphic control -each line centered left/right and entire string centered top/bottom. It should be easy to change the alignment choices. For convenience, the font size is placed in the dialog caption.


                  Code:
                  'Compilable Example:
                  #Compile Exe
                  #Dim All
                  #Include "win32api.inc"
                  
                  %IDC_Graphic = 500
                  Global hDlg,hGraphic,hFont As Dword
                  
                  Function PBMain () As Long
                     Dialog New Pixels, 0, "Font Size:",100,100,400,400, %WS_OverlappedWindow To hDlg
                     Control Add Graphic, hDlg, %IDC_Graphic,"", 0,0,400,400, %WS_Visible Or %SS_Sunken
                     Control Handle hDlg, %IDC_Graphic To hGraphic
                     Graphic Attach hDlg, %IDC_Graphic, ReDraw
                     Dialog Show Modal hDlg Call DlgProc
                  End Function
                  
                  CallBack Function DlgProc() As Long
                     Select Case Cb.Msg
                        Case %WM_Size
                           Local w,h As Long
                           Dialog Get Client hDlg To w,h
                           Control Set Size hDlg, %IDC_Graphic, w,h
                           ShowMultiLineMessage "One for the money" + $CrLf + "Two for the show!"
                     End Select
                  End Function
                  
                  Sub ShowMultilineMessage(ByVal Msg$)
                  '   'size message to fit Graphic then print the message, centered
                     Local i,LineCount,iLongestLine,wMax,x,y,w,h,LineHeight As Long
                     Local temp$, LongestLine$, FontName As WStringZ * %Max_Path, FontSize, FontAttr As Long
                     Local hTempBMP, hTempFont As Dword
                  
                     LineCount     = ParseCount(Msg$,$CrLf)
                     'get longest line
                     For i = 1 To ParseCount(Msg$,$CrLf)
                        w = Graphic(Text.Size.X,Parse$(Msg$,$CrLf,i)) 'width of line
                        If w > wMax Then iLongestLine = i : wMax = w  'track longest line
                     Next i
                     LongestLine$ = Parse$(Msg$,$CrLf,iLongestLine)
                  
                     'FontSize
                     Graphic Get Client To w,h
                     Graphic Bitmap New 100,100 To hTempBMP
                     Graphic Attach hTempBMP, 0
                     Font New FontName, 1000, 1 To hTempFont
                     Graphic Set Font hTempFont
                     x = Graphic(Text.Size.X,LongestLine$)
                     y = Graphic(Text.Size.Y,LongestLine$) * LineCount
                     Graphic Bitmap End
                     FontSize = 1000/IIf( x/w > y/h , x/(w*0.9) , y/(h*0.9))
                     FontSize = Min(156,FontSize)
                     Font New FontName, FontSize, FontAttr To hFont 'hFont fits longest line to width of Graphic control
                     Dialog Set Text hDlg, "Font Size = " + Str$(FontSize)
                     'print Message$
                     Graphic Attach hDlg, %IDC_Graphic, ReDraw
                     Graphic Clear
                     Graphic Set Font hFont
                     Graphic Get Client To w,h
                     LineHeight    = Graphic(Text.Size.Y,"X")
                  
                     For i = 1 To LineCount
                        temp$ = Parse$(Msg$, $CrLf, i)
                        x = (w-Graphic(Text.Size.X,temp$))/2
                        y = (h-LineCount*LineHeight)*0.3 + (i-1) * LineHeight  '.3 is just a preference - keep it slightly above midpoint
                        Graphic Set Pos(x,y)
                        Graphic Print temp$
                     Next i
                  
                     Font End hFont
                     Graphic ReDraw
                  End Sub

                  Comment


                  • #10
                    Alternatively, output the report to a bitmap in all cases and XPRINT or GRAPHIC STRETCH it to the dimensions of the appropriate output device.

                    Comment


                    • #11
                      Originally posted by Robert Alvarez View Post
                      The problem is scaling the correct size font for both computer monitors.
                      Using the info have gone around in circles and can not find the right formula that
                      would work for both.
                      If it is already a page for printing, just make it a bitmap and make your control
                      the proper aspect ration and STRETCH it to fit.

                      The world is strange and wonderful.*
                      I reserve the right to be horrifically wrong.
                      Please maintain a safe following distance.
                      *wonderful sold separately.

                      Comment


                      • #12
                        The API function I was thinking of in my previous might be GetDeviceCapabilities. It might return in metric, so you'll need to convert to inches. Then calculate how many seventysecondths of an inch are in 12 pixels. That is the font size (because one point is 1/72 inches) A possible error comes from how much white space (by font face and how it is distributed)((White space can also be programmatically changed.))

                        (((post 1 was clear enough (unless I got it wrong) What size font puts about 16 lines of text in 953 vertical pixels?)))

                        Cheers
                        Dale

                        Comment


                        • #13
                          Here is more info in what I am trying to do. This is the first step in developing a print preview zoom. There will be two types of print preview zooms I would like to do. The first one where zoom scales are 1:1, 1:1.25 and 1:1.50 with vert. scrolling for larger scales . The next one would be like auto scaling when you resize the dialog window which would be narrow in size to the sides of the screen making the dialog enlarge as the window gets resized horiz. and the text enlarge also. There is still a lot I have to figure out for that.

                          Something like the program PRINT DLL which I have or like the PDF print preview. And yes maybe those are bitmaps. That was the next step I was going to explore. Bitmaps. So now to work out the code for bitmap scaling with text.
                          Robert

                          Comment


                          • #14
                            Dale I tried using 72pt scaling. 12pt = .16666" , scale that down by a percent to a smaller screen and the scaling does not work. The results are that the scale text is too small or too large . Not the correct proportion .
                            Will check out Gary's code.
                            Robert

                            Comment


                            • #15
                              Robert, if you have PBWin 9.0, there's a sample in PBWin90\Samples\DDT\Graphic\TextView\ with code for a complete text file print / print preview with select Font dialog, different zoom factors, resize to window preview and way to set margins, etc. It even comes with a nice "hollow" looking glass cursor. Just as a tip..

                              Comment


                              • #16
                                Scaling? Not what I said. You did not find physical size of pixel, Bye.
                                Dale

                                Comment


                                • #17
                                  Borje found that program , for those that do not have PB9 here is the program code with attachments . Will try to get what I need . May need help along the way.
                                  There were about 4 files not allowed to upload . So maybe the main code may help someone.

                                  Code:
                                  '====================================================================
                                  '
                                  '  TextView.bas for PowerBASIC Compiler for Windows
                                  '  Copyright (c) 2005 - 2008 PowerBASIC, Inc.
                                  '  All Rights Reserved.
                                  '
                                  '--------------------------------------------------------------------
                                  '
                                  '  Demo for the Print Preview dialog include file, Preview.inc.
                                  '
                                  '  The program has been XP Theme enabled via a file called xptheme.xml,
                                  '  which is embedded through the program's resource file (TEXTVIEW.RC).
                                  '
                                  '====================================================================
                                  ' Declares
                                  '----------------------------------------------------------
                                  #COMPILER PBWIN 9
                                  #COMPILE EXE
                                  #RESOURCE "TEXTVIEW.PBR"  ' Resource file - see TEXTVIEW.RC
                                  '----------------------------------------------------------
                                  %USEMACROS = 1            ' Use Macros for leanest result
                                  #INCLUDE "COMDLG32.INC"   ' Common dialog declares
                                  #INCLUDE "PREVIEW.INC"    ' Preview dialog include
                                  '----------------------------------------------------------
                                  %IDC_BUTNOPEN   = 120     ' Control id's
                                  %IDC_BUTNVIEW   = 121
                                  %IDC_BUTNPAGE   = 122
                                  %IDC_BUTNFONT   = 123
                                  %IDC_TEXTBOX1   = 131
                                  '----------------------------------------------------------
                                  $PROGNAME = "TextView"     ' Program title string equate
                                  '----------------------------------------------------------
                                  
                                  
                                  '====================================================================
                                  FUNCTION PBMAIN()
                                  '--------------------------------------------------------------------
                                  ' Program entry point
                                  '--------------------------------------------------------------------
                                    LOCAL hDlg AS LONG
                                  
                                    DIALOG NEW 0, $PROGNAME, , , 380, 240, %WS_CAPTION OR %WS_SYSMENU OR _
                                                  %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX OR %WS_THICKFRAME TO hDlg
                                  
                                    DIALOG SET ICON hDlg, "APPICON"
                                  
                                    '------------------------------------------------------------------
                                    CONTROL ADD BUTTON, hDlg, %IDC_BUTNOPEN,  "&Open",      2, 3, 50, 14
                                    CONTROL ADD BUTTON, hDlg, %IDC_BUTNVIEW,  "&Preview",  52, 3, 50, 14
                                    CONTROL ADD BUTTON, hDlg, %IDC_BUTNPAGE,  "&Margins", 106, 3, 50, 14
                                    CONTROL ADD BUTTON, hDlg, %IDC_BUTNFONT,  "&Font",    156, 3, 50, 14
                                    CONTROL ADD BUTTON, hDlg, %IDCANCEL,      "E&xit",    210, 3, 50, 14
                                  
                                    CONTROL ADD TEXTBOX, hDlg, %IDC_TEXTBOX1, "", 0, 20, 201, 101, _
                                                %ES_MULTILINE OR %ES_WANTRETURN OR %WS_HSCROLL OR _
                                                %WS_VSCROLL OR %WS_TABSTOP, %WS_EX_CLIENTEDGE
                                  
                                  
                                    '------------------------------------------------------------------
                                    ' Note: the standard TextBox has a theoretical 64 KB max text size
                                    ' limit in Win95/98, but usually it's even less than that. To set
                                    ' as high limit as possible, %EM_SETLIMITTEXT can be used, with
                                    ' wParam = 0 to set the maximum text limit. In Windows XP there
                                    ' should be no practical text size limitations at all..
                                    '------------------------------------------------------------------
                                    CONTROL SEND hDlg, %IDC_TEXTBOX1, %EM_SETLIMITTEXT, 0, 0
                                  
                                    '------------------------------------------------------------------
                                    DIALOG SHOW MODAL hDlg, CALL DlgProc
                                  
                                  END FUNCTION
                                  
                                  
                                  '====================================================================
                                  CALLBACK FUNCTION DlgProc() AS LONG
                                  '--------------------------------------------------------------------
                                  ' Main Dialog procedure
                                  '--------------------------------------------------------------------
                                    LOCAL h, w, x, y AS LONG
                                    LOCAL sText, sTitle AS STRING
                                    LOCAL hNewFont AS LONG
                                    STATIC hFont AS LONG
                                  
                                    SELECT CASE AS LONG CBMSG
                                    CASE %WM_INITDIALOG  ' <- Received right before the dialog is shown
                                        '------------------------------------------------------------------
                                        ' The main program is responsible for maintaining a Static or Global
                                        ' PreviewData variable and calling InitPreviewDlg before the Print
                                        ' Preview dialog in Preview.inc is used.
                                        '------------------------------------------------------------------
                                        STATIC pd AS PreviewData  ' <- Defined in Preview.inc
                                        InitPreviewDlg pd         ' <- Located in Preview.inc
                                  
                                        '------------------------------------------------------------------
                                        ' Create and set an initial fixed-width font in the TextBox.
                                        '------------------------------------------------------------------
                                        FONT NEW "Courier New", 10 TO hFont
                                        CONTROL SET FONT CB.HNDL, %IDC_TEXTBOX1, hFont
                                  
                                    CASE %WM_COMMAND
                                        '----------------------------------------------------
                                        ' Messages from the controls are handled here.
                                        ' Example: Button clicks generates a %BN_CLICKED message
                                        '          and menu clicks generates the value 1.
                                        '----------------------------------------------------
                                        SELECT CASE AS LONG CBCTL
                                        CASE %IDC_BUTNOPEN  ' "Open" button was clicked
                                            IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                                                sText = OpenFileProc(CBHNDL)
                                                IF LEN(sText) THEN
                                                    CONTROL SET TEXT CBHNDL, %IDC_TEXTBOX1, sText
                                                END IF
                                            END IF
                                  
                                        CASE %IDC_BUTNVIEW  ' "Preview" button was clicked
                                            IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                                                ' Grab TextBox contents, dialog title and show Print Preview
                                                CONTROL GET TEXT CBHNDL, %IDC_TEXTBOX1 TO sText
                                                DIALOG GET TEXT CBHNDL TO sTitle
                                                DlgPreview CBHNDL, sTitle, sText, pd
                                            END IF
                                  
                                        CASE %IDC_BUTNPAGE
                                            IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                                                PageSetupDlgProc CBHNDL, pd
                                            END IF
                                  
                                        CASE %IDC_BUTNFONT
                                            IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                                                IF SelectFontProc (CBHNDL, hNewFont, pd) THEN
                                                    ' Assing the new font to the textbox
                                                    CONTROL SET FONT CB.HNDL, %IDC_TEXTBOX1, hNewFont
                                                    ' Destory the previously used font
                                                    FONT END hFont
                                                    ' Set the new font as the current font
                                                    hFont = hNewFont
                                                END IF
                                            END IF
                                  
                                        CASE %IDCANCEL      ' "Exit" button or the Esc-key was pressed
                                            IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                                                DIALOG END CBHNDL, 0
                                            END IF
                                        END SELECT
                                  
                                  
                                    CASE %WM_SIZE  ' is sent on resize - auto-size the TextBox
                                        IF CBWPARAM <> %SIZE_MINIMIZED THEN  ' avoid minimized state
                                            w = LO(WORD, CBLPARAM)      ' dialog client area's width in pixels
                                            h = HI(WORD, CBLPARAM)      ' dialog client area's height in pixels
                                            DIALOG PIXELS CBHNDL, w, h TO UNITS w, h  ' convert to dialog units
                                            CONTROL GET LOC CBHNDL, %IDC_TEXTBOX1 TO x, y      ' we need top pos
                                            CONTROL SET SIZE CBHNDL, %IDC_TEXTBOX1, w, h - y   ' auto-size..
                                        END IF
                                  
                                    END SELECT
                                  
                                  END FUNCTION
                                  
                                  
                                  '====================================================================
                                  FUNCTION GetFile (BYVAL sFile AS STRING) AS STRING
                                  '--------------------------------------------------------------------
                                  ' Open and return given file's contents as a string
                                  '--------------------------------------------------------------------
                                    LOCAL ff   AS LONG
                                    LOCAL sBuf AS STRING
                                  
                                    IF LEN(DIR$(sFile)) = 0 THEN
                                        MSGBOX "The file does not exist.", %MB_TASKMODAL, "Error"
                                        EXIT FUNCTION
                                    END IF
                                  
                                    ff = FREEFILE
                                    OPEN sFile FOR BINARY AS ff LEN = 8192
                                        IF ERR THEN  ' Always trap ev. errors on file open actions
                                            MSGBOX ERROR$(ERR), %MB_TASKMODAL, "Error"
                                            RESET : ERRCLEAR : EXIT FUNCTION
                                        END IF
                                        GET$ ff, LOF(ff), sBuf
                                    CLOSE ff
                                  
                                    FUNCTION = sBuf
                                  
                                  END FUNCTION
                                  
                                  
                                  '====================================================================
                                  FUNCTION OpenFileProc (BYVAL hDlg AS DWORD) AS STRING
                                  '--------------------------------------------------------------------
                                  ' Open file dialog procedure
                                  '--------------------------------------------------------------------
                                    LOCAL  dwStyle AS DWORD
                                    LOCAL  Buf     AS STRING
                                    LOCAL  sFile   AS STRING
                                    STATIC sPath   AS STRING  ' Use a Static path variable
                                  
                                    IF LEN(sPath) = 0 THEN sPath = CURDIR$
                                    sFile = "*.BAS;*.INC;*.RC"
                                  
                                    Buf = CHR$("PB code files (*.BAS, *.INC, *.RC)", 0, "*.BAS;*.INC;*.RC", 0,  _
                                               "Text files (*.TXT)", 0, "*.TXT", 0, _
                                               "All files (*.*)", 0, "*.*", 0)
                                  
                                    dwStyle = %OFN_EXPLORER OR %OFN_FILEMUSTEXIST OR %OFN_HIDEREADONLY
                                  
                                    DISPLAY OPENFILE hDlg, , , "", sPath, buf, "", "BAS", dwStyle TO sFile
                                  
                                    IF LEN(sFile) THEN
                                        ' Store the selected file's path in static sPath for next time
                                        sPath = PATHNAME$(PATH, sFile)
                                        ' Set file name to dialog caption
                                        DIALOG SET TEXT hDlg, $PROGNAME + " - " + sFile
                                  
                                        ' Grab and return the file's contents as a text string
                                        FUNCTION = GetFile(sFile)
                                  
                                    END IF
                                  
                                  END FUNCTION
                                  
                                  
                                  '====================================================================
                                  FUNCTION PageSetupDlgProc (BYVAL hDlg AS DWORD, _
                                                             pd AS PreviewData) AS LONG
                                  '--------------------------------------------------------------------
                                  ' Use COMDLG32's Page setup dialog for margin settings
                                  '--------------------------------------------------------------------
                                    LOCAL psd AS PAGESETUPDLGA
                                    '------------------------------------------------------------------
                                    psd.lStructSize      = SIZEOF(psd)
                                    psd.hwndOwner        = hDlg
                                    psd.flags            = %PSD_MARGINS OR %PSD_DISABLEPAPER OR _
                                                           %PSD_DISABLEORIENTATION OR %PSD_DISABLEPRINTER
                                  
                                    '------------------------------------------------------------------
                                    ' The Preview dialog uses the more fine-grained Metric millimeters
                                    ' internally, so in US/Inch systems we need to convert the values.
                                    ' Note: The PageSetupDlg uses 1/1000 Inches (US) or 1/100 mm's (Metric) ..
                                    '------------------------------------------------------------------
                                    IF pd.locale = 1 THEN  ' Convert from mm. to Inches
                                        psd.rtMargin.nLeft   = pd.mL * 1000 / 25.4
                                        psd.rtMargin.nTop    = pd.mT * 1000 / 25.4
                                        psd.rtMargin.nRight  = pd.mR * 1000 / 25.4
                                        psd.rtMargin.nBottom = pd.mB * 1000 / 25.4
                                    ELSE
                                        psd.rtMargin.nLeft   = pd.mL * 100
                                        psd.rtMargin.nTop    = pd.mT * 100
                                        psd.rtMargin.nRight  = pd.mR * 100
                                        psd.rtMargin.nBottom = pd.mB * 100
                                    END IF
                                  
                                    '------------------------------------------------------------------
                                    ' Show the PageSetup dialog
                                    '------------------------------------------------------------------
                                    IF PageSetupDlg(psd) THEN
                                        IF pd.locale = 1 THEN  ' Convert from Inches to mm.
                                            pd.mL = psd.rtMargin.nLeft   / 1000 * 25.4
                                            pd.mT = psd.rtMargin.nTop    / 1000 * 25.4
                                            pd.mR = psd.rtMargin.nRight  / 1000 * 25.4
                                            pd.mB = psd.rtMargin.nBottom / 1000 * 25.4
                                        ELSE
                                            pd.mL = psd.rtMargin.nLeft   / 100
                                            pd.mT = psd.rtMargin.nTop    / 100
                                            pd.mR = psd.rtMargin.nRight  / 100
                                            pd.mB = psd.rtMargin.nBottom / 100
                                        END IF
                                        '----------------------------------------------------------
                                        FUNCTION = 1
                                    END IF
                                    IF psd.hDevMode  THEN GlobalFree psd.hDevMode
                                    IF psd.hDevNames THEN GlobalFree psd.hDevNames
                                  
                                  END FUNCTION
                                  
                                  
                                  '====================================================================
                                  FUNCTION SelectFontProc (BYVAL hDlg AS DWORD, hFont AS LONG, pd AS PreviewData) AS LONG
                                  
                                    LOCAL  newFontName  AS STRING
                                    LOCAL  newFontSize  AS LONG
                                    LOCAL  newFontStyle AS LONG
                                  
                                    DISPLAY FONT hDlg, , , pd.FontName, pd.FontSize, pd.FontType, %CF_BOTH OR %CF_FORCEFONTEXIST OR %CF_INITTOLOGFONTSTRUCT OR %CF_NOSCRIPTSEL _
                                        TO newFontName, newFontSize, newFontStyle
                                  
                                    IF LEN(newFontName) THEN
                                       pd.FontName = newFontName
                                       pd.FontSize = newFontSize
                                       pd.FontType = newFontStyle
                                  
                                      FONT NEW newFontName, newFontSize, newFontStyle TO hFont
                                      FUNCTION = %TRUE
                                    END IF
                                  
                                  END FUNCTION
                                  Attached Files
                                  Robert

                                  Comment


                                  • #18
                                    here is the TextView.rc
                                    Code:
                                    #include "resource.h"
                                    
                                    APPICON   ICON    TextView.ico  // Program icon
                                    ZOOM      CURSOR  ZOOM.CUR      // Preview cursor
                                    
                                    VS_VERSION_INFO VERSIONINFO
                                    FILEVERSION 1, 0, 0, 0
                                    PRODUCTVERSION 1, 0, 0, 0
                                    FILEOS VOS_WINDOWS32
                                    FILETYPE VFT_APP
                                    BEGIN
                                      BLOCK "StringFileInfo"
                                      BEGIN
                                        BLOCK "040904E4"
                                        BEGIN
                                          VALUE "CompanyName",      "CompanyName, Inc.\0"
                                          VALUE "FileDescription",  "Print Preview example\0"
                                          VALUE "FileVersion",      "1.00\0"
                                          VALUE "InternalName",     "TextView\0"
                                          VALUE "OriginalFilename", "TextView.exe\0"
                                          VALUE "LegalCopyright",   "Copyright 2005 CompanyName, Inc.\0"
                                          VALUE "LegalTrademarks",  "TextView is a trademark of CompanyName, Inc.\0"
                                          VALUE "ProductName",      "TextView\0"
                                          VALUE "ProductVersion",   "1.00\0"
                                          VALUE "Comments",         "TextView for Windows.\0"
                                        END
                                      END
                                    END
                                    
                                    // XP Theme enable the program already at compile time via xptheme.xml
                                    1 24 "xptheme.xml"
                                    and the xptheme.xml

                                    Code:
                                    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
                                    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
                                    <assemblyIdentity
                                        name="Microsoft.Windows.Shell.shell32"
                                        processorArchitecture="x86"
                                        version="5.1.0.0"
                                        type="win32"/>
                                    <description>Windows Shell</description>
                                    <dependency>
                                        <dependentAssembly>
                                            <assemblyIdentity
                                                type="win32"
                                                name="Microsoft.Windows.Common-Controls"
                                                version="6.0.0.0"
                                                processorArchitecture="x86"
                                                publicKeyToken="6595b64144ccf1df"
                                                language="*"
                                            />
                                        </dependentAssembly>
                                    </dependency>
                                    </assembly>
                                    the zoom.cur icon is the only one that is not text
                                    Robert

                                    Comment


                                    • #19
                                      deleted this edit , put on bottom of the following post
                                      Robert

                                      Comment


                                      • #20
                                        Code:
                                        '====================================================================
                                        '
                                        '  Preview.inc for PowerBASIC Compiler for Windows.
                                        '  Print Preview dialog include file.
                                        '  Copyright (c) 2005 - 2008 PowerBASIC, Inc.
                                        '  All Rights Reserved.
                                        '
                                        '--------------------------------------------------------------------
                                        '
                                        '  You have a royalty-free right to use, modify, reproduce and distribute
                                        '  this file (and/or any modified version) in any way you find useful,
                                        '  provided that you agree that both PowerBASIC and Microsoft have no
                                        '  warranty, obligation or liability for its contents.
                                        '
                                        '--------------------------------------------------------------------
                                        '
                                        '  This dialog provides text Printing and Print Preview features for
                                        '  any program by simply inserting #INCLUDE "PREVIEW.INC" among the
                                        '  declares for the main dialog and then call like:
                                        '
                                        '  DlgPreview hDlg, sTitle, sText, pd
                                        '
                                        '    hDlg   = Parent dialog's handle.
                                        '    sTitle = File path+name.
                                        '    sText  = Text to Print/Preview.
                                        '    pd     = PreviewData variable.
                                        '
                                        '  Note: The main program is responsible for maintaining a Static
                                        '        or Global PreviewData variable and call InitPreviewDlg.
                                        '        See TextView.bas for an example of how-to-use.
                                        '
                                        '--------------------------------------------------------------------
                                        '  Short desciption:
                                        '
                                        '  The Preview dialog uses two Graphic controls placed in a container
                                        '  control, which in is reality is a child dialog with %DS_CONTROL
                                        '  style. One Graphic control acts as a hidden, full-sized buffer in
                                        '  which all printing is done. The other Graphic control acts as view
                                        '  control with different sizes depending on zoom level. On resizing
                                        '  or page change, the view control is killed and re-created with the
                                        '  new size, while the buffer control is repainted and then stretched
                                        '  into the visible view control. The darkgray container window is
                                        '  resized to look as a full "client" area for the main Preview dialog,
                                        '  and it handles scrollbars plus mouse scrolling.
                                        '
                                        '--------------------------------------------------------------------
                                        '  Procedures:
                                        '  DlgPreview      - Main function where dialog and ViewPort is created.
                                        '  DlgPreviewProc  - Preview dialog callback procedure.
                                        '  InitPreviewBuf  - Setup and create a Graphic buffer control.
                                        '  InitPreviewDlg  - Get printer data and setup initial margins, etc.
                                        '  PrepareText     - Put text into an array and line wrap at given width.
                                        '  PrintToBuffer   - Prepare a page in the Graphic buffer control.
                                        '  PrintToPrinter  - Print the entire text after a Print dialog is shown.
                                        '  PrintToScreen   - Stretch the Graphic buffer to the screen.
                                        '  SetScrollPosHV  - Set the ViewPort scrolling on zoom and resize.
                                        '  SetScrollSize   - Set the ViewPort scrollbars on zoom and resize.
                                        '  ViewPortProc    - ViewPort parent dialog's callback procedure.
                                        '  vp_OnHScroll    - %WM_HSCROLL handler for the ViewPort.
                                        '  vp_OnMouseWheel - %WM_MOUSEWHEEL handler for the ViewPort.
                                        '  vp_OnVScroll    - %WM_VSCROLL handler for the ViewPort.
                                        '
                                        '====================================================================
                                        ' Declares
                                        '----------------------------------------------------------
                                        #IF NOT %DEF(%WINAPI)       ' if not already included
                                          %USEMACROS = 1            ' Use Macros for smallest size
                                          #INCLUDE "WIN32API.INC"   ' Win32 API declares
                                        #ENDIF
                                        
                                        '----------------------------------------------------------
                                        %IDC_GRAPHICBUFF  = %WM_USER + 101
                                        %IDC_GRAPHICVIEW  = %WM_USER + 102
                                        %IDC_LABEL1       = %WM_USER + 111
                                        %IDC_LABEL2       = %WM_USER + 112
                                        %IDC_BUTNPRINT    = %WM_USER + 121
                                        %IDC_BUTNFIRST    = %WM_USER + 122
                                        %IDC_BUTNPREV     = %WM_USER + 123
                                        %IDC_BUTNNEXT     = %WM_USER + 124
                                        %IDC_BUTNLAST     = %WM_USER + 125
                                        %IDC_BUTNZOOM     = %WM_USER + 126
                                        '------------------------------------------------
                                        TYPE PreviewData       ' SIZEOF = 240 bytes
                                          pw        AS SINGLE  ' Printer page width
                                          ph        AS SINGLE  ' Printer page height
                                          mL        AS SINGLE  ' Left   margin
                                          mT        AS SINGLE  ' Top    margin
                                          mR        AS SINGLE  ' Right  margin
                                          mB        AS SINGLE  ' Bottom margin
                                          lm        AS SINGLE  ' Left   non-printing region
                                          tm        AS SINGLE  ' Top    non-printing region
                                          rm        AS SINGLE  ' Right  non-printing region
                                          bm        AS SINGLE  ' Bottom non-printing region
                                          orient    AS LONG    ' Printer orientation
                                          pXppi     AS LONG    ' Printer resolution x
                                          pYppi     AS LONG    ' Printer resolution y
                                          '----------------------------------------------
                                          gw        AS SINGLE  ' Graphic view page width
                                          gh        AS SINGLE  ' Graphic view page height
                                          gx1       AS SINGLE  ' Graphic view Left   margin
                                          gy1       AS SINGLE  ' Graphic view Top    margin
                                          gx2       AS SINGLE  ' Graphic view Right  margin
                                          gy2       AS SINGLE  ' Graphic view Bottom margin
                                          '----------------------------------------------
                                          gpw       AS SINGLE  ' Graphic buffer page width
                                          gph       AS SINGLE  ' Graphic buffer page height
                                          gcw       AS SINGLE  ' Graphic buffer client width
                                          gch       AS SINGLE  ' Graphic buffer client height
                                          gmL       AS SINGLE  ' Graphic buffer Left   margin
                                          gmT       AS SINGLE  ' Graphic buffer Top    margin
                                          gmR       AS SINGLE  ' Graphic buffer Right  margin
                                          gmB       AS SINGLE  ' Graphic buffer Bottom margin
                                          '----------------------------------------------
                                          gXppi     AS LONG    ' Graphic resolution x
                                          gYppi     AS LONG    ' Graphic resolution y
                                          locale    AS LONG    ' Locale measurement, US/Metric - 1/0
                                          lpp       AS LONG    ' Lines per page
                                          PageCount AS LONG    ' Total page count
                                          zoom      AS LONG    ' zoom factor, 0 - 100
                                          zoomX     AS SINGLE  ' zoom factor x
                                          zoomY     AS SINGLE  ' zoom factor y
                                          '----------------------------------------------
                                          clrWindow     AS LONG   ' System window color
                                          clrWindowText AS LONG   ' System window text color
                                          clrShadow     AS LONG   ' System shadow color
                                          clrDkShadow   AS LONG   ' System dark shadow color
                                          clrBorder     AS LONG   ' Page border color (def blue)
                                          FontSize      AS LONG   ' Font size
                                          FontType      AS LONG   ' Font type
                                          FontName AS ASCIIZ * 64 ' Font name
                                          pt       AS POINTAPI    ' Cursor pos on zoom click
                                        END TYPE
                                        '--------------------------------------------------------------------
                                        DECLARE SUB InitPreviewBuf (BYVAL hDlg AS DWORD)
                                        DECLARE SUB InitPreviewDlg (pd AS PreviewData)
                                        DECLARE SUB PrepareText    (BYVAL hDlg AS DWORD, BYVAL w AS SINGLE, _
                                                                    BYVAL sText AS STRING, sArray() AS STRING)
                                        DECLARE SUB PrintToBuffer  (BYVAL hDlg AS DWORD, BYVAL PageNum AS LONG, sArray() AS STRING)
                                        DECLARE SUB PrintToPrinter (BYVAL hDlg AS DWORD, sArray() AS STRING)
                                        DECLARE SUB PrintToScreen  (BYVAL hDlg AS DWORD, sArray() AS STRING)
                                        DECLARE SUB SetScrollPosHV (BYVAL hDlg AS DWORD)
                                        DECLARE SUB SetScrollSize  (BYVAL hDlg AS DWORD, BYVAL w AS LONG, BYVAL h AS LONG)
                                        '--------------------------------------------------------------------
                                        DECLARE FUNCTION vp_OnHScroll     (BYVAL hWnd AS DWORD, _
                                                                           BYVAL wParam AS DWORD, _
                                                                           BYVAL lParam AS LONG) AS LONG
                                        DECLARE FUNCTION vp_OnVScroll     (BYVAL hWnd AS DWORD, _
                                                                           BYVAL wParam AS DWORD, _
                                                                           BYVAL lParam AS LONG) AS LONG
                                        DECLARE FUNCTION vp_OnMouseWheel  (BYVAL hWnd AS DWORD, _
                                                                           BYVAL wParam AS DWORD, _
                                                                           BYVAL lParam AS LONG) AS LONG
                                        
                                        
                                        '====================================================================
                                        FUNCTION DlgPreview (BYVAL hDlgMain AS DWORD, _      ' Parent dialog handle
                                                             BYVAL sTitle AS STRING, _       ' File path+name
                                                             BYVAL sText AS STRING, _        ' Text to print/preview
                                                             pd AS PreviewData) AS LONG      ' Printer and Preview data
                                        '--------------------------------------------------------------------
                                        ' Program entry point
                                        '--------------------------------------------------------------------
                                          LOCAL hDlg, hClient AS DWORD, h, w, x, y AS LONG
                                        
                                          '------------------------------------------------------------------
                                          ' Let initial size be 90% of the desktop client size.
                                          '------------------------------------------------------------------
                                          DESKTOP GET CLIENT TO w, h
                                          w = w * 0.9
                                          h = h * 0.9
                                        
                                          '------------------------------------------------------------------
                                          ' Use a PIXELS based dialog for more exact measurements
                                          '------------------------------------------------------------------
                                          DIALOG NEW PIXELS, hDlgMain, sTitle,,, w, h, _
                                                             %WS_OVERLAPPEDWINDOW OR %WS_CLIPCHILDREN, 0 TO hDlg
                                        
                                          '------------------------------------------------------------------
                                          ' Use a Label as "ToolBar"
                                          '------------------------------------------------------------------
                                          DIALOG UNITS hDlg, 44, 16 TO PIXELS w, h
                                          CONTROL ADD LABEL, hDlg, %IDC_LABEL1, "", 0, 0, 640, h,, %WS_EX_DLGMODALFRAME
                                        
                                        
                                          '------------------------------------------------------------------
                                          ' ViewPort, a dialog with %DS_CONTROL style acting as Graphic parent.
                                          '------------------------------------------------------------------
                                          DIALOG NEW PIXELS, hDlg, "", 0, h, 50, 50, %DS_CONTROL OR _ ' Scroll window
                                                             %WS_CHILD OR %WS_CLIPCHILDREN, 0 TO hClient
                                          DIALOG SET COLOR hClient, -1, GetSysColor(%COLOR_3DSHADOW)
                                          '------------------------------------------------------------------
                                          CONTROL ADD GRAPHIC, hClient, %IDC_GRAPHICVIEW, "", 0, 0, 50, 50, %SS_NOTIFY
                                          GRAPHIC ATTACH hClient, %IDC_GRAPHICVIEW, REDRAW
                                        
                                        
                                          '------------------------------------------------------------------
                                          ' Add buttons and a label to the "ToolBar"
                                          '------------------------------------------------------------------
                                          DIALOG UNITS hDlg, 44, 12 TO PIXELS w, h  ' Button size
                                          DIALOG UNITS hDlg, 2,   2 TO PIXELS x, y  ' Space between..
                                        
                                          CONTROL ADD BUTTON, hDlg, %IDC_BUTNPRINT, "&Print", x, 3, w, h
                                          x = x + w + 4*y
                                          CONTROL ADD BUTTON, hDlg, %IDC_BUTNFIRST, "<<",  x, 3, w/2, h
                                          x = x + w/2
                                          CONTROL ADD BUTTON, hDlg, %IDC_BUTNPREV,  "<",   x, 3, w/2, h
                                          x = x + w/2
                                          CONTROL ADD BUTTON, hDlg, %IDC_BUTNNEXT,  ">",   x, 3, w/2, h
                                          x = x + w/2
                                          CONTROL ADD BUTTON, hDlg, %IDC_BUTNLAST,  ">>",  x, 3, w/2, h
                                          x = x + w/2 + y
                                          '------------------------------------------------------------------
                                          CONTROL ADD LABEL, hDlg, %IDC_LABEL2, "", x, 7, w, h - 2, %SS_CENTER
                                          x = x + w + y
                                          CONTROL ADD BUTTON, hDlg, %IDC_BUTNZOOM, "&Zoom", x, 3, w, h
                                          x = x + w + 4*y
                                          CONTROL ADD BUTTON, hDlg, %IDCANCEL, "&Close", x, 3, w, h
                                        
                                          '------------------------------------------------------------------
                                          CONTROL DISABLE hDlg, %IDC_BUTNFIRST
                                          CONTROL DISABLE hDlg, %IDC_BUTNPREV
                                          CONTROL DISABLE hDlg, %IDC_BUTNNEXT
                                          CONTROL DISABLE hDlg, %IDC_BUTNLAST
                                        
                                          '------------------------------------------------------------------
                                          ' Store some important handles and pointer to given text with USER
                                          '------------------------------------------------------------------
                                          IF LEN(sText) = 0 THEN sText = " "
                                          DIALOG SET USER hDlg, 1, hClient
                                          DIALOG SET USER hDlg, 2, VARPTR(pd)
                                          DIALOG SET USER hDlg, 3, STRPTR(sText)
                                          DIALOG SET USER hDlg, 4, LEN(sText)
                                          DIALOG SET USER hClient, 1, hDlg
                                          DIALOG SET USER hClient, 2, VARPTR(pd)
                                        
                                          '------------------------------------------------------------------
                                          DIALOG SHOW MODELESS hClient, CALL ViewPortProc
                                          DIALOG SHOW MODAL hDlg CALL DlgPreviewProc
                                        
                                        END FUNCTION
                                        
                                        
                                        '====================================================================
                                        CALLBACK FUNCTION DlgPreviewProc() AS LONG
                                        '--------------------------------------------------------------------
                                        ' Main dialog callback procedure
                                        '--------------------------------------------------------------------
                                          LOCAL w, x, y AS LONG, hClient, pStr AS DWORD
                                          LOCAL zLocale AS ASCIIZ * 4
                                          LOCAL pd AS PreviewData PTR
                                          '----------------------------------------------
                                          STATIC sArray() AS STRING
                                          STATIC hCurZoom AS DWORD
                                          STATIC noFlag, PageNum AS LONG
                                          STATIC sText AS STRING
                                          '----------------------------------------------
                                          DIALOG GET USER CB.HNDL, 1 TO hClient
                                          DIALOG GET USER CB.HNDL, 2 TO pd
                                          '----------------------------------------------
                                        
                                          SELECT CASE CB.MSG
                                          CASE %WM_INITDIALOG  ' <- Received right before the dialog is shown
                                              '--------------------------------------------------------------
                                              hCurZoom  = LoadCursor(GetModuleHandle(""), "ZOOM")
                                              '--------------------------------------------------------------
                                              DIALOG GET USER CB.HNDL, 3 TO pStr
                                              DIALOG GET USER CB.HNDL, 4 TO w
                                              IF w THEN sText = PEEK$(pStr, w)
                                              '--------------------------------------------------------------
                                        
                                              GRAPHIC FONT @pd.FontName, @pd.FontSize, @pd.FontType
                                              GRAPHIC GET PPI TO @pd.gXppi, @pd.gYppi  ' Graphic resolution in pixels/inch
                                        
                                              '--------------------------------------------------------------
                                              ' Initialize and show first page
                                              '--------------------------------------------------------------
                                              InitPreviewBuf hClient
                                        
                                              '--------------------------------------------------------------
                                              ' Calculate lines-per-page
                                              '--------------------------------------------------------------
                                              GRAPHIC CHR SIZE TO x, y   ' Line height
                                              @pd.lpp = @pd.gch \ y      ' lines-per-page
                                        
                                              '--------------------------------------------------------------
                                              ' Setup and show Page 1
                                              '--------------------------------------------------------------
                                              IF LEN(sText) THEN
                                                  PageNum = 1
                                                  PrepareText   hClient, @pd.gcw, sText, sArray()
                                                  PrintToBuffer hClient, PageNum, sArray()
                                                  PrintToScreen hClient, sArray()
                                        
                                                  IF @pd.PageCount > 1 THEN
                                                      CONTROL ENABLE CB.HNDL, %IDC_BUTNNEXT
                                                      CONTROL ENABLE CB.HNDL, %IDC_BUTNLAST
                                                  END IF
                                              END IF
                                              '--------------------------------------------------------------
                                              CONTROL SET FOCUS CB.HNDL, %IDCANCEL
                                              FUNCTION = 1
                                        
                                        
                                          CASE %WM_SIZE
                                              '--------------------------------------------------------------
                                              ' Auto-size the ToolBar label and the Graphic ViewPort parent.
                                              '--------------------------------------------------------------
                                              IF CB.WPARAM <> %SIZE_MINIMIZED THEN
                                                  CONTROL GET SIZE CB.HNDL, %IDC_LABEL1 TO x, y
                                                  CONTROL SET SIZE CB.HNDL, %IDC_LABEL1, LO(WORD, CB.LPARAM) + 2, y
                                                  DIALOG SET SIZE hClient, LO(WORD, CB.LPARAM), HI(WORD, CB.LPARAM) - y
                                                  PrintToScreen hClient, sArray()
                                              END IF
                                        
                                        
                                          CASE %WM_GETMINMAXINFO
                                              '--------------------------------------------------------------
                                              ' Prevent too small dialog size
                                              '--------------------------------------------------------------
                                              LOCAL mmi AS MINMAXINFO PTR
                                              mmi = CB.LPARAM
                                              @mmi.ptMinTrackSize.x = 200
                                              @mmi.ptMinTrackSize.y = 200
                                              FUNCTION = 1
                                        
                                        
                                          CASE %WM_COMMAND         ' <- a control is calling
                                              SELECT CASE CB.CTL    ' <- look at control's id
                                              CASE %IDC_BUTNPRINT  ' "Print" button was clicked
                                                  IF CB.CTLMSG = %BN_CLICKED THEN
                                                      PrintToPrinter CB.HNDL, sArray()
                                                  END IF
                                        
                                              CASE %IDC_BUTNFIRST  ' "<<" button was clicked
                                                  IF CB.CTLMSG = %BN_CLICKED THEN
                                                      PageNum = 1
                                                      CONTROL DISABLE CB.HNDL, %IDC_BUTNFIRST
                                                      CONTROL DISABLE CB.HNDL, %IDC_BUTNPREV
                                                      IF @pd.PageCount > 1 THEN
                                                          CONTROL ENABLE CB.HNDL, %IDC_BUTNNEXT
                                                          CONTROL ENABLE CB.HNDL, %IDC_BUTNLAST
                                                      END IF
                                                      CONTROL SET FOCUS CB.HNDL, %IDC_BUTNNEXT
                                                      PrintToBuffer hClient, PageNum, sArray()
                                                      PrintToScreen hClient, sArray()
                                                  END IF
                                        
                                              CASE %IDC_BUTNPREV  ' "<" button was clicked
                                                  IF CB.CTLMSG = %BN_CLICKED THEN
                                                      PageNum = MAX&(1, PageNum - 1)
                                                      CONTROL ENABLE CB.HNDL, %IDC_BUTNNEXT
                                                      CONTROL ENABLE CB.HNDL, %IDC_BUTNLAST
                                                      IF PageNum = 1 THEN
                                                          CONTROL DISABLE CB.HNDL, %IDC_BUTNFIRST
                                                          CONTROL DISABLE CB.HNDL, %IDC_BUTNPREV
                                                          CONTROL SET FOCUS CB.HNDL, %IDC_BUTNNEXT
                                                      END IF
                                                      PrintToBuffer hClient, PageNum, sArray()
                                                      PrintToScreen hClient, sArray()
                                                  END IF
                                        
                                              CASE %IDC_BUTNNEXT  ' ">" button was clicked
                                                  IF CB.CTLMSG = %BN_CLICKED THEN
                                                      PageNum = MIN&(MAX&(1, @pd.PageCount), PageNum + 1)
                                                      CONTROL ENABLE CB.HNDL, %IDC_BUTNFIRST
                                                      CONTROL ENABLE CB.HNDL, %IDC_BUTNPREV
                                                      IF PageNum = @pd.PageCount THEN
                                                          CONTROL DISABLE CB.HNDL, %IDC_BUTNNEXT
                                                          CONTROL DISABLE CB.HNDL, %IDC_BUTNLAST
                                                          CONTROL SET FOCUS CB.HNDL, %IDC_BUTNPREV
                                                      END IF
                                                      PrintToBuffer hClient, PageNum, sArray()
                                                      PrintToScreen hClient, sArray()
                                                  END IF
                                        
                                              CASE %IDC_BUTNLAST  ' ">>" button was clicked
                                                  IF CB.CTLMSG = %BN_CLICKED THEN
                                                      PageNum = MAX&(1, @pd.PageCount)
                                                      CONTROL ENABLE CB.HNDL, %IDC_BUTNFIRST
                                                      CONTROL ENABLE CB.HNDL, %IDC_BUTNPREV
                                                      IF PageNum = @pd.PageCount THEN
                                                          CONTROL DISABLE CB.HNDL, %IDC_BUTNNEXT
                                                          CONTROL DISABLE CB.HNDL, %IDC_BUTNLAST
                                                      END IF
                                                      CONTROL SET FOCUS CB.HNDL, %IDC_BUTNPREV
                                                      PrintToBuffer hClient, PageNum, sArray()
                                                      PrintToScreen hClient, sArray()
                                                  END IF
                                        
                                              CASE %IDC_BUTNZOOM
                                                  IF CB.CTLMSG = %BN_CLICKED THEN
                                                      SELECT CASE @pd.zoom
                                                      CASE 0   : @pd.zoom = 75
                                                      CASE 75  : @pd.zoom = 100
                                                      CASE 100 : @pd.zoom = 0
                                                      END SELECT
                                                      '--------------------------------------------------
                                                      PrintToScreen hClient, sArray()
                                                  END IF
                                        
                                              CASE %IDCANCEL      ' "Cancel" button or the Esc-key was pressed
                                                  IF CB.CTLMSG = %BN_CLICKED THEN ' close dialog
                                                      DIALOG END CB.HNDL
                                                  END IF
                                        
                                              END SELECT
                                        
                                        
                                          CASE %WM_SETCURSOR
                                              '------------------------------------------------------------------------
                                              ' When the mouse is over the Graphic control, over-ride default arrow
                                              ' cursor and set a magnify glass cursor instead. Note - in dialogs,
                                              ' we must return %TRUE to inform dialog engine we have taken charge.
                                              '------------------------------------------------------------------------
                                              IF GetDlgCtrlID(CB.WPARAM) = %IDC_GRAPHICVIEW THEN
                                        
                                                  SELECT CASE AS LONG HI(WORD, CB.LPARAM)
                                                  CASE %WM_LBUTTONDOWN                ' Zoom on clicks
                                                      IF noFlag THEN EXIT FUNCTION    ' Avoid multiple clicks
                                                      noFlag = 1
                                                      GetCursorPos @pd.pt
                                                      ScreenToClient hClient, @pd.pt  ' Store click-point
                                                      SELECT CASE @pd.zoom            ' Toggle zoom value
                                                      CASE 0   : @pd.zoom = 75
                                                      CASE 75  : @pd.zoom = 100
                                                      CASE 100 : @pd.zoom = 0
                                                      END SELECT
                                                      '--------------------------------------------------
                                                      PrintToScreen hClient, sArray()
                                                      noFlag = 0
                                        
                                                  CASE %WM_MOUSEMOVE  ' Ensure zoom cursor is used
                                                      IF GetCursor <> hCurZoom THEN SetCursor hCurZoom
                                                  END SELECT
                                        
                                                  FUNCTION = 1
                                                  EXIT FUNCTION
                                              END IF
                                        
                                        
                                          CASE %WM_MOUSEWHEEL  ' Pass on mouse scroll messages
                                              FUNCTION = vp_OnMouseWheel (hClient, CB.WPARAM, CB.LPARAM)
                                        
                                          END SELECT
                                        END FUNCTION
                                        
                                        
                                        '====================================================================
                                        SUB InitPreviewBuf (BYVAL hClient AS DWORD)
                                        '--------------------------------------------------------------------
                                        ' Setup and create a Graphic buffer control
                                        '--------------------------------------------------------------------
                                          LOCAL pd AS PreviewData PTR
                                          '------------------------------------------------------------------
                                          DIALOG GET USER hClient, 2 TO pd    ' Get preview data
                                          IF pd = 0 THEN EXIT SUB             ' Ensure we got something
                                        
                                          '--------------------------------------------------------------
                                          @pd.zoom = 0                                        ' Auto-sized to dialog height
                                          '--------------------------------------------------------------
                                          @pd.clrWindow     = GetSysColor(%COLOR_WINDOW)      ' System window color
                                          @pd.clrWindowText = GetSysColor(%COLOR_WINDOWTEXT)  ' System window text color
                                          @pd.clrShadow     = GetSysColor(%COLOR_3DSHADOW)    ' System shadow color
                                          @pd.clrDkShadow   = GetSysColor(%COLOR_3DDKSHADOW)  ' System dark shadow color
                                          @pd.clrBorder     = RGB(49,105,198)                 ' Blue-ish border
                                        
                                          '--------------------------------------------------------------
                                          ' if no printer resolution - set own page size and orientation
                                          '--------------------------------------------------------------
                                          IF @pd.pXppi = 0 THEN
                                              @pd.pXppi = @pd.gXppi                   ' Use Graphic resolution in pixels/inch
                                              @pd.pYppi = @pd.gYppi
                                              IF @pd.locale = 1 THEN                  ' US/Inches
                                                  @pd.pw = 8.5 * @pd.pXppi             ' "Letter" page width in Graphic resolution
                                                  @pd.ph = 11  * @pd.pYppi             ' "Letter" page height in Graphic resolution
                                              ELSE                                   ' Metric/millimeter
                                                  @pd.pw = 21   / 2.54 * @pd.pXppi     ' "A4" page width in Graphic resolution
                                                  @pd.ph = 29.7 / 2.54 * @pd.pYppi     ' "A4" page height in Graphic resolution
                                              END IF
                                              @pd.orient = 1          ' Set Portrait orientation mode
                                          END IF
                                        
                                          '--------------------------------------------------------------
                                          ' Calculate Graphic buffer size and margins
                                          '--------------------------------------------------------------
                                          @pd.gpw = @pd.pw * (@pd.gXppi / @pd.pXppi)  ' Graphic 100% page width
                                          @pd.gph = @pd.ph * (@pd.gYppi / @pd.pYppi)  ' Graphic 100% page height
                                          @pd.gmL = @pd.mL * (@pd.gXppi / 25.40)      ' Graphic 100% left margin
                                          @pd.gmT = @pd.mT * (@pd.gYppi / 25.40)      ' Graphic 100% top margin
                                          @pd.gmR = @pd.mR * (@pd.gXppi / 25.40)      ' Graphic 100% right margin
                                          @pd.gmB = @pd.mB * (@pd.gYppi / 25.40)      ' Graphic 100% bottom margin
                                          @pd.gcw = @pd.gpw - @pd.gmL - @pd.gmR + 2   ' Graphic 100% client width
                                          @pd.gch = @pd.gph - @pd.gmT - @pd.gmB + 2   ' Graphic 100% client height
                                        
                                          '--------------------------------------------------------------
                                          ' Create and setup Graphic buffer control
                                          '--------------------------------------------------------------
                                          CONTROL ADD GRAPHIC, hClient, %IDC_GRAPHICBUFF, "", 0, 0, @pd.gpw, @pd.gph
                                          CONTROL SHOW STATE hClient, %IDC_GRAPHICBUFF, %SW_HIDE
                                          GRAPHIC ATTACH hClient, %IDC_GRAPHICBUFF
                                          GRAPHIC COLOR @pd.clrWindowText, @pd.clrWindow
                                          GRAPHIC FONT @pd.FontName, @pd.FontSize, @pd.FontType
                                          GRAPHIC CLEAR
                                        
                                        END SUB
                                        
                                        
                                        '====================================================================
                                        SUB InitPreviewDlg (pd AS PreviewData)
                                        '--------------------------------------------------------------------
                                        ' Grab Printer page size and resolution, etc, plus
                                        ' setup initial margins, font, zoom and colors. To be
                                        ' called from Main program before the Preview is shown.
                                        '--------------------------------------------------------------------
                                          LOCAL zLocale AS ASCIIZ * 4
                                        
                                          '----------------------------------------------------------------
                                          ERRCLEAR
                                          XPRINT ATTACH DEFAULT                     ' Attach the default host printer
                                          IF ERR = 0 AND LEN(XPRINT$) THEN          ' On success
                                              XPRINT GET SIZE TO pd.pw, pd.ph       ' Page size in pixels
                                              XPRINT GET MARGIN TO pd.lm, pd.tm, pd.rm, pd.bm  ' Printer margins (non-printable area)
                                              XPRINT GET ORIENTATION TO pd.orient   ' Printer orientation (1/2 - portrait/landscape)
                                              XPRINT GET PPI TO pd.pXppi, pd.pYppi  ' Get printer resolution in pixels/inch
                                              XPRINT CLOSE                          ' Detach the printer
                                          END IF
                                        
                                          '------------------------------------------------------------------
                                          ' Retrieve the locale measurement - Metric millimeters or inches.
                                          ' Returns 0 = Metrics or 1 = US (Inches)
                                          '--------------------------------------------------------------
                                          GetLocaleInfo %LOCALE_USER_DEFAULT, %LOCALE_IMEASURE, zLocale, SIZEOF(zLocale)
                                          pd.locale = VAL(zLocale)
                                          '--------------------------------------------------------------
                                          IF pd.locale = 1 THEN  ' US/Inches
                                              pd.mL   = 20.32    ' 20.32 mm margins = 0.8 inches
                                              pd.mT   = 20.32
                                              pd.mR   = 20.32
                                              pd.mB   = 20.32
                                          ELSE                   ' Metric/millimeters
                                              pd.mL   = 20       ' 20 mm margins
                                              pd.mT   = 20
                                              pd.mR   = 20
                                              pd.mB   = 20
                                          END IF
                                          '--------------------------------------------------------------
                                          pd.FontName = "Courier New"  ' Initial font name and size
                                          pd.FontSize = 10
                                          pd.FontType = 0
                                          '--------------------------------------------------------------
                                        
                                        END SUB
                                        
                                        
                                        '====================================================================
                                        SUB PrepareText (BYVAL hClient AS DWORD, BYVAL w AS SINGLE, _
                                                         BYVAL sText AS STRING, sArray() AS STRING)
                                        '--------------------------------------------------------------------
                                        ' Prepare given text for the preview and wrap too long lines
                                        '--------------------------------------------------------------------
                                          LOCAL c, lCount, p, tw, th AS LONG
                                          LOCAL pd AS PreviewData PTR
                                        
                                          '------------------------------------------------------------------
                                          DIALOG GET USER hClient, 2 TO pd    ' Get preview data
                                          IF pd = 0 THEN EXIT SUB             ' Ensure we got something
                                        
                                          '--------------------------------------------
                                          ' Parse given text line-by-line into an array
                                          '--------------------------------------------
                                          IF LEN(sText) THEN
                                              IF RIGHT$(sText, 2) <> $CRLF THEN sText = sText + $CRLF
                                              lCount = TALLY(sText, $CRLF)
                                              IF lCount > 0 THEN
                                                  REDIM sArray(1 TO lCount) AS STRING
                                                  PARSE sText, sArray(), $CRLF
                                              END IF
                                          ELSEIF UBOUND(sArray) < 0 THEN
                                              EXIT SUB
                                          END IF
                                        
                                          '--------------------------------------------
                                          ' Ensure the Graphic buffer is attached
                                          '--------------------------------------------
                                          GRAPHIC ATTACH hClient, %IDC_GRAPHICBUFF
                                          GRAPHIC FONT @pd.FontName, @pd.FontSize, @pd.FontType
                                        
                                          '--------------------------------------------------------------
                                          ' Loop through all array items (lines) and compare their width
                                          ' with given page client width - if too long, wrap to next line.
                                          '--------------------------------------------------------------
                                          c = 1
                                          DO
                                              GRAPHIC TEXT SIZE sArray(c) TO tw, th  ' Line width
                                              IF tw > w THEN                         ' If too wide
                                                  p = 1
                                                  DO                                 ' Scan the line backwards
                                                      p = INSTR(-p, sArray(c), " ")  ' for a space to wrap at
                                                      IF p THEN
                                                          GRAPHIC TEXT SIZE LEFT$(sArray(c), p) TO tw, th
                                                          IF tw <= w THEN
                                                              REDIM PRESERVE sArray(1 TO UBOUND(sArray)+1)
                                                              ARRAY INSERT sArray(c), LEFT$(sArray(c), p)
                                                              sArray(c+1) = MID$(sArray(c+1), p+1)
                                                              DECR c
                                                              EXIT DO
                                                          ELSE
                                                              p = LEN(sArray(c)) - p + 2
                                                          END IF
                                                      ELSE
                                                          FOR p = LEN(sArray(c)) - 1 TO 1 STEP -1
                                                              GRAPHIC TEXT SIZE LEFT$(sArray(c), p) TO tw, th
                                                              IF tw <= w THEN
                                                                  sArray(c) = LEFT$(sArray(c), p)
                                                                  EXIT FOR
                                                              END IF
                                                          NEXT
                                                          EXIT DO
                                                      END IF
                                                  LOOP
                                              END IF
                                              INCR c
                                              IF c > UBOUND(sArray) THEN EXIT DO
                                           LOOP
                                           @pd.PageCount = UBOUND(sArray) \ @pd.lpp + 1        ' and total PageCount
                                        
                                        END SUB
                                        
                                        
                                        '====================================================================
                                        SUB PrintToBuffer (BYVAL hClient AS DWORD, _
                                                           BYVAL PageNum AS LONG, _
                                                           sArray() AS STRING)
                                        '--------------------------------------------------------------------
                                        ' Print sArray() to the Graphic Buffer control
                                        '--------------------------------------------------------------------
                                          LOCAL hDlgMain AS DWORD
                                          LOCAL c, x, y, StartLine, EndLine AS LONG
                                          LOCAL sHeader, sTitle AS STRING
                                          LOCAL pd AS PreviewData PTR
                                        
                                          '--------------------------------------------
                                          ' Get preview data, setup font and colors
                                          '--------------------------------------------
                                          DIALOG GET USER hClient, 1 TO hDlgMain
                                          DIALOG GET USER hClient, 2 TO pd
                                          IF pd = 0 THEN EXIT SUB
                                        
                                          '--------------------------------------------
                                          sHeader = "Page" + STR$(PageNum) + "/" + FORMAT$(@pd.PageCount)
                                          CONTROL SET TEXT hDlgMain, %IDC_LABEL2, sHeader
                                        
                                          '--------------------------------------------
                                          ' Attach the Graphic control
                                          '--------------------------------------------
                                          GRAPHIC ATTACH hClient, %IDC_GRAPHICBUFF
                                          GRAPHIC COLOR @pd.clrWindowText, @pd.clrWindow
                                          GRAPHIC CLEAR
                                        
                                          '--------------------------------------------
                                          ' Print a centered header
                                          '--------------------------------------------
                                          DIALOG GET TEXT hDlgMain TO sTitle
                                          c = INSTR(-1, sTitle, ANY "\/")
                                          IF c THEN
                                              sTitle = TRIM$(MID$(sTitle, c+1))
                                          ELSE
                                              sTitle = "Untitled"
                                          END IF
                                          sTitle = sTitle + "  |  " + sHeader + "  |  " + DATE$ + ", " + TIME$
                                          GRAPHIC COLOR @pd.clrDkShadow, @pd.clrWindow
                                          GRAPHIC TEXT SIZE sTitle TO x, y
                                          GRAPHIC SET POS ((@pd.gpw - x) / 2, (@pd.gmT - y) / 2)
                                          GRAPHIC PRINT sTitle
                                        
                                          '--------------------------------------------
                                          ' Print the text
                                          '--------------------------------------------
                                          StartLine = 1 + ((PageNum - 1) * @pd.lpp)
                                          StartLine = MIN&(StartLine, UBOUND(sArray))
                                          EndLine   = StartLine + @pd.lpp - 1
                                          EndLine   = MIN&(EndLine, UBOUND(sArray))
                                        
                                          GRAPHIC COLOR -1, -2
                                          GRAPHIC SET POS (@pd.gmL, @pd.gmT)  ' starting point
                                          FOR c = StartLine TO EndLine
                                              GRAPHIC GET POS TO x, y
                                              GRAPHIC SET POS (@pd.gmL, y)
                                              GRAPHIC PRINT sArray(c)
                                          NEXT
                                        
                                        END SUB
                                        
                                        
                                        '====================================================================
                                        SUB PrintToPrinter (BYVAL hDlg AS DWORD, sArray() AS STRING)
                                        '--------------------------------------------------------------------
                                        ' Print the entire text array after a Print dialog is shown.
                                        '--------------------------------------------------------------------
                                          LOCAL c, x, y, x1, y1, x2, y2, w1, h1, pgNum AS LONG
                                          LOCAL sHeader  AS STRING, sTitle AS STRING
                                          LOCAL hFont AS LONG
                                        
                                          LOCAL pd AS PreviewData PTR
                                          '------------------------------------------------------------------
                                          DIALOG GET USER hDlg, 2 TO pd       ' Get preview data
                                          IF pd = 0 THEN EXIT SUB             ' Ensure we got something
                                        
                                          '----------------------------------------------------------------------------
                                          ' Document title for the header
                                          '----------------------------------------------------------------------------
                                          DIALOG GET TEXT hDlg TO sTitle
                                          c = INSTR(-1, sTitle, ANY "\/")
                                          IF c THEN
                                              sTitle = TRIM$(MID$(sTitle, c+1))
                                          ELSE
                                              sTitle = "Untitled"
                                          END IF
                                        
                                          '----------------------------------------------------------------------------
                                          ' Print the entire, line wrapped array
                                          '----------------------------------------------------------------------------
                                          ERRCLEAR
                                          CONTROL DISABLE hDlg, %IDC_BUTNPRINT
                                          XPRINT ATTACH CHOOSE, TRIM$(sTitle)     ' Show the Print dialog (COMDLG32)
                                          CONTROL ENABLE hDlg, %IDC_BUTNPRINT
                                        
                                          IF ERR = 0 AND LEN(XPRINT$) THEN        ' On success
                                              x1 = (@pd.mL) * (@pd.pXppi / 25.40) - @pd.lm  ' user-defined left margin
                                              y1 = (@pd.mT) * (@pd.pYppi / 25.40) - @pd.tm  ' user-defined top margin
                                        
                                              FONT NEW @pd.FontName, @pd.FontSize, @pd.FontType TO hFont
                                              XPRINT SET FONT hFont
                                        
                                              GOSUB PrintPageHeader             ' Begin by printing a header
                                        
                                              '------------------------------------------------------------------------
                                              FOR c = 1 TO UBOUND(sArray)
                                                  XPRINT GET POS TO x, y        ' Check position
                                                  IF c MOD (@pd.lpp+1) = 0 THEN ' When all lines-per-page has been printed,
                                                      XPRINT FORMFEED           ' eject the paper and start a new page.
                                                      GOSUB PrintPageHeader     ' Print header part
                                                  ELSE
                                                      XPRINT SET POS (x1, y)    ' else simply set position
                                                  END IF
                                                  XPRINT sArray(c)              ' and print line
                                              NEXT
                                              '------------------------------------------------------------------------
                                        
                                              XPRINT CLOSE                      ' End printjob and detach the printer
                                        
                                              FONT END hFont
                                          END IF
                                        
                                        EXIT SUB
                                        
                                        '--------------------------------------------------------------------
                                        PrintPageHeader:
                                          INCR pgNum      ' print a centered header at top of the page
                                          sHeader = sTitle  + "  |  Page" + STR$(pgNum) + "/" + FORMAT$(@pd.PageCount)
                                          sHeader = sHeader + "  |  " + DATE$ + ", " + TIME$
                                          XPRINT TEXT SIZE sHeader TO w1, h1
                                          XPRINT SET POS ((@pd.pw - w1) / 2, (y1 - h1) / 2)
                                          XPRINT sHeader
                                          y = y1
                                          XPRINT SET POS (x1, y) ' set position for actual text
                                        RETURN
                                        
                                        END SUB
                                        
                                        
                                        '====================================================================
                                        SUB PrintToScreen (BYVAL hClient AS DWORD, sArray() AS STRING)
                                        '--------------------------------------------------------------------
                                        ' Preview control setup
                                        '--------------------------------------------------------------------
                                          LOCAL x, y, DW, dh, w, h, doFlag AS LONG
                                          LOCAL pd AS PreviewData PTR
                                        
                                          '------------------------------------------------------------------
                                          ' Turn off redraw for smoother action
                                          '------------------------------------------------------------------
                                          DIALOG SEND hClient, %WM_SETREDRAW, 0, 0
                                        
                                          '------------------------------------------------------------------
                                          DIALOG GET USER hClient, 2 TO pd    ' Get preview data
                                          IF pd = 0 THEN EXIT SUB             ' Ensure we got something
                                        
                                          '------------------------------------------------------------------
                                        ShowPreviewStart:
                                          DIALOG GET CLIENT hClient TO DW, dh ' client size
                                        
                                          IF @pd.zoom = 0 THEN                ' Auto-size mode
                                              w = DW - 14                     ' Allow some space left/right
                                              h = dh - 14                     ' Allow some space above/below
                                          ELSE
                                              w = (@pd.zoom / 100) * (@pd.pw * (@pd.gXppi / @pd.pXppi))
                                              h = (@pd.zoom / 100) * (@pd.ph * (@pd.gYppi / @pd.pYppi))
                                          END IF
                                        
                                          IF @pd.orient = 2 THEN              ' If Landscape mode
                                              @pd.gw = w                      ' Graphic width
                                              @pd.gh = w * @pd.ph/@pd.pw      ' Graphic proportional height
                                              IF @pd.gh > h THEN              ' If Graphic height > max height
                                                  @pd.gh = h                  ' Resize to max height
                                                  @pd.gw = h * @pd.pw/@pd.ph  ' New proportional width
                                              END IF
                                          ELSE                                ' Else Portrait mode (default)
                                              @pd.gh = h                      ' Graphic proportional height
                                              @pd.gw = h * @pd.pw/@pd.ph      ' Graphic proportional width
                                              IF @pd.gw > w THEN              ' If Graphic width > max width
                                                  @pd.gw = w                  ' Resize to max width
                                                  @pd.gh = w * @pd.ph/@pd.pw  ' New proportional height
                                              END IF
                                          END IF
                                          x = MAX&(7, (DW - @pd.gw) / 2)      ' Horizontally centered
                                          y = MAX&(7, (dh - @pd.gh) / 2)      ' Vertically centered
                                        
                                          '------------------------------------------------------------------
                                          ' SetScrollSize, then repeat the calculations + SetScrollSize
                                          ' to ensure correct view in case scrollbars were added/removed.
                                          '------------------------------------------------------------------
                                          SetScrollSize hClient, @pd.gw + x + 7, @pd.gh + y + 7
                                          IF doFlag = 0 THEN
                                              doFlag = 1
                                              GOTO ShowPreviewStart
                                          END IF
                                        
                                          '------------------------------------------------------------------
                                          ' Destroy and re-create the Graphic control with new size
                                          '------------------------------------------------------------------
                                          CONTROL KILL hClient, %IDC_GRAPHICVIEW
                                          CONTROL ADD GRAPHIC, hClient, %IDC_GRAPHICVIEW, "", x, y, @pd.gw, @pd.gh, %SS_NOTIFY
                                          GRAPHIC ATTACH hClient, %IDC_GRAPHICVIEW, REDRAW
                                          GRAPHIC COLOR @pd.clrWindowText, @pd.clrWindow
                                          GRAPHIC CLEAR
                                        
                                          @pd.zoomX = @pd.gw / (@pd.pw * (@pd.gXppi / @pd.pXppi)) ' horizontal zoom factor
                                          @pd.zoomY = @pd.gh / (@pd.ph * (@pd.gYppi / @pd.pYppi)) ' vertical zoom factor
                                        
                                          '------------------------------------------------------------------
                                          ' Calculate margin positions
                                          '------------------------------------------------------------------
                                          @pd.gx1 = @pd.mL * (@pd.gXppi / 25.40) * @pd.zoomX
                                          @pd.gy1 = @pd.mT * (@pd.gYppi / 25.40) * @pd.zoomY
                                          @pd.gx2 = @pd.gw - (@pd.mR * (@pd.gXppi / 25.40) * @pd.zoomX) - 2 ' Adjust for 2-pixel shadow
                                          @pd.gy2 = @pd.gh - (@pd.mB * (@pd.gYppi / 25.40) * @pd.zoomY) - 2
                                        
                                          '------------------------------------------------------------------
                                          ' Stretch Graphic buffer to the new Graphic view control
                                          '------------------------------------------------------------------
                                          GRAPHIC STRETCH hClient, %IDC_GRAPHICBUFF, _
                                                         (0, 0)-(@pd.gpw-1, @pd.gph-1) TO (0, 0)-(@pd.gw-1, @pd.gh-1)
                                        
                                          '------------------------------------------------------------------
                                          ' Draw margins, using a dotted line
                                          '------------------------------------------------------------------
                                          GRAPHIC WIDTH 1
                                          GRAPHIC STYLE 2
                                          GRAPHIC LINE (@pd.gx1, 6)-(@pd.gx1, @pd.gh-6), @pd.clrShadow  ' Left margin
                                          GRAPHIC LINE (@pd.gx2, 6)-(@pd.gx2, @pd.gh-6), @pd.clrShadow  ' Right margin
                                          GRAPHIC LINE (6, @pd.gy1)-(@pd.gw-6, @pd.gy1), @pd.clrShadow  ' Top margin
                                          GRAPHIC LINE (6, @pd.gy2)-(@pd.gw-6, @pd.gy2), @pd.clrShadow  ' Bottom margin
                                        
                                          '------------------------------------------------------------------
                                          ' Draw borders and shadows
                                          '------------------------------------------------------------------
                                          GRAPHIC WIDTH 2
                                          GRAPHIC STYLE 1
                                          GRAPHIC BOX (1,1)-(@pd.gw-2, @pd.gh-2), 0, @pd.clrBorder        ' Blue-ish border
                                          GRAPHIC LINE (@pd.gw-1, 2)-(@pd.gw-1, @pd.gh), @pd.clrDkShadow  ' Right
                                          GRAPHIC LINE (2, @pd.gh-1)-(@pd.gw, @pd.gh-1), @pd.clrDkShadow  ' Bottom
                                          GRAPHIC LINE (@pd.gw-1, -1)-(@pd.gw-1, 2), @pd.clrShadow  ' Top/right corner adjustment
                                          GRAPHIC LINE (-1, @pd.gh-1)-(2, @pd.gh-1), @pd.clrShadow  ' Left/bottom corner adjustment
                                        
                                          '------------------------------------------------------------------
                                          SetScrollPosHV hClient                    ' Set scroll position
                                          DIALOG SEND hClient, %WM_SETREDRAW, 1, 0  ' Turn on redraw
                                          DIALOG REDRAW hClient                     ' and redraw
                                          GRAPHIC REDRAW
                                        
                                        END SUB
                                        
                                        
                                        '====================================================================
                                        SUB SetScrollPosHV (BYVAL hClient AS DWORD)
                                        '--------------------------------------------------------------------
                                        ' Set scroll position
                                        '--------------------------------------------------------------------
                                          LOCAL rc AS RECT, si AS SCROLLINFO
                                          LOCAL pd AS PreviewData PTR
                                          STATIC oldW AS LONG, oldH AS LONG
                                        
                                          '------------------------------------------------------------------
                                          DIALOG GET USER hClient, 2 TO pd        ' Get preview data
                                        
                                          '------------------------------------------------------------------
                                          GetClientRect hClient, rc   ' scroll window's client size, in pixels
                                        
                                          '------------------------------------------------------------------
                                          ' Set horizontal scroll
                                          '------------------------------------------------------------------
                                          si.cbSize = SIZEOF(SCROLLINFO)       ' fill in SCROLLINFO members
                                          si.fMask  = %SIF_ALL                 ' Grab all scrollbar info
                                          GetScrollInfo hClient, %SB_HORZ, si
                                        
                                          IF oldW = 0 THEN oldW = @pd.gw + 13
                                          rc.nLeft = MAX&(0, rc.nRight - oldW)
                                        
                                          IF @pd.zoom = 0 OR ((@pd.pt.x + si.nPos - rc.nLeft) < oldW / 3) THEN
                                              si.nPos = 0
                                          ELSEIF (@pd.pt.x + si.nPos - rc.nLeft) > oldW / 3 * 2 THEN
                                              si.nPos = si.nMax - si.nPage
                                          ELSE
                                              si.nPos = MIN&(si.nMax - si.nPage, ((@pd.pt.x + si.nPos) / oldW) * si.nMax - @pd.pt.x)
                                          END IF
                                          oldW = @pd.gw + 13
                                        
                                          si.fMask  = %SIF_POS                    ' Set scrollbar pos
                                          SetScrollInfo hClient, %SB_HORZ, si, 1  ' set horizontal scrollbar info
                                          ScrollWindow hClient, -si.nPos, 0, BYVAL %NULL, BYVAL %NULL
                                        
                                          '------------------------------------------------------------------
                                          ' Set vertical scroll
                                          '------------------------------------------------------------------
                                          si.fMask  = %SIF_ALL                 ' Grab all scrollbar info
                                          GetScrollInfo hClient, %SB_VERT, si
                                        
                                          IF oldH = 0 THEN oldH = @pd.gh + 13
                                          rc.nTop = MAX&(0, rc.nBottom - oldH)
                                        
                                          IF @pd.zoom = 0 OR (si.nPos = 0 AND ((@pd.pt.y + si.nPos - rc.nTop) < oldH / 3)) THEN
                                              si.nPos = 0
                                          ELSEIF (@pd.pt.y + si.nPos - rc.nTop) > oldH / 3 * 2 THEN
                                              si.nPos = si.nMax - si.nPage
                                          ELSE
                                              si.nPos = MIN&(si.nMax - si.nPage, ((@pd.pt.y + si.nPos) / oldH) * si.nMax - @pd.pt.y)
                                          END IF
                                          oldH = @pd.gh + 13
                                        
                                          si.fMask  = %SIF_POS                    ' Set scrollbar pos
                                          SetScrollInfo hClient, %SB_VERT, si, 1  ' set vertical scrollbar info
                                          ScrollWindow hClient, 0, -si.nPos, BYVAL %NULL, BYVAL %NULL
                                        
                                          '------------------------------------------------------------------
                                          ' Reset cursor position
                                          '------------------------------------------------------------------
                                          IF @pd.zoom = 0 THEN
                                              @pd.pt.x = 0
                                              @pd.pt.y = 0
                                          END IF
                                        
                                        END SUB
                                        
                                        
                                        '====================================================================
                                        SUB SetScrollSize (BYVAL hClient AS DWORD, BYVAL w AS LONG, BYVAL h AS LONG)
                                        '--------------------------------------------------------------------
                                        ' Set scroll area
                                        '--------------------------------------------------------------------
                                          LOCAL rc AS RECT, si AS SCROLLINFO
                                          '------------------------------------------------------------------
                                          GetClientRect hClient, rc   ' scroll window's client size, in pixels
                                        
                                          '------------------------------------------------------------------
                                          ' adjust scrolling zone to include eventual scrollbars
                                          '------------------------------------------------------------------
                                          IF (GetWindowLong(hClient, %GWL_STYLE) AND %WS_HSCROLL) = 0 THEN
                                              IF w > rc.nRight  THEN
                                                  rc.nBottom = rc.nBottom - GetSystemMetrics(%SM_CYHSCROLL)
                                              END IF
                                          ELSE
                                              IF w < rc.nRight - GetSystemMetrics(%SM_CXVSCROLL) THEN
                                                  rc.nBottom = rc.nBottom + GetSystemMetrics(%SM_CYHSCROLL)
                                              END IF
                                          END IF
                                        
                                          IF (GetWindowLong(hClient, %GWL_STYLE) AND %WS_VSCROLL) = 0 THEN
                                              IF h > rc.nBottom THEN
                                                  rc.nRight = rc.nRight - GetSystemMetrics(%SM_CXVSCROLL)
                                              END IF
                                          ELSE
                                              IF h < rc.nBottom - GetSystemMetrics(%WS_HSCROLL) THEN
                                                  rc.nRight = rc.nRight + GetSystemMetrics(%SM_CXVSCROLL)
                                              END IF
                                          END IF
                                        
                                          '------------------------------------------------------------------
                                          h = MAX&(h, rc.nBottom) - 1
                                          w = MAX&(w, rc.nRight) - 1
                                        
                                          si.cbSize = SIZEOF(SCROLLINFO)          ' fill in SCROLLINFO members
                                          si.fMask  = %SIF_PAGE OR %SIF_RANGE     ' need this mask
                                        
                                          '------------------------------------------------------------------
                                          GetScrollInfo hClient, %SB_HORZ, si
                                          si.nMin   = 0                           ' min scroll pos
                                          si.nMax   = w                           ' max scroll pos
                                          si.nPage  = rc.nRight                   ' page width
                                          SetScrollInfo hClient, %SB_HORZ, si, 1  ' set horizontal scrollbar info
                                          '------------------------------------------------------------------
                                          GetScrollInfo hClient, %SB_VERT, si
                                          si.nMin   = 0                           ' min scroll pos
                                          si.nMax   = h                           ' max scroll pos
                                          si.nPage  = rc.nBottom                  ' page height
                                          SetScrollInfo hClient, %SB_VERT, si, 1  ' set vertical scrollbar info
                                        
                                        END SUB
                                        
                                        
                                        '====================================================================
                                        CALLBACK FUNCTION ViewPortProc() AS LONG
                                        '--------------------------------------------------------------------
                                        ' View port - Child dialog's (ScrollWindow's) callback procedure
                                        '--------------------------------------------------------------------
                                          SELECT CASE AS LONG CB.MSG
                                          CASE %WM_HSCROLL    : FUNCTION = vp_OnHScroll (CB.HNDL, CB.WPARAM, CB.LPARAM)
                                          CASE %WM_VSCROLL    : FUNCTION = vp_OnVScroll (CB.HNDL, CB.WPARAM, CB.LPARAM)
                                          END SELECT
                                        
                                        END FUNCTION
                                        
                                        
                                        '====================================================================
                                        FUNCTION vp_OnHScroll (BYVAL hWnd AS DWORD, _
                                                               BYVAL wParam AS DWORD, _
                                                               BYVAL lParam AS LONG) AS LONG
                                        '--------------------------------------------------------------------
                                          ' %WM_HSCROLL handler
                                          '------------------------------------------------------------------
                                          LOCAL oldPosY AS LONG
                                          LOCAL si AS SCROLLINFO
                                        
                                          si.cbSize = SIZEOF(SCROLLINFO)
                                          si.fMask  = %SIF_POS OR %SIF_PAGE OR %SIF_RANGE
                                          GetScrollInfo hWnd, %SB_HORZ, si
                                          oldPosY   = si.nPos
                                        
                                          SELECT CASE LOWRD(wParam)
                                              CASE %SB_LINELEFT   : si.nPos = si.nPos - 10
                                              CASE %SB_PAGELEFT   : si.nPos = si.nPos - si.nPage
                                              CASE %SB_LINERIGHT  : si.nPos = si.nPos + 10
                                              CASE %SB_PAGERIGHT  : si.nPos = si.nPos + si.nPage
                                              CASE %SB_THUMBTRACK : si.nPos = HIWRD(wParam)
                                              CASE ELSE           : EXIT FUNCTION
                                          END SELECT
                                        
                                          ' Ensure pos is within range
                                          si.nPos = MAX&(si.nMin, MIN&(si.nPos, si.nMax - si.nPage))
                                        
                                          ' Update the scroll bar and scroll window
                                          si.fMask  = %SIF_POS
                                          SetScrollInfo hWnd, %SB_HORZ, si, 1
                                          ScrollWindow hWnd, oldPosY - si.nPos, 0, BYVAL %NULL, BYVAL %NULL
                                        
                                        END FUNCTION
                                        
                                        
                                        '====================================================================
                                        FUNCTION vp_OnMouseWheel (BYVAL hWnd AS DWORD, _
                                                                  BYVAL wParam AS DWORD, _
                                                                  BYVAL lParam AS LONG) AS LONG
                                        '--------------------------------------------------------------------
                                          ' %WM_MOUSEWHEEL handler
                                          '------------------------------------------------------------------
                                        
                                          LOCAL zDelta AS INTEGER, pvParam AS LONG
                                          zDelta = HIWRD(wParam)
                                        
                                          SystemParametersInfo %SPI_GETWHEELSCROLLLINES, 0, pvParam, 0
                                          IF pvParam = 0 THEN pvParam = 1
                                        
                                          IF (LOWRD(wParam) AND %MK_MBUTTON) = %MK_MBUTTON THEN 'horizontal scroll
                                              IF pvParam > 0 THEN
                                                  IF zDelta > 0 THEN               'scroll to the left  (Arrow left)
                                                      SendMessage hWnd, %WM_HSCROLL, MAKLNG(%SB_LINELEFT, 0), 0
                                                  ELSE                             'scroll to the right (Arrow right
                                                      SendMessage hWnd, %WM_HSCROLL, MAKLNG(%SB_LINERIGHT, 0), 0
                                                  END IF
                                              ELSE
                                                  IF zDelta > 0 THEN               'scroll to the left  (Page left)
                                                      SendMessage hWnd, %WM_HSCROLL, MAKLNG(%SB_PAGELEFT, 0), 0
                                                  ELSE                             'scroll to the right (Page right
                                                      SendMessage hWnd, %WM_HSCROLL, MAKLNG(%SB_PAGERIGHT, 0), 0
                                                  END IF
                                              END IF
                                        
                                          ELSE                                      'vertical scroll
                                              IF pvParam > 0 THEN
                                                  IF zDelta > 0 THEN                'scroll upwards    (Arrow up)
                                                      FOR pvParam = 1 TO pvParam
                                                          SendMessage hWnd, %WM_VSCROLL, MAKLNG(%SB_LINEUP, 0), 0
                                                      NEXT
                                                  ELSE                              'scroll downwards  (Arrow down)
                                                      FOR pvParam = 1 TO pvParam
                                                          SendMessage hWnd, %WM_VSCROLL, MAKLNG(%SB_LINEDOWN, 0), 0
                                                      NEXT
                                                  END IF
                                              ELSE
                                                  IF zDelta > 0 THEN                'scroll upwards    (Page up)
                                                      SendMessage hWnd, %WM_VSCROLL, MAKLNG(%SB_PAGEUP, 0), 0
                                                  ELSE                              'scroll downwards  (Page down)
                                                      SendMessage hWnd, %WM_VSCROLL, MAKLNG(%SB_PAGEDOWN, 0), 0
                                                  END IF
                                              END IF
                                          END IF
                                        END FUNCTION
                                        
                                        
                                        '====================================================================
                                        FUNCTION vp_OnVScroll (BYVAL hWnd AS DWORD, _
                                                               BYVAL wParam AS DWORD, _
                                                               BYVAL lParam AS LONG) AS LONG
                                        '--------------------------------------------------------------------
                                          ' %WM_VSCROLL handler
                                          '------------------------------------------------------------------
                                          LOCAL oldPosY AS LONG
                                          LOCAL si AS SCROLLINFO
                                        
                                          si.cbSize = SIZEOF(SCROLLINFO)
                                          si.fMask  = %SIF_POS OR %SIF_PAGE OR %SIF_RANGE
                                          GetScrollInfo hWnd, %SB_VERT, si
                                          oldPosY   = si.nPos
                                        
                                          SELECT CASE LOWRD(wParam)
                                              CASE %SB_LINEDOWN   : si.nPos = si.nPos + 10
                                              CASE %SB_PAGEDOWN   : si.nPos = si.nPos + si.nPage
                                              CASE %SB_LINEUP     : si.nPos = si.nPos - 10
                                              CASE %SB_PAGEUP     : si.nPos = si.nPos - si.nPage
                                              CASE %SB_THUMBTRACK : si.nPos = HIWRD(wParam)
                                              CASE ELSE           : EXIT FUNCTION
                                          END SELECT
                                        
                                          ' Ensure pos is within range
                                          si.nPos = MAX&(si.nMin, MIN&(si.nPos, si.nMax - si.nPage))
                                        
                                          ' Update the scroll bar and scroll window
                                          si.fMask  = %SIF_POS
                                          SetScrollInfo hWnd, %SB_VERT, si, 1
                                          ScrollWindow hWnd, 0, oldPosY - si.nPos, BYVAL %NULL, BYVAL %NULL
                                        
                                        END FUNCTION
                                        Robert

                                        Comment

                                        Working...
                                        X