by D¨nyi Tam¨s (1 Submission)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Wed 25th October 2000
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Description: The EASIEST way to print fully justified text!!! Place all the declarations and functions and subs 'into a .bas file:
API Declarations
'Place all these declarations and functions and subs
'into a .bas file:
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
Public Declare Function SetTextJustification Lib "gdi32" (ByVal hDC As Long, ByVal extra As Integer, ByVal break As Integer) As Integer
Const EM_GETLINE = &HC4
Const EM_GETLINECOUNT = &HBA
'You can change the names below if you want
With MDIForm1.ActiveForm.TextBox
Dim intRet As Long, strLineString As String
'Preparing a buffer for the string
strLineString = space$(80)
'Reading the line
intRet = SendMessage(.hwnd, EM_GETLINE, iLine, strLineString)
'The function returns with the text of the specified line
ReadLine = strLineString
End With
End Function
Function CountStrings(ByVal where As String, ByVal what As String) As Long
Dim pos As Long, count As Long
pos = 1
'If the searched string is found
Do While InStr(pos, where, what)
pos = InStr(pos, where, what) + 1
count = count + 1
Loop
'The fucntion returns the number of occurrence of the searched string
'within the specified text
CountStrings = count
End Function
Sub FilePrintText()
Dim lLineCount As Long, lCurrLine As Long
Dim space As Integer, extra As Integer, strPrint As String
With MDIForm1.ActiveForm.TextBox
'Determining the number of lines in the RichTextBox
lLineCount = SendMessage(.hwnd, EM_GETLINECOUNT, 0&, 0&)
End With
With Printer
'Changing the Scale Mode
.ScaleMode = vbCentimeters
'Determinig the top margin
.CurrentY = 0.5
'Determinig the PageWidth
.Width = 21
'Determinig the PageHeight
.Height = 29.7
'For all lines in the RichTextBox
For lCurrLine = 0 To lLineCount - 1
'Reading the current line
strPrint = RTrim(ReadLine(lCurrLine))
'Removing the previous values
SetTextJustification .hDC, 0, 0
'Determinig the Left margin
.CurrentX = 0.5
'Determinig the Bottom margin
If .CurrentY + .TextHeight > 28.7 Then
.NewPage
.CurrentY = 0.5
End If
'counting the spaces within the current line
space = CountStrings(strPrint, " ")
'Determining the extra space you need to add
'to justify the current line
extra = .ScaleX(20, vbCentimeters, vbTwips) - .TextWidth(strPrint)
'Justifing the current line
SetTextJustification .hDC, extra, space
'Sending the current line to the printer
Printer.Print strPrint
Next lCurrLine
'Printing the text
.EndDoc
End With
End Sub
No comments have been posted about Description: The EASIEST way to print fully justified text!!! Place all the declarations and functi. Why not be the first to post a comment about Description: The EASIEST way to print fully justified text!!! Place all the declarations and functi.