How do I change the current Windows theme programmatically?

前端 未结 12 1960
轻奢々
轻奢々 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条回答
  •  -上瘾入骨i
    2020-12-04 12:10

    There are certainly good reasons for wanting to change the current theme programmatically. E.g. an automated test tool may need to switch between various themes to make sure the application works correctly with all of them.

    As a user, you can change the theme by double-clicking a .theme file in Windwos Explorer and then closing the Control Panel applet that pops up. You can easily do the same from code. The steps below work just fine for me. I've only tested on Windows 7.

    1. Use SHGetKnownFolderPath() to get the "Local AppData" folder for the user. Theme files are stored in the Microsoft\Windows\Themes subfolder. Theme files stored there are applied directly, while theme files stored elsewhere are duplicated when you execute them. So it's best to use files from that folder only.
    2. Use ShellExecute() to execute the .theme file you located in step 1.
    3. Wait for the theme to be applied. I simply let my app sleep for 2 seconds.
    4. Call FindWindow('CabinetWClass', 'Personalization') to get the handle of the Control Panel window that popped up when the theme was applied. The "Personalization" caption will likely be different on non-US-English versions of Windows.
    5. Call PostMessage(HWND, WM_CLOSE, 0, 0) to close the Control Panel window.

    This isn't a very elegant solution, but it does the job.

提交回复
热议问题