コード例 #1
0
        /// <summary>
        /// Get the current defined appearance
        /// </summary>
        /// <returns></returns>
        public static DesktopAppearance GetAppearance()
        {
            string theme = GetTheme();

            if (theme.ToLowerInvariant() == AeroString.ToLowerInvariant())
            {
                if (IsDwmEnabled)
                {
                    return(DesktopAppearance.AeroWithComposition);
                }
                else
                {
                    return(DesktopAppearance.AeroWithoutComposition);
                }
            }
            else if (theme.ToLowerInvariant() == LunaString.ToLowerInvariant())
            {
                string colorScheme = GetThemeStyle();

                if (colorScheme.ToLowerInvariant() == LunaMetallicString.ToLowerInvariant())
                {
                    return(DesktopAppearance.LunaMetallic);
                }
                else if (colorScheme.ToLowerInvariant() == LunaHomesteadString.ToLowerInvariant())
                {
                    return(DesktopAppearance.LunaHomestead);
                }
                else
                {
                    return(DesktopAppearance.LunaNormalColor);
                }
            }
            else if (theme.ToLowerInvariant() == WindowsClassicString.ToLowerInvariant())
            {
                return(DesktopAppearance.WindowsClassic);
            }
            else
            {
                return(DesktopAppearance.Unrecognized);
            }
        }
コード例 #2
0
        /// <summary>
        /// Gets the collection of supported appearances on this machine
        /// </summary>
        /// <returns></returns>
        public static DesktopAppearance[] GetAvailableAppearances()
        {
            ArrayList appearanceList = new ArrayList();

            string[] availableThemes = GetAvailableThemes();

            foreach (string theme in availableThemes)
            {
                if (theme.ToLowerInvariant() == AeroString.ToLowerInvariant())
                {
                    appearanceList.Add(DesktopAppearance.AeroWithoutComposition);

                    if (IsDwmCompositionSupported)
                    {
                        appearanceList.Add(DesktopAppearance.AeroWithComposition);
                    }
                }
                else if (theme.ToLowerInvariant() == LunaString.ToLowerInvariant())
                {
                    appearanceList.Add(DesktopAppearance.LunaNormalColor);
                    appearanceList.Add(DesktopAppearance.LunaMetallic);
                    appearanceList.Add(DesktopAppearance.LunaHomestead);
                }
                else if (theme.ToLowerInvariant() == RoyaleString.ToLowerInvariant())
                {
                    appearanceList.Add(DesktopAppearance.Royale);
                }
                else if (theme.ToLowerInvariant() == WindowsClassicString.ToLowerInvariant())
                {
                    appearanceList.Add(DesktopAppearance.WindowsClassic);
                }
            }

            DesktopAppearance[] availableAppearances = new DesktopAppearance[appearanceList.Count];
            appearanceList.CopyTo(availableAppearances);

            return(availableAppearances);
        }
コード例 #3
0
        /// <summary>
        /// Change the theme, style, and color scheme to match a defined appearance
        /// </summary>
        /// <param name="appearance">the desired defined appearance</param>
        public static void SetAppearance(DesktopAppearance appearance)
        {
            if (appearance == GetAppearance())
            {
                return;
            }
            else if ((!new ArrayList(GetAvailableAppearances()).Contains(appearance)))
            {
                throw new ArgumentException("The appearance '" + appearance + "' is not currently available.", appearance.ToString());
            }

            string themeName     = GetTheme();
            string styleFilename = null;

            // This is only needed if a change in color scheme is required (for Luna and Aero)
            UIHandler appearanceUIHandler = null;

            if (appearance == DesktopAppearance.LunaNormalColor ||
                appearance == DesktopAppearance.LunaMetallic ||
                appearance == DesktopAppearance.LunaHomestead)
            {
                // First ensure the theme is set to Luna
                if (themeName.ToLowerInvariant() != LunaString.ToLowerInvariant())
                {
                    SetTheme(LunaString);
                    themeName = GetTheme();
                    if (themeName.ToLowerInvariant() != LunaString.ToLowerInvariant())
                    {
                        throw new ArgumentException("Failed to change to the theme '" + LunaString + "'.", appearance.ToString());
                    }
                }

                // Build the path for the Luna.msstyles file
                styleFilename = Path.Combine(themeDirectory, themeName + @"\" + themeName + ".msstyles");
                if (!File.Exists(styleFilename))
                {
                    throw new ArgumentException("The msstyles file '" + styleFilename + "' does not exist.", appearance.ToString());
                }

                string lunaResourceString = "";

                // Get the localized string to select in the color scheme menu
                if (appearance == DesktopAppearance.LunaMetallic)
                {
                    lunaResourceString = Microsoft.Test.Loaders.ResourceHelper.GetUnmanagedResourceString(styleFilename, 1001);
                }
                else if (appearance == DesktopAppearance.LunaHomestead)
                {
                    lunaResourceString = Microsoft.Test.Loaders.ResourceHelper.GetUnmanagedResourceString(styleFilename, 1002);
                }
                else
                {
                    lunaResourceString = Microsoft.Test.Loaders.ResourceHelper.GetUnmanagedResourceString(styleFilename, 1000);
                }

                // Use the UIHandler for the XP Appearance dialog
                appearanceUIHandler = new ChangeXPAppearanceUIHandler(lunaResourceString);
            }
            else if (appearance == DesktopAppearance.AeroWithoutComposition ||
                     appearance == DesktopAppearance.AeroWithComposition)
            {
                // First ensure the theme is set to Aero
                if (themeName.ToLowerInvariant() != AeroString.ToLowerInvariant())
                {
                    SetTheme(AeroString);
                    themeName = GetTheme();
                    if (themeName.ToLowerInvariant() != AeroString.ToLowerInvariant())
                    {
                        throw new ArgumentException("Failed to change to the theme '" + AeroString + "'.", appearance.ToString());
                    }
                }

                // Build the path to Aero.msstyles
                styleFilename = Path.Combine(themeDirectory, themeName + @"\" + themeName + ".msstyles");
                if (!File.Exists(styleFilename))
                {
                    throw new ArgumentException("The msstyles file '" + styleFilename + "' does not exist.", appearance.ToString());
                }

                // Use the UIHandler for the Vista Appearance dialog
                appearanceUIHandler = new ChangeVistaAppearanceUIHandler(appearance);
            }
            else if (appearance == DesktopAppearance.Royale)
            {
                // Change the theme to Royale
                if (themeName.ToLowerInvariant() != RoyaleString.ToLowerInvariant())
                {
                    SetTheme(RoyaleString);
                    themeName = GetTheme();
                    if (themeName.ToLowerInvariant() != RoyaleString.ToLowerInvariant())
                    {
                        throw new ArgumentException("Failed to change to the theme '" + RoyaleString + "'.", appearance.ToString());
                    }
                }
            }
            else if (appearance == DesktopAppearance.WindowsClassic)
            {
                // Change the theme to Classic
                if (themeName.ToLowerInvariant() != WindowsClassicString.ToLowerInvariant())
                {
                    SetTheme(WindowsClassicString);
                    themeName = GetTheme();
                    if (themeName.ToLowerInvariant() != WindowsClassicString.ToLowerInvariant())
                    {
                        throw new ArgumentException("Failed to change to the theme '" + WindowsClassicString + "'.", appearance.ToString());
                    }
                }
            }

            if (appearanceUIHandler != null)
            {
                //Get the active window since the window activation will be lost by lauching the display properties
                IntPtr             activehWnd = GetForegroundWindow();
                ApplicationMonitor appMonitor = new ApplicationMonitor();
                try
                {
                    //invoke the msstyles file and handle the dispay config UI
                    appMonitor.RegisterUIHandler(appearanceUIHandler, "rundll32", null, UIHandlerNotification.Visible);
                    appMonitor.StartProcess(styleFilename);
                    if (!appMonitor.WaitForUIHandlerAbort(60000))
                    {
                        throw new TimeoutException("The 60 second timeout occured while waiting for themes to change.");
                    }
                }
                finally
                {
                    //Stop the AppMonitor (this will kill rundll32 if it has not exited... hopefuly this wont leave the machine in a bad state if it a timeout occurs
                    appMonitor.Close();

                    //Restore the active window
                    if (activehWnd != IntPtr.Zero)
                    {
                        SetForegroundWindow(activehWnd);
                    }
                }
            }

            if (GetAppearance() != appearance)
            {
                throw new Exception("Unable to change appearance.");
            }
        }