写入图片框和打印机时避免重复

duplication avoidance when writing to picturebox and printer

所以偶尔用VB6编程,但我一直没有解决这个问题。

我有一个 Private Sub 可以向图片框绘制很多东西,例如线条、文本、图片。很多很多行代码。但后来我想使用相同的线条绘制到打印机对象。但是我不知道怎么做。

例如:

private sub command1_click()
    picture1.print "hello there"
    etc etc etc
end sub
private sub command2_click()
    printer.print "hello world"
    etc etc etc
    printer.print
end sub

成为

public sub pictureengine(action....)
    if action = draw then picturebox is selected for output
    if action = print then printer object is selected output 
    <object/control>.print "hello world"
    etc etc etc
    if action = print then printer.enddoc printer.print
end sub

应该有一个别名可以使用 controls/objects。 提前致谢

我确实遇到了这个问题。我决定通过实现 Interfaces 来抽象掉 PictureBox、Printer 或任何其他表面的细节。你最终会得到 3 类:

  • ISurface
  • CPrinterSurface
  • CPictureBoxSurface

ISurface Class 定义接口,不包含代码:

Option Explicit

Public Sub Create(ByRef SurfaceObject As Object)
End Sub

Public Sub AddLine(ByVal StartX As Double, _
                   ByVal StartY As Double, _
                   ByVal EndX As Double, _
                   ByVal EndY As Double, _
                   Optional ByVal PenColor As Long = vbWhite, _
                   Optional ByVal PenSize As Integer = 1, _
                   Optional ByVal PenStyle As DrawStyleConstants = vbSolid)
End Sub

Public Sub AddCircle(ByVal StartX As Double, _
                     ByVal StartY As Double, _
                     ByVal Radius As Double, _
                     Optional ByVal PenColor As Long = vbWhite, _
                     Optional ByVal PenSize As Integer = 1, _
                     Optional ByVal PenStyle As DrawStyleConstants = vbSolid)
End Sub

CPrinterSurface Class实现接口。这是您添加代码的地方。

Option Explicit

Implements ISurface

Private oPrinter As Printer

Private Sub ISurface_Create(SurfaceObject As Object)
   Set oPrinter = SurfaceObject
End Sub

Private Sub ISurface_AddLine(ByVal StartX As Double, _
                             ByVal StartY As Double, _
                             ByVal EndX As Double, _
                             ByVal EndY As Double, _
                             Optional ByVal PenColor As Long = 16777215, _
                             Optional ByVal PenSize As Integer = 1, _
                             Optional ByVal PenStyle As DrawStyleConstants = 0&)
   oPrinter.DrawWidth = PenSize
   oPrinter.DrawStyle = PenStyle
   oPrinter.ForeColor = PenColor       
   oPrinter.Line (StartX, StartY)-(EndX, EndY), PenColor
End Sub

Private Sub ISurface_AddCircle(ByVal StartX As Double, _
                               ByVal StartY As Double, _
                               ByVal Radius As Double, _
                               Optional ByVal PenColor As Long = 16777215, _
                               Optional ByVal PenSize As Integer = 1, _
                               Optional ByVal PenStyle As DrawStyleConstants = 0&)
   oPrinter.DrawWidth = PenSize
   oPrinter.DrawStyle = PenStyle
   oPrinter.ForeColor = PenColor
   oPrinter.Circle (StartX, StartY), Radius, PenColor
End Sub

CPictureBoxSurface Class也实现了接口

Option Explicit

Implements ISurface

Private oPictureBox As PictureBox

Private Sub ISurface_Create(SurfaceObject As Object)
   Set oPictureBox = SurfaceObject
End Sub

Private Sub ISurface_AddLine(ByVal StartX As Double, _
                             ByVal StartY As Double, _
                             ByVal EndX As Double, _
                             ByVal EndY As Double, _
                             Optional ByVal PenColor As Long = 16777215, _
                             Optional ByVal PenSize As Integer = 1, _
                             Optional ByVal PenStyle As DrawStyleConstants = 0&)
   oPictureBox.DrawWidth = PenSize
   oPictureBox.DrawStyle = PenStyle
   oPictureBox.ForeColor = PenColor       
   oPictureBox.Line (StartX, StartY)-(EndX, EndY), PenColor
End Sub

Private Sub ISurface_AddCircle(ByVal StartX As Double, _
                               ByVal StartY As Double, _
                               ByVal Radius As Double, _
                               Optional ByVal PenColor As Long = 16777215, _
                               Optional ByVal PenSize As Integer = 1, _
                               Optional ByVal PenStyle As DrawStyleConstants = 0&)
   oPictureBox.DrawWidth = PenSize
   oPictureBox.DrawStyle = PenStyle
   oPictureBox.ForeColor = PenColor
   oPictureBox.Circle (StartX, StartY), Radius, PenColor
End Sub

主应用程序。 创建一个 EXE 项目,其中包含一般生成绘图的逻辑。交换表面,一个代码库可以绘制到任何已实现的表面。您的项目树将包含带有 PictureBox 和 Button 的主窗体,以及上面描述的 3 类。这是主窗体的代码:

Option Explicit

Private MySurface As ISurface

Private Sub cmdCreate_Click()
   Set MySurface = New CPictureBoxSurface
   MySurface.Create Picture1
   MySurface.AddCircle 1000, 1000, 500, vbRed
   MySurface.AddCircle 1500, 1500, 500, vbBlue
End Sub

当您有多个表面时,上面提供的代码消除了重复。为了清晰起见并突出显示基本架构,它已被精简。希望您能够为您的应用阐述这些概念。