|
|
|
Select your preferred language |
GraphicsDLL Listing and details.
This DLL is designed to draw a simple shape in a choice of colours and
serialize itself as an XML file.
using
System;
using
System.ComponentModel;
using
System.Drawing;
using
System.IO;
using
System.Xml.Serialization;
namespace
WellFormed
{
[Serializable()]
public class
ColorShape
{
public
enum Shapes
{
Cross,
Circle,
Square
}
Shapes
_shape;
public
Shapes Shape
{
get{return
_shape;}
set{_shape=value;}
}
Color
_color;
[XmlIgnore()]
public
Color Color
{
get{return
_color;}
set{_color=value;}
}
[
Browsable(false),
XmlElement("Color")
]
public
string XMLColor
{
get{
return
string.Format("{0},{1},{2}",_color.R,_color.G,_color.B);
}
set
{
Color
c=Color.Empty;
string[]
sa=value.Split(new
char[]{','});
_color=Color.FromArgb((byte)int.Parse(sa[0]),(byte)int.Parse(sa[1]),(byte)int.Parse(sa[2]));
}
}
public
void Draw(Graphics g, Rectangle r)
{
float
PenWidth=0.1f*(float)Math.Min(r.Width,r.Height);
Pen p=new
Pen(_color,PenWidth>=1 ? PenWidth : 1);
g.Clear(Color.White);
switch(Shape)
{
case
Shapes.Circle:
g.DrawEllipse(p,r.Left+PenWidth,r.Top+PenWidth,r.Width-(2*PenWidth),r.Height-(2*PenWidth));
break;
case
Shapes.Cross:
g.DrawLine(p,r.Left,r.Top,r.Right,r.Bottom);
g.DrawLine(p,r.Right,r.Top,r.Left,r.Bottom);
break;
case
Shapes.Square:
g.DrawRectangle(p,r.Left+PenWidth,r.Top+PenWidth,r.Width-(2*PenWidth),r.Height-(2*PenWidth));
break;
}
}
public
void Save(string
filename)
{
XmlSerializer
s=new XmlSerializer(typeof(ColorShape));
FileStream
fs=new FileStream(filename,FileMode.Create);
s.Serialize(fs,this);
fs.Close();
}
public
static ColorShape Load(string
filename)
{
XmlSerializer
s = new XmlSerializer(typeof(ColorShape));
FileStream
fs=new FileStream(filename,FileMode.Open);
ColorShape
cs=(ColorShape)s.Deserialize(fs);
fs.Close();
return
cs;
}
public
ColorShape()
{
}
}
}
This DLL is
strongly named and may be installed in the GAC.
To strongly name a
file, generate a strong name keyfile with the SN tool in the directory that
contains the source code. Open a command prompt, find the directory and type;
sn -k
GraphicsDLL.snk
A key file will be
created and you can use it at compile time by modifying the AssemblyInfo file as
shown in the listing below. Check out the last few lines.
using System.Reflection;
using System.Runtime.CompilerServices;
//
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
//
[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
//
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.1")]
//
// In order to sign your assembly you must specify a key to use. Refer to the
// Microsoft .NET Framework documentation for more information on assembly signing.
//
// Use the attributes below to control which key is used for signing.
//
// Notes:
// (*) If no key is specified, the assembly is not signed.
// (*) KeyName refers to a key that has been installed in the Crypto Service
// Provider (CSP) on your machine. KeyFile refers to a file which contains
// a key.
// (*) If the KeyFile and the KeyName values are both specified, the
// following processing occurs:
// (1) If the KeyName can be found in the CSP, that key is used.
// (2) If the KeyName does not exist and the KeyFile does exist, the key
// in the KeyFile is installed into the CSP and used.
// (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility.
// When specifying the KeyFile, the location of the KeyFile should be
// relative to the project output directory which is
// %Project Directory%\obj\<configuration>. For example, if your KeyFile is
// located in the project directory, you would specify the AssemblyKeyFile
// attribute as [assembly: AssemblyKeyFile("..\\..\\mykey.snk")]
// (*) Delay Signing is an advanced option - see the Microsoft .NET Framework
// documentation for more information on this.
//
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile("..\\..\\GraphicsDLL.snk")]
[assembly: AssemblyKeyName("")]
Use your Back button to return to the article.
Imports
System.Xml.Serialization
Imports
System.ComponentModel
Imports
System.Drawing
Imports
System.IO
<Serializable()> _
Public
Class ColorShape
Public Enum
Shapes
Cross
Circle
Square
End Enum
Dim _shape As
Shapes
Public Property
Shape() As Shapes
Get
Return
_shape
End
Get
Set(ByVal
Value As Shapes)
_shape
= Value
End
Set
End Property
Dim _color As
Color
<XmlIgnore()>
_
Public Property
Color() As Color
Get
Return
_color
End
Get
Set(ByVal
Value As Color)
_color
= Value
End
Set
End Property
< _
Browsable(False),
_
XmlElement("Color") _
> _
Public Property
XMLColor() As String
Get
Return
String.Format("{0},{1},{2}", _color.R, _color.G,
_color.B)
End
Get
Set(ByVal
Value As String)
Dim
c As Color = Color.Empty
Dim
sa() As String =
Value.Split(New Char()
{","c})
_color
= Color.FromArgb(Integer.Parse(sa(0)),
Integer.Parse(sa(1)),
Integer.Parse(sa(2)))
End
Set
End Property
Public Sub Draw(ByVal
g As Graphics, ByVal
r As Rectangle)
Dim
PenWidth As Single
= CSng(0.1F * Math.Min(r.Width, r.Height))
Dim
p As Pen = New
Pen(_color, IIf(PenWidth >= 1, PenWidth, 1))
g.Clear(Color.White)
Select
Case Shape
Case
Shapes.Circle
g.DrawEllipse(p, r.Left + PenWidth, r.Top + PenWidth, r.Width - (2 *
PenWidth), r.Height - (2 * PenWidth))
Exit
Sub
Case
Shapes.Cross
g.DrawLine(p, r.Left, r.Top, r.Right, r.Bottom)
g.DrawLine(p, r.Right, r.Top, r.Left, r.Bottom)
Exit
Sub
Case
Shapes.Square
g.DrawRectangle(p,
r.Left + PenWidth, r.Top + PenWidth, r.Width - (2 * PenWidth), r.Height - (2 *
PenWidth))
Exit
Sub
End
Select
End Sub
Public Sub Save(ByVal
filename As String)
Dim
s As XmlSerializer =
New XmlSerializer(GetType(ColorShape))
Dim
fs As FileStream = New
FileStream(filename, FileMode.Create)
s.Serialize(fs,
Me)
fs.Close()
End Sub
Public Shared
Function Load(ByVal
filename As String)
Dim
s As XmlSerializer =
New XmlSerializer(GetType(ColorShape))
Dim
fs As FileStream = New
FileStream(filename, FileMode.Open)
Dim
cs As ColorShape =
CType(s.Deserialize(fs), ColorShape)
fs.Close()
Return
cs
End Function
Public Sub
New()
End Sub
End
Class
Use your Back button to return to the article.