pic.Line (X1, Y1)-(X2, Y2), , B
Else
pic.Line (X1, Y1)-(X2, Y2)
End If
End Sub
Private Sub IMyPrinter_PPrint(Optional ByVal F0D1H2 As Integer = 0, Optional PrnInfo As Variant)
Select Case F0D1H2 "该参数为0:跟分号;1:跟逗号;2:无符号
Case 0
If Not IsMissing(PrnInfo) Then
pic.Print PrnInfo;
End If
Case 1
If Not IsMissing(PrnInfo) Then
pic.Print PrnInfo,
End If
Case 2
If Not IsMissing(PrnInfo) Then
pic.Print PrnInfo
Else
pic.Print
End If
End Select
End Sub
Private Function IMyPrinter_ScaleX(ByVal Width As Single, Optional ByVal FromScale As Variant, Optional ByVal ToScale As Variant) As Single
IMyPrinter_ScaleX = pic.ScaleX(Width, FromScale, ToScale)
End Function
Private Function IMyPrinter_TextWidth(ByVal Str As String) As Single
IMyPrinter_TextWidth = pic.TextWidth(Str)
End Function
Private Property Get IMyPrinter_Width() As Long
IMyPrinter_Width = pic.Width
End Property
Private Property Let IMyPrinter_Width(ByVal RHS As Long)
pic.Width = RHS
End Property
第三步、在主类模块定义如下变量
Private cIMyPrn As IMyPrinter
Private cPic As MyPic
Private cPrn As MyPrn
在主类模块Class_Initialize事件中,添加:
Set cPrn As New MyPrn
Set cIMyPrn = cPrn
在主类模块中,将原来有关打印语句中的Printer替换为cIMyPrn。在实现预览的方法中,添加:
Set cPic = New MyPic
Set cIMyPrn = cMyPic
修改特殊类型的语句(如Line、Print,方法名和参数形式已经改变,将其改为符合cIMyPrn语法的语句)
预览结束后:
Set cIMyPrn = cPrn
Set cPic = Nothing
通过上面的方法,就可以用一个IMyPrinter变量,实现打印和预览的前期绑定当然,该方法未必是实现打印、预览的首选,还可以使用API方法,直接操纵设备场景来实现打印预览,但该方法的意义不在于此,其意义在于该方法拓展了VB中提供的多态技术的应用范围。
本文副标题中的所谓“高层次”不是作者层次高,而是编程方法和思路,不是面向底层、面向Win核心,而是在对象的高层、在编程语言和方法的较高层上实现多态。
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




