C# WinForm (笨方法)根据不同的样式配置 设置窗体相关控件的背景 以改变窗体风格

//1. 项目下增加相关图片文件夹
------------------------------
--项目WinFormStudy
--窗体LoginForm.cs
--窗体MainForm.cs
--文件夹StyleImage
--子文件夹StyleA
--相关图片btnAddUser.JPG及其他
(将图片做为 嵌入的资源 进行生成)
--子文件夹StyleB
--相关图片btnAddUser.JPG及其他

//2. App.config中保存当前窗体的风格
------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="CurrentStyle" value="StyleB"/>
</appSettings>
...
</configuration>

//3. 窗体调用
----------------
DrawStylePicture.DrawButtonBackgroundImage(this.btnAddUser, "btnAddUser.JPG");


//4. DrawStylePicture
-----------------------

Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Reflection;
usingSystem.Drawing;
usingSystem.IO;

namespaceWinFormStudy
{
classDrawStylePicture
{
publicstaticvoidDrawFormBackgroundImage(Objectobj,stringstrPicName)
{
Formfrm
=(Form)obj;
stringstrStyleName=AppConfigXMLManage.GetAppConfig("CurrentStyle");
Assemblyassem
=Assembly.GetExecutingAssembly();
System.IO.Streamstream
=assem.GetManifestResourceStream("WinFormStudy.StyleImage."+strStyleName+"."+strPicName);
Imageimage
=Bitmap.FromStream(stream);
frm.BackgroundImage
=image;
frm.BackgroundImageLayout
=ImageLayout.Stretch;
}

publicstaticvoidDrawPanelBackgroundImage(Objectobj,stringstrPicName)
{
Panelpnl
=(Panel)obj;
stringstrStyleName=AppConfigXMLManage.GetAppConfig("CurrentStyle");
Assemblyassem
=Assembly.GetExecutingAssembly();
System.IO.Streamstream
=assem.GetManifestResourceStream("WinFormStudy.StyleImage."+strStyleName+"."+strPicName);
Imageimage
=Bitmap.FromStream(stream);
pnl.BackgroundImage
=image;
pnl.BackgroundImageLayout
=ImageLayout.Stretch;
}

publicstaticvoidDrawButtonBackgroundImage(Objectobj,stringstrPicName)
{
Buttonbtn
=(Button)obj;
stringstrStyleName=AppConfigXMLManage.GetAppConfig("CurrentStyle");
Assemblyassem
=Assembly.GetExecutingAssembly();
System.IO.Streamstream
=assem.GetManifestResourceStream("WinFormStudy.StyleImage."+strStyleName+"."+strPicName);
Imageimage
=Bitmap.FromStream(stream);
btn.BackgroundImage
=image;
btn.BackgroundImageLayout
=ImageLayout.Stretch;
}
}
}

//5. AppConfigXMLManage
-------------------------

Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Xml;
usingSystem.Windows.Forms;

namespaceWinFormStudy
{
classAppConfigXMLManage
{
publicstaticstringGetAppConfig(stringstrKey)
{
//returnSystem.Configuration.ConfigurationManager.AppSettings[strKey];
XmlDocumentdoc=newXmlDocument();
try
{
doc.Load(Application.ExecutablePath
+".config");
XmlNodenode
=doc.SelectSingleNode(@"//add[@key='"+strKey+"']");
XmlElementele
=(XmlElement)node;
returnele.GetAttribute("value");
}
catch
{
returnstring.Empty;
}
}

publicstaticboolUpdateAppConfig(stringstrKey,stringstrValue)
{
XmlDocumentdoc
=newXmlDocument();
try
{
doc.Load(Application.ExecutablePath
+".config");
XmlNodenode
=doc.SelectSingleNode(@"//add[@key='"+strKey+"']");
XmlElementele
=(XmlElement)node;
ele.SetAttribute(
"value",strValue);
doc.Save(Application.ExecutablePath
+".config");
}
catch
{
returnfalse;
}
returntrue;
}
}
}

你可能感兴趣的:(WinForm)