How do I change the current Windows theme programmatically?

前端 未结 12 1986
轻奢々
轻奢々 2020-12-04 11:12

I want to allow my users to toggle the current user theme between Aero and Windows Classic(1). Is there a way that I can do this programatically?

I don\'t want to po

12条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-04 11:55

    In addition of the post of "Jan Goyvaerts": I use SendMessage instead of PostMessage. The difference is that SendMessage waits for the command to be taken in by the window. Meaning that in the SendMessages returns, you know that the theme dialog is closed.

    So if you start it with the monstrous (but genious) rundll32.exe method suggested by "Campbell". You should wait a sec before sending WM_CLOSE. Otherwise the theme will not be set and the application closes right away.

    The code snippet below extracts a file from resource (a themepack). Then executes the desk.cpl with rundll32.exe, waits 3 sceonds, then sends WM_CLOSE (0x0010), waits for the command to be process (the time it takes for the theme to be set).

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

提交回复
热议问题