【问题标题】:duplication avoidance when writing to picturebox and printer写入图片框和打印机时避免重复
【发布时间】:2020-04-08 04:55:35
【问题描述】:

所以偶尔用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

应该有一个别名来使用控件/对象。 提前致谢

【问题讨论】:

    标签: printing vb6 picturebox


    【解决方案1】:

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

    • I表面
    • CPrinterSurface
    • CPictureBoxSurface

    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
    

    主应用程序。创建一个 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
    

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

    【讨论】:

    • 我将在一个新项目中尝试一下,看看它是如何工作的。那么我可以添加更多功能吗?我正在尝试制作一个程序来呈现 2 种尺寸和某些类型的日历。我基本上使用图像作为标志,文本作为年、月、日、线条。轮廓。到目前为止,该程序有效,但我仍在消除错误并进行改进。我想避免打印图片框中的内容,因为结果很糟糕,如果打印到 pdf 打印机,直接打印文本可以选择文本。
    • 是的,当然。您可以添加任意数量的功能。只需按照我介绍的模式即可。我的完整应用程序有十几个或更多功能,包括 AddText、AddPicture、AddRectangle 等。您甚至可以实现其他表面,例如 PDF、AutoCAD 或 XML。这个问题几乎正是我几年前遇到的问题,对我来说效果很好。
    • @HugoSimões 我稍微简化了我的原始帖子,然后尝试运行它。它运行良好。您收到的确切消息是什么?
    • @HugoSimões 你没有一个名为 ISurface 的类。您有一个名为 Class1 的类保存到名为 ISurface 的文件中。重命名所有 3 个类,你应该很高兴。
    • @HugoSimões 很高兴为您提供帮助,也祝您新年快乐!如果此答案有帮助,请考虑通过单击复选标记接受它并通过单击向上箭头进行投票。这向更广泛的社区表明您已经找到了解决方案。没有义务这样做。
    猜你喜欢
    • 1970-01-01
    • 2014-11-11
    • 2019-12-27
    • 2010-12-12
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多