避免写入图片盒和打印机时的重复操作

问题描述 投票:3回答:1

因此偶尔在VB6中编程,但我从未解决过此问题。

我有一个私人小组可以在图片框上绘制很多东西,例如线条,文字,图片。大量的代码行。但是,然后我想使用相同的线条绘制到打印机对象。但是我不知道该怎么做。

例如:

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

应该有一个别名才能使用控件/对象。在此先感谢

printing vb6 picturebox
1个回答
2
投票

您需要提取PictureBox和打印机以及您要使用的任何其他表面的细节。我使用的一种方法是实现Interfaces。您将获得4个课程:

  • ISurface
  • CPrinterSurface
  • CPictureBoxSurface
  • CDraw

ISurface类定义接口,不包含任何代码:

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类实现接口。这是您添加代码的地方。

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类也实现了该接口。

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

CDraw类将包含一般生成图形的逻辑。交换表面,一个代码库可以绘制到任何已实现的表面。

Option Explicit

Private MySurface As ISurface

Private Sub Class_Initialize()
   'somewhere you need a mechanism to create the right surface
   Set MySurface = New CPrinterSurface
   MySurface.Create Printer
End Sub

Public Sub DrawStuff()
   MySurface.AddLine 30, 710, 580, 710
   MySurface.AddCircle 50, 50, 10
End Sub

上面显示的代码在有多个曲面时可以消除重复。为了清楚起见并突出显示基本体系结构,已对其进行了简化。希望您能够为您的应用程序阐述这些概念。

© www.soinside.com 2019 - 2024. All rights reserved.