|
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Windows.Forms;
using System.Xml;
namespace automationControls.GradientMaker
{
/// <summary>
/// UserSettings: Serializable class to save/load user setting to/from an xml file.
/// Version: 1.2
/// Company: automationControls
/// Date: Feb 2005
/// Version Date: 10 Oct 2005
/// Author: T.J.M-N
/// </summary>
[Serializable()]
public class UserSettings
{
#region Constructors...
/// <summary>
/// Constructor using the default file name. This is the application path and name and .config.xml
/// </summary>
public UserSettings()
{
// The default file name for xml file is the fill executable path and .config.xml
ConfigFileName = System.Windows.Forms.Application.ExecutablePath + ".config.xml";
}
/// <summary>
/// Constructor supplying the full path file name for the xml file.
/// </summary>
/// <param name="FileName">Full path and file name for the xml file</param>
public UserSettings(string FileName)
{
// The default file name for xml file is the fill executable path and .xml
ConfigFileName = FileName;
}
#endregion
#region Properties...
/// <summary>
/// Full path and file name for the xml file
/// </summary>
protected string ConfigFileName;
/// <summary>
/// File name of the xml file from which to load or save data
/// </summary>
public string FileName
{
get
{
return ConfigFileName;
}
set
{
ConfigFileName = value;
}
}
#endregion
#region Config - Data to save/load...
/// <summary>
/// Type for Image options
/// </summary>
[Serializable()]
public struct TypeImageOptions
{
/// <summary>
/// Height of image
/// </summary>
public decimal Height;
/// <summary>
/// Width of image
/// </summary>
public decimal Width;
/// <summary>
/// Gradient mode
/// </summary>
public int GradientMode;
/// <summary>
/// Angle when in gradient mode angle
/// </summary>
public decimal Angle;
/// <summary>
/// Image Center Gradient Height
/// </summary>
public decimal CenterHeight;
/// <summary>
/// Image Center Gradient Width
/// </summary>
public decimal CenterWidth;
/// <summary>
/// Gradient start color
/// </summary>
public System.Drawing.Color StartColor;
/// <summary>
/// Gradient end color
/// </summary>
public System.Drawing.Color EndColor;
/// <summary>
/// Enable gradient end color
/// </summary>
public bool EndColorEnabled;
}
/// <summary>
/// Type for blend options
/// </summary>
[Serializable()]
public struct TypeBlend
{
/// <summary>
/// Enable blend mode
/// </summary>
public bool Enabled;
/// <summary>
/// Array for blend settings
/// </summary>
public int[] Blend;
}
/// <summary>
/// Type for text options
/// </summary>
[Serializable()]
public struct TypeTextOptions
{
/// <summary>
/// Text
/// </summary>
public string Text;
/// <summary>
/// Text Anti Alias Mode
/// </summary>
public int AntiAliasMode;
/// <summary>
/// Text Alignment
/// </summary>
public int TextAlignment;
/// <summary>
/// Name of the font for the window text
/// </summary>
public string FontName;
/// <summary>
/// Size of the window text
/// </summary>
public float FontSize;
/// <summary>
/// Style (Bold, Underline...) of the window text
/// </summary>
public System.Drawing.FontStyle FontStyle;
/// <summary>
/// Text rotation angle
/// </summary>
public decimal RotationAngle;
/// <summary>
/// Border height
/// </summary>
public decimal BorderHeight;
/// <summary>
/// Border width
/// </summary>
public decimal BorderWidth;
/// <summary>
/// Text Align
/// </summary>
public int TextAlign;
/// <summary>
/// Gradient Mode
/// </summary>
public int GradientMode;
/// <summary>
/// Angle when in gradient mode angle
/// </summary>
public decimal Angle;
/// <summary>
/// Text Center Gradient Height
/// </summary>
public decimal CenterHeight;
/// <summary>
/// Text Center Gradient Width
/// </summary>
public decimal CenterWidth;
/// <summary>
/// Gradient start color
/// </summary>
public System.Drawing.Color StartColor;
/// <summary>
/// Alpha of the text start color
/// </summary>
public decimal StartAlpha;
/// <summary>
/// Gradient end color
/// </summary>
public System.Drawing.Color EndColor;
/// <summary>
/// Enable gradient end color
/// </summary>
public bool EndColorEnabled;
/// <summary>
/// lpha of the text end color
/// </summary>
public decimal EndAlpha;
}
/// <summary>
/// Type for configuration options
/// </summary>
[Serializable()]
public struct TypeConfiguration
{
/// <summary>
/// Image options
/// </summary>
public TypeImageOptions ImageOptions;
/// <summary>
/// Image blend
/// </summary>
public TypeBlend ImageBlend;
/// <summary>
/// Text options
/// </summary>
public TypeTextOptions TextOptions;
/// <summary>
/// Text blend
/// </summary>
public TypeBlend TextBlend;
}
/// <summary>
/// Setting to save or load from the xml file
/// </summary>
public TypeConfiguration Configuration;
#endregion
#region Save and load methods...
/// <summary>
/// Save this object to the xml file set in the property FileName
/// </summary>
public void SaveSoapData()
{
SaveSoapData(FileName, this);
}
/// <summary>
/// Save a Serializable object to the xml file FileName
/// </summary>
/// <param name="FileName">Full file name and path</param>
/// <param name="obj">A Serializable object</param>
public static void SaveSoapData(string FileName, object obj)
{
FileStream configFile = new FileStream(FileName, FileMode.Create);
if (!configFile.CanWrite)
{
throw new FileLoadException("Cannot load xml file to write. File name: " + FileName, FileName);
}
SoapFormatter soapFormat = new SoapFormatter(null, new StreamingContext(StreamingContextStates.File));
soapFormat.Serialize(configFile, obj);
configFile.Close();
}
/// <summary>
/// Load data from the xml file into this object. The xml file set in the property FileName
/// </summary>
public void LoadSoapData()
{
this.Configuration = ((UserSettings)LoadSoapData(FileName)).Configuration;
}
/// <summary>
/// Load data into a Serializable object from the xml file FileName
/// </summary>
/// <param name="FileName">Full file name and path</param>
/// <returns>An object that can be cast to the object loaded</returns>
public static object LoadSoapData(string FileName)
{
FileStream configFile = new FileStream(FileName, FileMode.Open);
if (!configFile.CanRead)
{
throw new FileLoadException("Cannot load xml file to read. File name: " + FileName, FileName);
}
SoapFormatter soapFormat = new SoapFormatter(null, new StreamingContext(StreamingContextStates.File));
object obj;
try
{
obj = soapFormat.Deserialize(configFile);
}
catch (Exception e)
{
obj = null;
throw e;
}
finally
{
configFile.Close();
}
return obj;
}
#endregion
#region Display config file in tree node...
/// <summary>
/// Display the config xml file in a tree
/// </summary>
/// <param name="Tree">A TreeNode which to display the leafs of the xml file</param>
public void DisplayConfigFile(TreeNodeCollection Tree)
{
try
{
XmlDocument configDocument = new XmlDocument();
configDocument.Load(FileName);
DisplayXmlNode(configDocument, Tree);
}
catch {}
}
private void DisplayXmlNode(XmlNode configXmlNode, TreeNodeCollection configTreeNode)
{
TreeNode childNode = configTreeNode.Add(configXmlNode.Name);
switch (configXmlNode.NodeType)
{
case XmlNodeType.Element:
if (configXmlNode.Attributes.Count > 0)
{
TreeNode attrNode = childNode.Nodes.Add("ATTRIBUTES");
foreach (XmlAttribute xmlAttribute in configXmlNode.Attributes)
{
attrNode.Nodes.Add(xmlAttribute.Name + " = " + xmlAttribute.Value);
}
}
break;
case XmlNodeType.Text:
childNode.Text = configXmlNode.Value;
break;
case XmlNodeType.Comment:
childNode.Text = "<!--" + configXmlNode.Value + "-->";
break;
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.XmlDeclaration:
childNode.Text = "<?" + configXmlNode.Name + " " + configXmlNode.Value + "?>";
break;
default:
// Do nothing
break;
}
XmlNode xmlChildren = configXmlNode.FirstChild;
while (xmlChildren != null)
{
DisplayXmlNode(xmlChildren, childNode.Nodes);
xmlChildren = xmlChildren.NextSibling;
}
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.