这是一个旋转打印的例子;原理也是打印pictureType FontCoord x As Integer y As Integer End TypeType RkFont Vert() As FontCoord Nv As Integer End TypePublic F As RkFont Public FontVect(256) As RkFont Public Const PI = 3.14159265358979Function Rad(angle As Single) As Single Rad = angle * PI / 180 End Function Sub DrawLettera(Obj As Object, C As String, Sx As Integer, Sy As Integer, Xc As Integer, Yc As Integer, Size As Integer, Angolo As Single, Stile As Integer)
' Draws a Single Letter
Dim Nv As Integer Dim Ci As Integer Dim i As Integer Dim x As Single, y As Single Dim x1 As Single, y1 As Single Dim Seno As Single, Coseno As Single Dim xA As Single, yA As Single
' xC char space x (120) ' yC height (215)
Ci = Asc(C)
Nv = FontVect(Ci).Nv
For i = 1 To Nv
x = FontVect(Ci).Vert(i).x y = FontVect(Ci).Vert(i).y
x = (Xc + x) / 120 y = y / 120 x = x * Size y = y * Size
' Do Rotation If Angolo <> 0 Then Seno = Sin(Rad(Angolo)): Coseno = Cos(Rad(Angolo)) xA = Coseno * x - Seno * y yA = Seno * x + Coseno * y x = xA y = yA End If
' start point x = Sx + x y = Sy + y
If i Mod 2 Then x1 = x y1 = y Else Obj.Line (x, y)-(x1, y1) End If NextEnd SubSub LoadFont()' Load a Font. ' Note: ' To build addtional font types look as the FontVect structure is done '
On Error Resume Next
Open App.Path & "\Courier.Fnt" For Binary As 1 Get 1#, , FontVect Close End SubSub TestFont(P As Object)
' Example 1 - ROTATION Dim Size As Integer Dim x As Integer, y As Integer Dim Angolo As Single Dim Stile As Integer Dim Width As Integer
P.ScaleMode = 3 ' NEED TO BE LIKE THIS!!!!
Size = 12 Width = 1 Stile = 0
x = P.ScaleWidth / 2 y = P.ScaleHeight / 2
If TypeOf P Is PictureBox Then P.Cls For Angolo = 0 To 340 Step 20 TextTo P, " Visual Basic", x, y, Size, 1, Angolo, Stile NextEnd SubSub TestFont2(P As Object)
' Example 3: Linear print Dim Size As Integer Dim x As Integer, y As Integer Dim Angolo As Single Dim Stile As Integer Dim Width As Integer
P.ScaleMode = 3 ' NEED TO BE LIKE THIS!!!!
Size = 18 Width = 1 Stile = 0
x = 0 y = 0
P.Cls
x = -Size For r = 1 To 10 y = (r - 1) * (Size + 10) ' step y x = x + Size 'TextTo P, "Rigo N? & CStr(r), x, y, Size, 1, Angolo, Stile" Next End Sub Sub TestSetChar(P As Object)' Example 2: CHAR SET Dim Size As Integer Dim x As Integer, y As Integer Dim Angolo As Single Dim Stile As Integer Dim Width As Integer Dim Chars(5) As String
For r = 1 To 4 y = (r - 1) * Size ' step y TextTo P, Chars(r), x, y, Size, 1, Angolo, Stile Next
End Sub Sub TextTo(Obj As Object, Stringa As Variant, x As Integer, y As Integer, Size As Integer, Width As Integer, Angolo As Single, Stile As Integer)
' TextTo ' Obj - PictureBox, Form or Printer ' Stringa - String to Print ' x, y - Starting point ' Size - Font Size ' Width - tickness ' Angolo- Rotation angle ' Stile- 0-Trasparent, 1-Solid (not active)
Dim CaretX As Integer, CaretY As Integer Dim C As String, i As Integer
If Width <= 0 Then Width = Obj.DrawWidth If Size <= 0 Then Size = Obj.FontSize
If TypeOf Obj Is Printer Then Sz% = Size * 3 Else Sz% = Size End If
Obj.DrawWidth = Width
' For each char extacts from FontVect the corresponding sets of lines and draws them ' each sets of lines are stored in the letter's chr(x) index
For i = 1 To Len(Stringa) C = Mid$(Stringa, i, 1) CaretX = (i - 1) * 120 CaretY = 0 DrawLettera Obj, C, x, y, CaretX, CaretY, Sz%, Angolo, Stile Next
用到的主要API的函数 Private Declare Function CreateFontIndirect Lib "gdi32" Alias "CreateFontIndirectA" (lpLogFont As LogFont) As Long Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As LongPrivate Const LOGPIXELSY = 90 ' Logical pixels/inch in YPrivate Declare Function GetDesktopWindow Lib "user32" () As Long Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hdc As Long) As Long如果需要,我有一个完整的类,只要传入旋转的角度就可以了。 [email protected]
x As Integer
y As Integer
End TypeType RkFont
Vert() As FontCoord
Nv As Integer
End TypePublic F As RkFont
Public FontVect(256) As RkFont
Public Const PI = 3.14159265358979Function Rad(angle As Single) As Single
Rad = angle * PI / 180
End Function
Sub DrawLettera(Obj As Object, C As String, Sx As Integer, Sy As Integer, Xc As Integer, Yc As Integer, Size As Integer, Angolo As Single, Stile As Integer)
' Draws a Single Letter
Dim Nv As Integer
Dim Ci As Integer
Dim i As Integer
Dim x As Single, y As Single
Dim x1 As Single, y1 As Single
Dim Seno As Single, Coseno As Single
Dim xA As Single, yA As Single
' xC char space x (120)
' yC height (215)
Ci = Asc(C)
Nv = FontVect(Ci).Nv
For i = 1 To Nv
x = FontVect(Ci).Vert(i).x
y = FontVect(Ci).Vert(i).y
x = (Xc + x) / 120
y = y / 120
x = x * Size
y = y * Size
' Do Rotation
If Angolo <> 0 Then
Seno = Sin(Rad(Angolo)): Coseno = Cos(Rad(Angolo))
xA = Coseno * x - Seno * y
yA = Seno * x + Coseno * y
x = xA
y = yA
End If
' start point
x = Sx + x
y = Sy + y
If i Mod 2 Then
x1 = x
y1 = y
Else
Obj.Line (x, y)-(x1, y1)
End If
NextEnd SubSub LoadFont()' Load a Font.
' Note:
' To build addtional font types look as the FontVect structure is done
'
On Error Resume Next
Open App.Path & "\Courier.Fnt" For Binary As 1
Get 1#, , FontVect
Close
End SubSub TestFont(P As Object)
' Example 1 - ROTATION Dim Size As Integer
Dim x As Integer, y As Integer
Dim Angolo As Single
Dim Stile As Integer
Dim Width As Integer
P.ScaleMode = 3 ' NEED TO BE LIKE THIS!!!!
Size = 12
Width = 1
Stile = 0
x = P.ScaleWidth / 2
y = P.ScaleHeight / 2
If TypeOf P Is PictureBox Then P.Cls
For Angolo = 0 To 340 Step 20
TextTo P, " Visual Basic", x, y, Size, 1, Angolo, Stile
NextEnd SubSub TestFont2(P As Object)
' Example 3: Linear print Dim Size As Integer
Dim x As Integer, y As Integer
Dim Angolo As Single
Dim Stile As Integer
Dim Width As Integer
P.ScaleMode = 3 ' NEED TO BE LIKE THIS!!!!
Size = 18
Width = 1
Stile = 0
x = 0
y = 0
P.Cls
x = -Size
For r = 1 To 10
y = (r - 1) * (Size + 10) ' step y
x = x + Size
'TextTo P, "Rigo N? & CStr(r), x, y, Size, 1, Angolo, Stile"
Next
End Sub
Sub TestSetChar(P As Object)' Example 2: CHAR SET Dim Size As Integer
Dim x As Integer, y As Integer
Dim Angolo As Single
Dim Stile As Integer
Dim Width As Integer
Dim Chars(5) As String
Chars(1) = "ABCDEFGHILMNOPQRSTUVZWYKX"
Chars(2) = "abcdefghilmnopqrstuvzwykx"
Chars(3) = "0123456789\|!?%&/()=?^ъ"
Chars(4) = "_.:-,;喟蜱+*栝[]@#"
P.ScaleMode = 3 ' NEED TO BE LIKE THIS!!!!
Size = 18
Width = 1
Stile = 0
x = 0
y = 0
P.Cls
For r = 1 To 4
y = (r - 1) * Size ' step y
TextTo P, Chars(r), x, y, Size, 1, Angolo, Stile
Next
End Sub
Sub TextTo(Obj As Object, Stringa As Variant, x As Integer, y As Integer, Size As Integer, Width As Integer, Angolo As Single, Stile As Integer)
' TextTo
' Obj - PictureBox, Form or Printer
' Stringa - String to Print
' x, y - Starting point
' Size - Font Size
' Width - tickness
' Angolo- Rotation angle
' Stile- 0-Trasparent, 1-Solid (not active)
Dim CaretX As Integer, CaretY As Integer
Dim C As String, i As Integer
If Width <= 0 Then Width = Obj.DrawWidth
If Size <= 0 Then Size = Obj.FontSize
If TypeOf Obj Is Printer Then
Sz% = Size * 3
Else
Sz% = Size
End If
Obj.DrawWidth = Width
' For each char extacts from FontVect the corresponding sets of lines and draws them
' each sets of lines are stored in the letter's chr(x) index
For i = 1 To Len(Stringa)
C = Mid$(Stringa, i, 1)
CaretX = (i - 1) * 120
CaretY = 0
DrawLettera Obj, C, x, y, CaretX, CaretY, Sz%, Angolo, Stile
Next
End Sub
http://www.yueliangwan.com.cn/yf/PrintView/PrintView.htm
Private Declare Function CreateFontIndirect Lib "gdi32" Alias "CreateFontIndirectA" (lpLogFont As LogFont) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As LongPrivate Const LOGPIXELSY = 90 ' Logical pixels/inch in YPrivate Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hdc As Long) As Long如果需要,我有一个完整的类,只要传入旋转的角度就可以了。
[email protected]