应用某些 Windows 主题的 VB 脚本 [英] VB Script to apply certain Windows Theme
问题描述
我编写了通过以下代码在我的目录中执行它来更改我的 Windows 主题的脚本:
I have written script that changes my Windows theme by executing it in my directory via the following code:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,@Themes /Action:OpenTheme /file:""C:\Users\[MYUSERNAME]\AppData\Local\Microsoft\Windows\Themes\MyTheme.theme"""
'This part of the code just closes the Explorer window that pops up after the file is executed
Wscript.Sleep 7000
WshShell.AppActivate ("Desktop Properties")
WshShell.SendKeys "%FC"
WshShell.SendKeys "{F4}"
我想让任何使用它的人都可以访问此代码.有没有办法不使用我的特定用户名,而是使用类似于当前用户的内容?那么让代码对任何人通用,而不必更改文件位置或文件路径中的用户名?非常感谢任何帮助,谢谢!
I want to make this code accessible to anyone that uses it. Is there a way to instead of using my specific username, use something that would resemble the current user? So make the code universal to anyone without having them have to change the file location or the username in the file path? Any help is much appreciated, thanks!
推荐答案
获取Local Application Data路径的方法有几种:
There are few ways to get Local Application Data path:
MsgBox CreateObject("WScript.Shell").ExpandEnvironmentStrings("%localappdata%")
MsgBox WScript.CreateObject("WScript.Shell").Environment("process").Item("localappdata")
MsgBox WScript.CreateObject("Shell.Application").NameSpace(&H1C).Self.Path
使用该功能,您的代码可能如下所示:
Using that abilities your code may be as follows:
Set WshShell = WScript.CreateObject("WScript.Shell")
sLocalAppDataPath = WshShell.ExpandEnvironmentStrings("%localappdata%")
WshShell.Run "rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,@Themes /Action:OpenTheme /file:""" & sLocalAppDataPath & "\Microsoft\Windows\Themes\MyTheme.theme"""
'This part of the code just closes the Explorer window that pops up after the file is executed
Wscript.Sleep 7000
WshShell.AppActivate ("Desktop Properties")
WshShell.SendKeys "%FC"
WshShell.SendKeys "{F4}"
请注意,.SendKeys
的使用不是最佳实践.如果您找到更可靠的解决方案会更好.
Note that usage of .SendKeys
is not the best practice. Would be better if you find a more reliable solution.
UPD:试试下面的代码关闭窗口:
UPD: Try the code below to close the window:
UPD2: 我已经通过 IWebBrowser 对象更改了主题应用程序启动字符串和资源管理器窗口控制.我想这正是您所需要的:
UPD2: I've changed theme application launch string and explorer window contol via IWebBrowser object. I guess that is exactly what you need:
Set WshShell = WScript.CreateObject("WScript.Shell")
sLocalAppDataPath = WshShell.ExpandEnvironmentStrings("%localappdata%")
Do While GetWnd(oWnd) ' close all control panel windows
oWnd.Quit
Loop
WshShell.Run "Explorer.exe """ & sLocalAppDataPath & "\Microsoft\Windows\Themes\MyTheme.theme""" ' apply theme
Do Until GetWnd(oWnd) ' wait until personalization window appears
WScript.Sleep 10
Loop
sLocationName = oWnd.LocationName ' debug
oWnd.Quit ' close appeared personalization window
WScript.Echo sLocationName & " Closed" ' debug
Function GetWnd(oShellWnd)
On Error Resume Next
GetWnd = False
For Each oShellWnd In CreateObject("Shell.Application").Windows
With oShellWnd
If InStr(LCase(TypeName(.Document)), "ishell") = 0 Then ' is explorer window, but not internet explorer
Else
If InStr(.Document.Folder.Self.Path, "::{26EE0668-A00A-44D7-9371-BEB064C98683}") = 0 Then ' any control panel window
Else
GetWnd = True
Exit For
End If
End If
End With
Next
End Function
这篇关于应用某些 Windows 主题的 VB 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!