我遇到了这个确切的问题。我决定通过实现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
当您有多个表面时,上面提供的代码消除了重复。为了清楚起见并突出基本架构,它已被剥离。希望您能够为您的应用程序阐述这些概念。