Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everybody!

Recently developed a graphic control, the base class is panel.

The main attribute is the Cellection graphical object, used a custom designer, each graphic object can specify the background picture, so wrote the control Serializer, each graphic object is Serialized by this Serializer, if the specified image, call MyBase.SerializeToResourceExpression (manager, IMG) of serialization, the picture will be saved in the design form of embedded resources.

Serialization in Form Designer in the code examples are as follows:
CimsDrawing2_Pattern1_2_Ellipse.TextureImage = CType (resources.GetObject ("resource1"), System.Drawing.Bitmap)

But has an error, when this control in other container controls,

for example in the form design, Ctrl + C the controls, and then ctrl+v, then following error code in Form Designer
CimsDrawing3_Pattern1_2_Ellipse.TextureImage = CType (resources.GetObject ("CimsDrawing2.Controls"), System.Windows.Forms.Control)

how to fix this problem?

hi everybody!


Recently developed a graphic control, the base class is panel.

The main attribute is the Cellection graphical object, used a custom designer, each graphic object can specify the background picture, so wrote the control Serializer, each graphic object is Serialized by this Serializer, if the specified image, call MyBase.SerializeToResourceExpression (manager, IMG) of serialization, the picture will be saved in the design form of embedded resources.

Serialization in Form Designer in the code examples are as follows:
CimsDrawing2_Pattern1_2_Ellipse.TextureImage = CType (resources.GetObject ("resource1"), System.Drawing.Bitmap)

But has an error, when this control in other container controls,

for example in the form design, Ctrl + C the controls, and then ctrl+v, then following error code in Form Designer
CimsDrawing3_Pattern1_2_Ellipse.TextureImage = CType (resources.GetObject ("CimsDrawing2.Controls"), System.Windows.Forms.Control)

how to fix this problem?

Public Class CIMSDrawingSerializer
Inherits CodeDomSerializer


Public Overrides Function Deserialize(ByVal manager As IDesignerSerializationManager, ByVal codeObject As Object) As Object
Dim baseClassSerializer As CodeDomSerializer = DirectCast(manager.GetSerializer(GetType(CIMSDrawing).BaseType, _
GetType(CodeDomSerializer)), _
CodeDomSerializer)
Dim obj = baseClassSerializer.Deserialize(manager, codeObject)

Dim rootObj As CIMSDrawing = DirectCast(obj, CIMSDrawing)

Return obj
End Function

Public Overrides Function Serialize(ByVal manager As IDesignerSerializationManager, ByVal value As Object) As Object
Dim baseClassSerializer As CodeDomSerializer = DirectCast(manager.GetSerializer(GetType(CIMSDrawing).BaseType, GetType(CodeDomSerializer)), CodeDomSerializer)

Dim codeObject As Object = baseClassSerializer.Serialize(manager, value)

Dim rootExpression As CodeExpression = Me.GetExpression(manager, value)
Dim statements As CodeStatementCollection

Dim rootObj As CIMSDrawing = DirectCast(value, CIMSDrawing)

If TypeOf codeObject Is CodeStatementCollection Then

statements = DirectCast(codeObject, CodeStatementCollection)

statements.Add(New CodeAssignStatement(New CodePropertyReferenceExpression(rootExpression, "Parent"), New CodeThisReferenceExpression()))

Me.SerializePattern(manager, statements, rootExpression, rootObj)

End If

Return codeObject
End Function

Protected Sub SerializePattern(ByVal manager As IDesignerSerializationManager, ByRef statements As CodeStatementCollection, ByVal rootExpression As CodeExpression, ByVal rootObj As CIMSDrawing)
Dim assignStatement As CodeAssignStatement
Dim objExpression As New List(Of CodeVariableReferenceExpression)
Dim methodExpresttion As CodeMethodInvokeExpression
Dim createStatement As CodeVariableDeclarationStatement
Dim drawingExpress As New CodePropertyReferenceExpression(rootExpression, "Drawing")

Dim pattern As CIMSDrawingPattern
Dim defPattern As New CIMSDrawingPattern
Dim defPatternList As New CIMSDrawingPatternList
Dim objName As String

If rootObj.Drawing Is Nothing OrElse rootObj.Drawing.Count = 0 Then Return

methodExpresttion = New CodeMethodInvokeExpression(rootExpression, "SuspendLayout", New CodeExpression() {})
statements.Add(methodExpresttion)

......

For i As Integer = 0 To rootObj.Drawing.Count - 1
pattern = rootObj.Drawing(i)

objName = rootObj.Name & "_Pattern" & i + 1

createStatement = New CodeVariableDeclarationStatement(GetType(CIMSDrawingPattern), objName, New CodeObjectCreateExpression(GetType(CIMSDrawingPattern), New CodeExpression() {}))
statements.Add(createStatement)

objExpression.Add(New CodeVariableReferenceExpression(objName))

With pattern
......

'background image
If .BackImage IsNot Nothing Then
assignStatement = New CodeAssignStatement(New CodePropertyReferenceExpression(objExpression(i), "BackImage"), GetImageExpression(manager, .BackImage))
statements.Add(assignStatement)
End If

'default line color
If .DefaultLineColor <> defPattern.DefaultLineColor Then
assignStatement = New CodeAssignStatement(New CodePropertyReferenceExpression(objExpression(i), "DefaultLineColor"), Me.GetColorExpression(.DefaultLineColor))
statements.Add(assignStatement)
End If

......

End With
Next

methodExpresttion = New CodeMethodInvokeExpression(New CodePropertyReferenceExpression(rootExpression, "Drawing"), "AddRange", New CodeArrayCreateExpression(GetType(CIMSDrawingPattern), objExpression.ToArray))
statements.Add(methodExpresttion)

If rootObj.DrawIndex >= 1 Then
methodExpresttion = New CodeMethodInvokeExpression(rootExpression, "Refresh", New CodeExpression() {})
statements.Add(methodExpresttion)

methodExpresttion = New CodeMethodInvokeExpression(rootExpression, "AdjustRendering", New CodePrimitiveExpression(rootObj.DrawIndex - 1))
statements.Add(methodExpresttion)
End If

methodExpresttion = New CodeMethodInvokeExpression(rootExpression, "ResumeLayout", New CodeExpression() {New CodePrimitiveExpression(False)})
statements.Add(methodExpresttion)
End Sub

......

Protected Function GetColorExpression(ByVal value As Color) As CodeExpression

Dim expression As CodeExpression = Nothing

If value.IsSystemColor Then

expression = New CodeFieldReferenceExpression(New CodeTypeReferenceExpression(GetType(SystemColors)), value.Name)

ElseIf value.IsNamedColor Then

expression = New CodeFieldReferenceExpression(New CodeTypeReferenceExpression(GetType(Color)), value.Name)

ElseIf value.IsEmpty Then

expression = New CodeFieldReferenceExpression(New CodeTypeReferenceExpression(GetType(Color)), "Empty")

Else

Dim param() As CodeExpression = { _
New CodeCastExpression(GetType(Byte), New CodePrimitiveExpression(value.A)), _
New CodeCastExpression(GetType(Byte), New CodePrimitiveExpression(value.R)), _
New CodeCastExpression(GetType(Byte), New CodePrimitiveExpression(value.G)), _
New CodeCastExpression(GetType(Byte), New CodePrimitiveExpression(value.B)) _
}

expression = New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(GetType(Color)), "FromArgb", param)

End If

Return expression

End Function

Protected Function GetImageExpression(ByVal manager As IDesignerSerializationManager, ByVal img As Image) As CodeExpression
Return MyBase.SerializeToResourceExpression(manager, img)
End Function

.......

End Class
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900