//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
-----------------------

 WinFormStudy
{
    class DrawStylePicture
    {
        
public static void DrawFormBackgroundImage(Object obj, string strPicName)
        {
            Form frm 
= (Form)obj;
            
string strStyleName = AppConfigXMLManage.GetAppConfig("CurrentStyle");
            Assembly assem 
= Assembly.GetExecutingAssembly();
            System.IO.Stream stream 
= assem.GetManifestResourceStream("WinFormStudy.StyleImage." + strStyleName + "." + strPicName);
            Image image 
= Bitmap.FromStream(stream);
            frm.BackgroundImage 
= image;
            frm.BackgroundImageLayout 
= ImageLayout.Stretch;
        }

        
public static void DrawPanelBackgroundImage(Object obj, string strPicName)
        {
            Panel pnl 
= (Panel)obj;
            
string strStyleName = AppConfigXMLManage.GetAppConfig("CurrentStyle");
            Assembly assem 
= Assembly.GetExecutingAssembly();
            System.IO.Stream stream 
= assem.GetManifestResourceStream("WinFormStudy.StyleImage." + strStyleName + "." + strPicName);
            Image image 
= Bitmap.FromStream(stream);
            pnl.BackgroundImage 
= image;
            pnl.BackgroundImageLayout 
= ImageLayout.Stretch;
        }

        
public static void DrawButtonBackgroundImage(Object obj, string strPicName)
        {
            Button btn 
= (Button)obj;
            
string strStyleName = AppConfigXMLManage.GetAppConfig("CurrentStyle");
            Assembly assem 
= Assembly.GetExecutingAssembly();
            System.IO.Stream stream 
= assem.GetManifestResourceStream("WinFormStudy.StyleImage." + strStyleName + "." + strPicName);
            Image image 
= Bitmap.FromStream(stream);
            btn.BackgroundImage 
= image;
            btn.BackgroundImageLayout 
= ImageLayout.Stretch;
        }
    }
}

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

 WinFormStudy
{
    class AppConfigXMLManage
    {
        
public static string GetAppConfig(string strKey)
        {
            
//return System.Configuration.ConfigurationManager.AppSettings[strKey];
            XmlDocument doc = new XmlDocument();
            
try
            {
                doc.Load(Application.ExecutablePath 
+ ".config");
                XmlNode node 
= doc.SelectSingleNode(@"//add[@key='" + strKey + "']");
                XmlElement ele 
= (XmlElement)node;
                
return ele.GetAttribute("value");
            }
            
catch
            {
                
return string.Empty;
            }
        }

        
public static bool UpdateAppConfig(string strKey, string strValue)
        {
            XmlDocument doc 
= new XmlDocument();
            
try
            {
                doc.Load(Application.ExecutablePath 
+ ".config");
                XmlNode node 
= doc.SelectSingleNode(@"//add[@key='" + strKey + "']");
                XmlElement ele 
= (XmlElement)node;
                ele.SetAttribute(
"value", strValue);
                doc.Save(Application.ExecutablePath 
+ ".config");
            }
            
catch
            {
                
return false;
            }
            
return true;
        }
    }
}

相关文章:

  • 2022-02-25
  • 2022-12-23
  • 2022-12-23
  • 2021-11-11
  • 2021-12-08
  • 2021-09-20
  • 2022-12-23
  • 2021-11-10
猜你喜欢
  • 2022-01-03
  • 2022-12-23
  • 2021-11-06
  • 2022-02-02
  • 2021-10-10
相关资源
相似解决方案