Implementing CloneCore: Deep copy in VG.net

Code samples and article discussion

Moderators: Frank Hileman, Anne Szyjan

Implementing CloneCore: Deep copy in VG.net

Postby Frank Hileman » Sat Jan 14, 2006 11:56 am

A client asked us how to implement CloneCore in Visual Basic. There are two basic techniques to implementing the copy constructor required by CloneCore. I have documented the techniques below, in this small sample sub-Picture. It was actually created in VS 2005.

Thanks Eric! :)

Code: Select all
' Comment out the line below to use the other technique, explicit copy
#Const BASE_COPY = True

Imports System.ComponentModel
Imports Prodige.Drawing
Imports Prodige.Drawing.Styles

Public Class Square
    Inherits Prodige.Drawing.Picture

    Friend WithEvents Rectangle1 As Prodige.Drawing.Rectangle

    Private mRollStyle As LinearGradientFill

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        ' NOTE: in general, it is not a good technique to start animation in the constructor,
        ' as that means it will be animating in the designer as well. It is better to use
        ' an explicit method call to trigger the animation, used only at run-time.
        AddHandler Display.Updating, AddressOf Me.Display_Updating
    End Sub

    Public Sub Display_Updating(ByVal sender As Object, ByVal e As EventArgs)
        Try
            mRollStyle = CType(Me.Styles("BlueRotate").Fill, LinearGradientFill)
            mRollStyle.Angle = (mRollStyle.Angle + 1)
        Catch ex As Exception
            RemoveHandler Display.Updating, AddressOf Me.Display_Updating
        End Try
    End Sub

    ' NOTE: for CloneCore to work, we need a copy constructor
    Protected Sub New(ByVal original As Square)
#If BASE_COPY Then
        ' NOTE: There are two ways to do this. If you want all the current settings
        ' in the base class you can call the base class copy constructor. This will copy both
        ' styles and elements. However you need to fix up any fields in this derived class.
        MyBase.New(original)
        ' the following statement only works if SerializeNames is set to true.
        Rectangle1 = CType(Elements("Rectangle1"), Rectangle)
        AddHandler Display.Updating, AddressOf Me.Display_Updating
#Else
        ' NOTE: an alternative way to do this is to call the normal New(), then copy over
        ' explicitly information you think should be copied. The first technique is more
        ' likely to capture all information that needs to be copied.
        Me.New()
        mRollStyle = CType(Me.Styles("BlueRotate").Fill, LinearGradientFill)
        mRollStyle.Angle = original.mRollStyle.Angle
#End If
    End Sub

    Protected Overrides Function CloneCore() As Element
        Return New Square(Me)
    End Function

#Region "Designer generated code "

    'NOTE: The following procedure is required by the Designer.
    'Do not modify it using the code editor.
    Private Sub InitializeComponent()
        Dim LinearGradientFill1 As Prodige.Drawing.Styles.LinearGradientFill = New Prodige.Drawing.Styles.LinearGradientFill(New Prodige.Drawing.Vector(0.5!, 0.5!), New Prodige.Drawing.Vector(1.0!, 0.5!), Prodige.Drawing.Styles.FillCoordinateType.Relative, Prodige.Drawing.Styles.GradientType.TwoColor)
        Me.Rectangle1 = New Prodige.Drawing.Rectangle
        CType(Me, System.ComponentModel.ISupportInitialize).BeginInit()
        '
        'Rectangle1
        '
        Me.Rectangle1.DrawAction = Prodige.Drawing.ShapeDrawAction.Fill
        Me.Rectangle1.Location = New Prodige.Drawing.Vector(0.0!, 0.0!)
        Me.Rectangle1.Name = "Rectangle1"
        Me.Rectangle1.Size = New Prodige.Drawing.FSize(50.0!, 50.0!)
        Me.Rectangle1.Style = "BlueRotate"
        '
        'Square
        '
        Me.Elements.AddRange(New Prodige.Drawing.Element() {Me.Rectangle1})
        Me.Name = "Square"
        LinearGradientFill1.EndColor = System.Drawing.Color.FromArgb(CType(CType(0, Byte), Integer), CType(CType(0, Byte), Integer), CType(CType(255, Byte), Integer))
        LinearGradientFill1.StartColor = System.Drawing.Color.FromArgb(CType(CType(0, Byte), Integer), CType(CType(0, Byte), Integer), CType(CType(0, Byte), Integer), CType(CType(255, Byte), Integer))
        LinearGradientFill1.StartOpacity = 0.0!
        Me.Styles.Add(New Prodige.Drawing.Styles.Style("BlueRotate", Nothing, LinearGradientFill1, Nothing, Nothing))
        CType(Me, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub

#End Region
End Class
User avatar
Frank Hileman
Site Admin
 
Posts: 1399
Joined: Sun Jul 25, 2004 8:16 pm
Location: California

Return to Samples

Who is online

Users browsing this forum: No registered users and 2 guests

cron