[C#]變更 Windows 7 佈景主題

  • 6323
  • 0
  • 2011-12-01

[C#]變更 Windows 7 佈景主題

 

一、問題描述

如何撰寫程式改變 WIndows 7 佈景主題

 

二、方法

1. Windows 7 佈景主題在 C:\Windows\Resources\Themes

2. 透過以下指令叫用個人化進行變更

rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,@Themes /Action:OpenTheme /file:"C:\Windows\Resources\Themes\architecture.theme"

 

撰寫程式,顯示佈景主題資料夾中的 theme 檔案,然後透過 cmd 執行指令進行變更即可,參考以下程式碼


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace wfAppChangeTheme
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DirectoryInfo dInfo = new DirectoryInfo(@"C:\Windows\Resources\Themes\");
            foreach (var f in dInfo.GetFiles("*.theme"))
            {
                cmbThemes.Items.Add(f.FullName);
            }
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            if (cmbThemes.SelectedIndex == -1)
            {
                return;
            }

            // string sFile = @"C:\Windows\Resources\Themes\architecture.theme";
            string sFile = cmbThemes.SelectedItem.ToString();
            string sCmd = string.Format(@"
rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,@Themes /Action:OpenTheme /file:""{0}""", sFile);

            Process cmd = new Process();
            cmd.StartInfo.FileName = "cmd.exe";
            cmd.StartInfo.RedirectStandardInput = true;
            cmd.StartInfo.RedirectStandardOutput = true;
            cmd.StartInfo.CreateNoWindow = true;
            cmd.StartInfo.UseShellExecute = false;
            cmd.Start();
            cmd.StandardInput.WriteLine(sCmd);
            cmd.StandardInput.Flush();
            cmd.StandardInput.Close();
        }
    }
}

 

三、程式執行結果

 

四、範例下載

wfAppChangeTheme.zip

 

五、相關參考與連結

How do I change the current windows theme programatically?