windows 获取标题打开窗口并以特定标题关闭?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2935591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get title opening windows and close with specific title?
提问by Alexandr
How can I close a window with a specific title in Windows XP base using VBscript?
如何使用 VBscript 在 Windows XP base 中关闭具有特定标题的窗口?
Or is there another way to solve this problem?
或者有其他方法可以解决这个问题吗?
回答by Helen
You can use the SendKeys
method to send the Alt+F4shortcut to the window you wish to close. This window must be active at the moment, so you also need to call AppActivate
right before SendKeys
.
您可以使用该SendKeys
方法将Alt+F4快捷方式发送到您要关闭的窗口。此窗口目前必须处于活动状态,因此您还需要在AppActivate
之前调用SendKeys
。
Basically, you'll need something like this:
基本上,你需要这样的东西:
Set oShell = CreateObject("WScript.Shell")
oShell.AppActivate "Untitled - Notepad"
oShell.SendKeys "%{F4}"
You may want to add checks and small delays to make your script more foolproof:
您可能希望添加检查和小延迟以使您的脚本更加万无一失:
Set oShell = CreateObject("WScript.Shell")
If oShell.AppActivate("Untitled - Notepad") Then
WScript.Sleep 500
oShell.SendKeys "%{F4}"
End If
Edit:(An answer to your comment/question about VBScript resources.)
编辑:(对您关于 VBScript 资源的评论/问题的回答。)
I've compiled some links to VBScript websites and resource pages that I hope they will be helpful:
我已经编译了一些指向 VBScript 网站和资源页面的链接,希望它们会有所帮助:
Learning
学习
- Scripting: Your First Steps
- Sesame Script(CHM download)
- VBScript Tutorialon W3Schools (note: this tutorial is oriented on browser scripting, not desktop scripting)
- 脚本编写:您的第一步
- 芝麻脚本(CHM下载)
- W3Schools 上的VBScript 教程(注意:本教程面向浏览器脚本,而不是桌面脚本)
References
参考
- MSDN 中的VBScript 用户指南和参考( CHM 下载)
Other resources
其他资源
- TechNet Script Center(CHM download)
- TechNet Script Repository(CHM download)
- Hey, Scripting Guy! blog(CHM arvhive)
- Rob van der Woude's Scripting Pages
- Also, check out VBScript questionshere on Stack Overflow
- TechNet 脚本中心(CHM 下载)
- TechNet 脚本存储库(CHM 下载)
- 嘿,脚本专家!博客( CHM arvhive)
- Rob van der Woude 的脚本页面
- 另外,在 Stack Overflow 上查看此处的VBScript 问题
As for VBScript resources in Russian, check out script-coding.infoand Серый форум— there're lots of useful and interesting examples. Also, take a look at the this thread, which contains links to many VBScript resources, including those in Russian.
至于俄语的 VBScript 资源,请查看script-coding.info和Серый форум— 有很多有用和有趣的例子。另外,请查看此线程,其中包含指向许多 VBScript 资源(包括俄语资源)的链接。
回答by Ritesh Karwa
Posting this answer for anyone who is still stuck on trying to close the WScript.Shell Object after creating it and not able to find a solution. I tried the above solution and it cause MSWord 2016 to crash, don't know the reason My Vb Script :
为任何在创建 WScript.Shell 对象后仍然试图关闭它而无法找到解决方案的人发布此答案。我尝试了上述解决方案,它导致 MSWord 2016 崩溃,不知道原因 My Vb Script :
Dim wsh As Object
Set wsh = CreateObject("WScript.Shell", vbNothing)
wsh.Run "cmd.exe /C pause"
wsh.Run "taskkill /F /IM cmd.exe"