34

ユーザーが現在のユーザーテーマをAeroとWindowsClassic(1)の間で切り替えられるようにしたい。これをプログラムで実行できる方法はありますか?

「表示プロパティ」をポップアップしたくないので、レジストリを変更するだけでは疑わしいです。(これには、変更を有効にするためにログアウトしてから再度ログインする必要があります)。

( Codejockライブラリを使用した)アプリケーションスキニングも機能しません。

これを行う方法はありますか?

アプリケーションは、 RDPを介したWindowsServer2008でホスト/実行されます

(1)問題のアプリケーションは、ホストされている「リモートアプリ」であり、表示されているアプリケーションの外観をデスクトップに合わせて変更できるようにしたいと考えています。

4

12 に答える 12

68

次のコマンドを使用して設定できます。

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

警告は、テーマ セレクタ ダイアログが表示されることです。そのダイアログをすぐに殺すことができます。

于 2011-02-03T03:11:07.983 に答える
20

現在のテーマをプログラムで変更したいのには、確かに十分な理由があります。たとえば、自動化されたテスト ツールは、アプリケーションがすべてのテーマで正しく動作することを確認するために、さまざまなテーマを切り替える必要がある場合があります。

.themeユーザーは、Windwos Explorer でファイルをダブルクリックし、表示されるコントロール パネル アプレットを閉じることで、テーマを変更できます。コードから同じことを簡単に行うことができます。以下の手順は私にとってはうまくいきます。Windows 7でのみテストしました。

  1. SHGetKnownFolderPath()ユーザーの「ローカル AppData」フォルダーを取得するために使用します。テーマ ファイルは、Microsoft\Windows\Themesサブフォルダーに格納されます。そこに保存されているテーマ ファイルは直接適用されますが、別の場所に保存されているテーマ ファイルは実行時に複製されます。そのため、そのフォルダーのファイルのみを使用することをお勧めします。
  2. を使用して、手順 1 でShellExecute()見つけたファイルを実行します。.theme
  3. テーマが適用されるまで待ちます。アプリを 2 秒間スリープさせるだけです。
  4. 呼び出しFindWindow('CabinetWClass', 'Personalization')て、テーマが適用されたときにポップアップしたコントロール パネル ウィンドウのハンドルを取得します。"Personalization" キャプションは、米国英語以外のバージョンの Windows では異なる可能性があります。
  5. PostMessage(HWND, WM_CLOSE, 0, 0)コントロール パネル ウィンドウを閉じるために呼び出します。

これは非常にエレガントなソリューションではありませんが、機能します。

于 2011-03-20T08:45:15.920 に答える
12

これが古いチケットであることは知っていますが、誰かが今日これを行う方法を私に尋ねました。上記の Mike の投稿から始めて、整理し、コメントを追加して、完全な C# コンソール アプリ コードを投稿します。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Win32;

namespace Windows7Basic
{
    class Theming
    {
        /// Handles to Win 32 API
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string sClassName, string sAppName);
        [DllImport("user32.dll")]
        private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        /// Windows Constants
        private const uint WM_CLOSE = 0x10;

        private String StartProcessAndWait(string filename, string arguments, int seconds, ref Boolean bExited)
        {
            String msg = String.Empty;
            Process p = new Process();
            p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
            p.StartInfo.FileName = filename;
            p.StartInfo.Arguments = arguments;
            p.Start();

            bExited = false;
            int counter = 0;
            /// give it "seconds" seconds to run
            while (!bExited && counter < seconds)
            {
                bExited = p.HasExited;
                counter++;
                System.Threading.Thread.Sleep(1000);
            }//while
            if (counter == seconds)
            {
                msg = "Program did not close in expected time.";
            }//if

            return msg;
        }

        public Boolean SwitchTheme(string themePath)
        {
            try
            {    
                //String themePath = System.Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\Resources\Ease of Access Themes\basic.theme";
                /// Set the theme
                Boolean bExited = false;
                /// essentially runs the command line:  rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,@Themes /Action:OpenTheme /file:"%WINDIR%\Resources\Ease of Access Themes\classic.theme"
                String ThemeOutput = this.StartProcessAndWait("rundll32.exe", System.Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\shell32.dll,Control_RunDLL " + System.Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\desk.cpl desk,@Themes /Action:OpenTheme /file:\"" + themePath + "\"", 30, ref bExited);

                Console.WriteLine(ThemeOutput);

                /// Wait for the theme to be set
                System.Threading.Thread.Sleep(1000);

                /// Close the Theme UI Window
                IntPtr hWndTheming = FindWindow("CabinetWClass", null);
                SendMessage(hWndTheming, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
            }//try
            catch (Exception ex)
            {
                Console.WriteLine("An exception occured while setting the theme: " + ex.Message);

                return false;
            }//catch
            return true;
        }

        public Boolean SwitchToClassicTheme()
        {
            return SwitchTheme(System.Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\Resources\Ease of Access Themes\basic.theme");
        }

        public Boolean SwitchToAeroTheme()
        {
            return SwitchTheme(System.Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\Resources\Themes\aero.theme");
        }

        public string GetTheme()
        {
            string RegistryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes";
            string theme;
            theme = (string)Registry.GetValue(RegistryKey, "CurrentTheme", string.Empty);
            theme = theme.Split('\\').Last().Split('.').First().ToString();
            return theme;
        }

        // end of object Theming
    }

    //---------------------------------------------------------------------------------------------------------------

    class Program
    {
        [DllImport("dwmapi.dll")]
        public static extern IntPtr DwmIsCompositionEnabled(out bool pfEnabled);

        /// ;RunProgram("%USERPROFILE%\AppData\Local\Microsoft\Windows\Themes\themeName.theme")      ;For User Themes
        /// RunProgram("%WINDIR%\Resources\Ease of Access Themes\classic.theme")                     ;For Basic Themes
        /// ;RunProgram("%WINDIR%\Resources\Themes\aero.theme")                                      ;For Aero Themes

        static void Main(string[] args)
        {
            bool aeroEnabled = false;
            Theming thm = new Theming();
            Console.WriteLine("The current theme is " + thm.GetTheme());

            /// The only real difference between Aero and Basic theme is Composition=0 in the [VisualStyles] in Basic (line omitted in Aero)
            /// So test if Composition is enabled
            DwmIsCompositionEnabled(out aeroEnabled);

            if (args.Length == 0 || (args.Length > 0 && args[0].ToLower(CultureInfo.InvariantCulture).Equals("basic")))
            {
                if (aeroEnabled)
                {
                    Console.WriteLine("Setting to basic...");
                    thm.SwitchToClassicTheme();
                }//if
            }//if
            else if (args.Length > 0 || args[0].ToLower(CultureInfo.InvariantCulture).Equals("aero"))
            {
                if (!aeroEnabled)
                {
                    Console.WriteLine("Setting to aero...");
                    thm.SwitchToAeroTheme();
                }//if
            }//else if
        }

        // end of object Program
    }
}

于 2014-04-21T20:16:03.243 に答える
2

「JanGoyvaerts」の投稿に加えて:PostMessageの代わりにSendMessageを使用しています。違いは、SendMessageがコマンドがウィンドウに取り込まれるのを待つことです。SendMessagesが戻るということは、テーマダイアログが閉じていることを意味します。

したがって、「キャンベル」によって提案された巨大な(しかし天才的な)rundll32.exeメソッドで開始した場合。WM_CLOSEを送信する前に1秒待つ必要があります。そうしないと、テーマが設定されず、アプリケーションがすぐに閉じます。

以下のコードスニペットは、リソース(テーマパック)からファイルを抽出します。次に、rundll32.exeを使用してdesk.cplを実行し、3秒間待機してから、WM_CLOSE(0x0010)を送信し、コマンドが処理されるのを待ちます(テーマの設定にかかる時間)。

    private Boolean SwitchToClassicTheme()
    {
        //First unpack the theme
        try
        {
            //Extract the theme from the resource
            String ThemePath = System.Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\Resources\Themes\ClassicTheme.themepack";
            //WriteFileToCurrentDirectory("ClassicTheme.theme", TabletConfigurator.Resources.ClassicTheme);
            if(File.Exists(ThemePath))
            {
                File.Delete(ThemePath);
            }
            if(File.Exists(ThemePath))
            {
                throw new Exception("The file '" + ThemePath + "' exists and can not be deleted. You can try to delete it manually.");
            }
            using (BinaryWriter sw = new BinaryWriter(new FileStream(ThemePath, FileMode.OpenOrCreate)))
            {
                sw.Write(TabletConfigurator.Resources.ClassicTheme);
                sw.Flush();
                sw.Close();
            }

            if(!File.Exists(ThemePath))
            {
                throw new Exception("The resource theme file could not be extracted");
            }

            //Set the theme file as like a user would have clicked it
            Boolean bTimedOut = false;
            String ThemeOutput = StartProcessAndWait("rundll32.exe", System.Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\shell32.dll,Control_RunDLL " + System.Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\desk.cpl desk,@Themes /Action:OpenTheme /file:\"" + ThemePath + "\"", ref bTimedOut);

            System.Threading.Thread.Sleep(3000);
            //Wait for the theme to be set
            IntPtr hWndTheming = FindWindow("CabinetWClass", null);
            SendMessage(hWndTheming, (uint)WM_CLOSE, 0, 0);

            //using (Bitmap bm = CaptureScreenShot())
            //{
            //    Boolean PixelIsGray = true;
            //    while (PixelIsGray)
            //    {
            //        System.Drawing.Color pixel = bm.GetPixel(0, 0)
            //    }
            //}

        }
        catch(Exception ex)
        {
            ShowError("An exception occured while setting the theme: " + ex.Message);
            return false;
        }
        return true;
    }
于 2012-09-17T10:43:13.280 に答える
2

ターゲットの .msstyles ファイル ( 内c:\windows\resources\themes) を開くと、表示プロパティ ボックスがポップアップ表示されます。この時点で、ウィンドウのサブクラス化を使用して、プログラムで正しいボタンをクリックできます。

于 2009-02-13T17:31:29.363 に答える