Ok here is the situation.
The code below is close "I THINK" , but I cannot seem to get the picturebox to accurately display what the printer would print with accurate margins etc..
note: everything is set to twips and as the msdn states 1440 twips = 1 logical inch so the goal is to display 1 inch margins and also print with 1 inch "THE PRINTING IS WORKING FINE" the preview is not.
I hammered together a quick test example to make it easier for anyone to assist.
Thank You.
The code below is close "I THINK" , but I cannot seem to get the picturebox to accurately display what the printer would print with accurate margins etc..
note: everything is set to twips and as the msdn states 1440 twips = 1 logical inch so the goal is to display 1 inch margins and also print with 1 inch "THE PRINTING IS WORKING FINE" the preview is not.
I hammered together a quick test example to make it easier for anyone to assist.
Thank You.
Code:
Option Explicit
'Add 2 command buttons
'Add 1 HScrollBar
'Add 1 VScrollBar
'Add 1 RichTextBox
'Add 1 PictureBox AND PLACE A PictureBox "Picture2" within the first picturebox
'Paste code and test
'I have been pasting all of this code to the RTB, Printing, and Previewing
'and trying various changes with no success.
'my goal is for the last line of the print out to match the last line of the preview
'with margin of 1 inch in preview
'thank you for any help.
Private Const WM_USER = &H400
Private Const EM_FORMATRANGE As Long = WM_USER + 57
Private Const PHYSICALOFFSETX As Long = 112
Private Const PHYSICALOFFSETY As Long = 113
Private Type CHARRANGE
cpMin As Long ' First character of range (0 for start of doc)
cpMax As Long ' Last character of range (-1 for end of doc)
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type FormatRange
hdc As Long ' Actual DC to draw on
hdcTarget As Long ' Target DC for determining text formatting
rc As RECT ' Region of the DC to draw to (in twips)
rcPage As RECT ' Region of the entire DC (page size) (in twips)
chrg As CHARRANGE ' Range of text to draw (see above declaration)
End Type
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Sub Command1_Click()
'PREVIEW
ShowPreview
End Sub
Private Sub Command2_Click()
'PRINT
PrintPage
End Sub
Private Sub Form_Load()
'Size and position User Interface
Me.Height = 9000
Me.Width = 12500
'
Picture1.Top = 120
Picture1.Left = 120
Picture1.Width = 10000
Picture1.Height = 4000
'
HScroll1.Top = Picture1.Top + Picture1.Height
HScroll1.Left = 120
HScroll1.Width = Picture1.Width
'
VScroll1.Top = 120
VScroll1.Left = Picture1.Left + Picture1.Width
VScroll1.Height = Picture1.Height
'
RichTextBox1.Top = HScroll1.Top + HScroll1.Height
RichTextBox1.Left = 120
RichTextBox1.Width = Picture1.Width
RichTextBox1.Height = 4000
'
Command1.Top = 120
Command1.Left = 10500 + VScroll1.Width
'
Command2.Top = RichTextBox1.Top
Command2.Left = 10500 + VScroll1.Width
'Add Captions
Command1.Caption = "Preview"
Command2.Caption = "Print Page"
'Autoverb for mouse paste
RichTextBox1.AutoVerbMenu = True
'RTB Font
RichTextBox1.Font.Name = "Courier New"
RichTextBox1.Font.Size = 10
'Set scroll bars to 100
HScroll1.Max = 100
VScroll1.Max = 100
'Setup PicBox for display
Picture2.AutoRedraw = True
Picture2.Appearance = 0
Picture2.BorderStyle = 0
Picture2.BackColor = vbWhite
Picture2.ScaleMode = vbTwips
'Position & Set Picture2 to paper size
Picture2.Top = 0
Picture2.Left = 0
Picture2.Height = 15840 'because 1440 twips = 1 inch and paper will be 11 in H
Picture2.Width = 12240 'because 1440 twips = 1 inch and paper will be 8.5 in W
End Sub
Public Sub ShowPreview()
Dim nLeftOffset As Long
Dim nTopOffset As Long
Dim nLeftMargin As Long
Dim nTopMargin As Long
Dim nRightMargin As Long
Dim nBottomMargin As Long
Dim fr As FormatRange
Dim rcDrawTo As RECT
Dim rcPage As RECT
Dim nTextLength As Long
Dim nRet As Long
'1440 for desired 1inch margins
Printer.TrackDefault = True
Printer.Orientation = 1
Printer.ScaleMode = vbTwips
nLeftOffset = Printer.ScaleX(GetDeviceCaps(Printer.hdc, PHYSICALOFFSETX), vbPixels, vbTwips)
nTopOffset = Printer.ScaleY(GetDeviceCaps(Printer.hdc, PHYSICALOFFSETY), vbPixels, vbTwips)
nLeftOffset = Printer.ScaleX(GetDeviceCaps(Printer.hdc, PHYSICALOFFSETX), vbPixels, vbTwips)
nTopOffset = Printer.ScaleY(GetDeviceCaps(Printer.hdc, PHYSICALOFFSETY), vbPixels, vbTwips)
nLeftMargin = 1440 - nLeftOffset
nTopMargin = 1440 - nTopOffset
nRightMargin = Printer.Width - (1440 - nLeftOffset)
nBottomMargin = Printer.Height - (1440 - nTopOffset)
rcPage.Left = 0
rcPage.Top = 0
rcPage.Right = Printer.ScaleWidth
rcPage.Bottom = Printer.ScaleHeight
rcDrawTo.Left = nLeftMargin
rcDrawTo.Top = nTopMargin
rcDrawTo.Right = nRightMargin
rcDrawTo.Bottom = nBottomMargin
fr.rc = rcDrawTo
fr.rcPage = rcPage
fr.chrg.cpMin = 0
fr.chrg.cpMax = -1
nTextLength = Len(RichTextBox1.Text)
fr.rc = rcDrawTo
fr.rcPage = rcPage
fr.hdc = Picture2.hdc
fr.hdcTarget = Printer.hdc
SendMessage RichTextBox1.hwnd, EM_FORMATRANGE, True, fr
Picture2.Print
'Allow RichTextBox to free up memory
nRet = SendMessage(RichTextBox1.hwnd, EM_FORMATRANGE, False, ByVal CLng(0))
End Sub
Public Sub PrintPage()
Dim nLeftOffset As Long
Dim nTopOffset As Long
Dim nLeftMargin As Long
Dim nTopMargin As Long
Dim nRightMargin As Long
Dim nBottomMargin As Long
Dim fr As FormatRange
Dim rcDrawTo As RECT
Dim rcPage As RECT
Dim nTextLength As Long
Dim nRet As Long
'1440 for desired 1inch margins
Printer.TrackDefault = True
Printer.Orientation = 1
Printer.Print Space(0)
Printer.ScaleMode = vbTwips
nLeftOffset = Printer.ScaleX(GetDeviceCaps(Printer.hdc, PHYSICALOFFSETX), vbPixels, vbTwips)
nTopOffset = Printer.ScaleY(GetDeviceCaps(Printer.hdc, PHYSICALOFFSETY), vbPixels, vbTwips)
nLeftMargin = 1440 - nLeftOffset
nTopMargin = 1440 - nTopOffset
nRightMargin = Printer.Width - (1440 - nLeftOffset)
nBottomMargin = Printer.Height - (1440 - nTopOffset)
rcPage.Left = 0
rcPage.Top = 0
rcPage.Right = Printer.ScaleWidth
rcPage.Bottom = Printer.ScaleHeight
rcDrawTo.Left = nLeftMargin
rcDrawTo.Top = nTopMargin
rcDrawTo.Right = nRightMargin
rcDrawTo.Bottom = nBottomMargin
fr.rc = rcDrawTo
fr.rcPage = rcPage
fr.chrg.cpMin = 0
fr.chrg.cpMax = -1
nTextLength = Len(RichTextBox1.Text)
fr.rc = rcDrawTo
fr.rcPage = rcPage
fr.hdc = Printer.hdc
fr.hdcTarget = Printer.hdc
SendMessage RichTextBox1.hwnd, EM_FORMATRANGE, True, fr
Printer.Print
Printer.EndDoc
'Allow RichTextBox to free up memory
nRet = SendMessage(RichTextBox1.hwnd, EM_FORMATRANGE, False, ByVal CLng(0))
End Sub
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'''All code below this line is for the scroll bars''''
Private Sub VScroll1_Change()
Call tp
End Sub
Private Sub VScroll1_Scroll()
Call tp
End Sub
Private Sub HScroll1_Change()
Call lft
End Sub
Private Sub HScroll1_Scroll()
Call lft
End Sub
Private Sub tp()
Dim l As Double
Dim a As Double
Dim X As Double
X = VScroll1.Value
a = Picture2.Height - Picture1.Height
l = (a * X) / 100
Picture2.Top = -l
End Sub
Private Sub lft()
Dim l As Double
Dim a As Double
Dim X As Double
X = HScroll1.Value
a = Picture2.Width - Picture1.Width
l = (a * X) / 100
Picture2.Left = -l
End Sub