Put Winform on a CSS coat

Source: Internet
Author: User
Changing the appearance of Winform at runtime allows Winform to customize its appearance like Asp. Net through configuration files similar to CSS.
Of course, the configuration file is in XML format rather than CSS format.
How can we achieve this?
There are three steps in total. Let's take one step.
Step 1: Create an object to store the appearance attributes. Here I define it as Style. cs.

Protected Color m_TextBackColor = Color. Yellow;

Protected Color m_TextForeColor = Color. Blue;

Protected Font m_TextFont = new System. drawing. font ("", 12F, System. drawing. fontStyle. regular, System. drawing. graphicsUnit. point, (bytes) (128 )));

Public string XMLTextBackColor
{
Get {return cc. ConvertToString (m_TextBackColor );}
Set {m_TextBackColor = (Color) cc. ConvertFromString (value );}
}

Public string XMLTextForeColor
{
Get {return cc. ConvertToString (m_TextForeColor );}
Set {m_TextForeColor = (Color) cc. ConvertFromString (value );}
}

Public string XMLTextFont
{
Get {return fc. ConvertToString (m_TextFont );}
Set {m_TextFont = (Font) fc. ConvertFromString (value );}
}

[XmlIgnore]
Public Color TextBackColor
{
Get {return m_TextBackColor ;}
Set {m_TextBackColor = value ;}
}

[XmlIgnore]
Public Color TextForeColor
{
Get {return m_TextForeColor ;}
Set {m_TextForeColor = value ;}
}


[XmlIgnore]
Public Font TextFont
{
Get {return m_TextFont ;}
Set {m_TextFont = value ;}
}

This class is similar to some of these colors and fonts. Note that there are two types of attributes in this class. One type (beginning with XML) is associated with the XML configuration file, the other is to prevent the default value from being defined in the configuration file.

Step 2: Add a BindingSource control on the form and follow the settings.
After DataSource is set, what we need to do at last is to truly associate these two items with the configuration file we call.

Step 3: generate an XML file and use this file.
Some classes in the. Net System. Xml. Serialization space can help us.
We need to read and write configuration files, serialization, and deserialization Style classes, so we need to use XmlTextReader and XmlSerializer classes. XmlTextReader xr = null;
Xr = new XmlTextReader (filePath );
Xr. WhitespaceHandling = WhitespaceHandling. All;
XmlSerializer serializer = new XmlSerializer (typeof (Style ));
M_Style = (Style) serializer. Deserialize (xr );
Return m_Style;

XmlSerializer serializer = null;
FileStream ioStream = null;
Style style = new Style ();
Serializer = new XmlSerializer (typeof (Style ));
IoStream = new FileStream (filePath, FileMode. Create );
Serializer. Serialize (ioStream, style );

The preceding operations read the configuration file and deserialize and serialize the configuration file to the configuration file.
In this way, we can use the configuration file.
You only need to set the DataSource of BindingSource to an instance of the class deserialization using the XML file in the form constructor.
Complete code: http://files.cnblogs.com/game-over/StyleForm.zip
Last Post:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.