如何通过P / Invoking在Windows 8 / 8.1中以编程方式更改视觉主题?

C#VB.Net中 ,我知道一个视觉主题.theme文件的规范,我想应用这个视觉主题在Windows中, 而不依赖于其他应用程序,如RunDll32.exe ,只是P /调用,但避免怪异/奇怪的事情,如打开个性化窗口,然后使用FindWindow函数closures它,该程序应该从平台调用自动不与其他窗口交互。

这个关于如何申请一个主题的问题之前曾经被很多人问过(包括我在内,通过registry修改加上服务停止/恢复,只能在Windows 7下运行),我想这是一个专家来说明我们的时间与WinAPI方法不涉及RunDll32.exe既不打开个性化窗口。

我不知道这可以通过在registry项HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\ThemeManager上设置一些值,然后通过SendMessagePostMessage或其他函数发送/发送消息,或者可能通过SendMessageTimeOut函数通知有关环境更改或SHChangeNotifySystemParametersInfo或另一个函数,因为在uxtheme.dll库似乎没有什么有用的这个任务,问题是什么函数和什么参数应用一个视觉主题的变化,有一些商业应用程序,可以做到这一点,这是做到这一步呢?,我尝试了所有这些function没有成功。


这是我过去为Windows 7所做的解决scheme,我记得这并不完美,因为对于某些主题,颜色并没有正确应用,只能通过用户会话重新login来解决,以便在修改后正确地影响更改:

 Private Sub SetAeroTheme(ByVal themeFile As String, Optional ByVal colorName As String = "NormalColor", Optional ByVal sizeName As String = "NormalSize") Dim regKeyPath As String = "Software\Microsoft\Windows\CurrentVersion\ThemeManager" Using themeService As New ServiceController("Themes") If themeService.Status = ServiceControllerStatus.Running Then themeService.Stop() themeService.WaitForStatus(ServiceControllerStatus.Stopped) End If Using regKey As RegistryKey = Registry.CurrentUser.OpenSubKey(regKeyPath, writable:=True) regKey.SetValue("LoadedBefore", "0", RegistryValueKind.String) regKey.SetValue("DllName", themeFile, RegistryValueKind.String) regKey.SetValue("ColorName", colorName, RegistryValueKind.String) regKey.SetValue("SizeName", sizeName, RegistryValueKind.String) End Using If themeService.Status = ServiceControllerStatus.Stopped Then themeService.Start() themeService.WaitForStatus(ServiceControllerStatus.Running) End If End Using End Sub 

在Windows 8中,我认为,因为DWM组成的改变只是代码不工作了。

在pinvoke.net上有一个名为“SetSystemVisualStyle”的未公开的函数 ,允许您更改当前的“msstyles”文件。 由于这个功能是无证的,所以需要注意:“自担风险”。

以下功能签名来自上面引用的站点。

C#签名

 [DllImport("UxTheme.Dll", EntryPoint = "#65", CharSet = CharSet.Unicode)] public static extern int SetSystemVisualStyle(string pszFilename, string pszColor, string pszSize, int dwReserved); 

用法:

 // This will set your Visual Style to Luna SetSystemVisualStyle(@"C:\WINDOWS\resources\Themes\Luna\Luna.msstyles", "Metallic", "NormalSize", 0); 

VB.Net签名

 <DllImport("UxTheme.DLL", BestFitMapping:=False, CallingConvention:=CallingConvention.Winapi, CharSet:=CharSet.Unicode, EntryPoint:="#65")> _ Shared Function SetSystemVisualStyle(ByVal pszFilename As String, ByVal pszColor As String, ByVal pszSize As String, ByVal dwReserved As Integer) As Integer End Function 

OP要求将以下信息添加到此答案中。

当第三方主题应用于自定义的msstyles时,函数本身并没有适当地改变一些对话框的颜色和一些控制样式,但是通过测试从0到Int32的所有可能的值来做实验。最大的把它传递给保留的参数SetSystemVisualTheme函数,到目前为止我发现一个65的值可以修正这种着色和样式问题。