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

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Drawing;
using System.IO;

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

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace 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;
        }
    }
}

转载于:https://www.cnblogs.com/freeliver54/archive/2008/11/24/1339755.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值