gpt4 book ai didi

printing - 写入图片盒和打印机时避免重复

转载 作者:行者123 更新时间:2023-12-02 22:02:21 27 4
gpt4 key购买 nike

所以偶尔用VB6编程,但我从未解决过这个问题。

我有一个私有(private)子可以在图片框中绘制很多东西,例如线条、文本、图片。很多很多行代码。但后来我想使用相同的线条来绘制打印机对象。但我不知道该怎么做。

例如:

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

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

最佳答案

我就遇到过这个问题。我决定通过实现接口(interface)来抽象掉 PictureBox、打印机或任何其他表面的细节。您最终将获得 3 门类(class):

  • 表面
  • CPrinterSurface
  • CPictureBoxSurface

ISurface 类定义接口(interface)并且不包含任何代码:

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 类实现该接口(interface)。您可以在此处添加代码。

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

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

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

关于printing - 写入图片盒和打印机时避免重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59344574/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com