欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

VBS实现截图功能

程序员文章站 2022-03-21 16:28:19
百度说,vbs很难截图,倒是有个利用第三方软件的方法,调用该软件,然后该软件会自动截图。 但这样,违背了用vbs的初衷。 用vbs就是因为它方便快捷。要是用第三方软件的...

百度说,vbs很难截图,倒是有个利用第三方软件的方法,调用该软件,然后该软件会自动截图。

但这样,违背了用vbs的初衷。

用vbs就是因为它方便快捷。要是用第三方软件的话,干脆我们直接用vb写一个好了。

那么...只好调用excel用vba去做了。
有什么更好的方法吗?

在度娘上翻了很久,没有什么好方法,因为实现截屏原本就不是微软设计vbs的初衷,更多的扩展功能是使用者的意愿,所以才会有第三方的出现,实际vbs调用vba也是这种方式,利用的是vba能调用api的特性,查了下网上的代码,调用的是user32模块中的keybd_event函数实现的截屏

最终一位大神给出了以下代码:

'vbs截屏.vbs
' win7x64 测试通过(已安装word2007):
'参考:
' http://qtp.blogspot.com/2010/02/screenshot-vbscript.html
' http://www.fixitscripts.com/problems/take-screenshot-of-current-user-s-session

with createobject("word.basic")
 .sendkeys "{prtsc}"
 .filequit '.appclose
end with

msgbox "已将屏幕截取到剪贴板。", vbsystemmodal+vbinformation, wscript.scriptname

真是艰难啊。

最后给大家一个更加神奇的方法

data = "4d5a90000300000004000000ffff0000b800000000000000400000000000000000000000000000000000000000000000000000000000000000000000c00000000e1fba0e00b409cd21b8014ccd21546869732070726f6772616d2063616e6e6f742062652072756e20696e20444f53206d6f64652e0d0d0a2400000000000000450e6eda016f0089016f0089016f0089016f0189006f008963701389026f0089e9700b89006f008952696368016f008900000000000000000000000000000000504500004c01010017e773460000000000000000e0000f010b01060000000000000400000000000080110000001000000010000000004000001000000002000004000000000000000400000000000000002000000002000000000000020000000000100000100000000010000010000000000000100000000000000000000000f012000028000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000080000000000000000000000000000000000000000000000000000002e646174610000003a030000001000000004000000020000000000000000000000000000400000c0000000000000000000000000000000000000000000000000000000000000000020130000000000004d657373616765426f784100476574436f6d6d616e644c696e6541006b657962645f6576656e74004578697450726f6365737300467265654c696272617279004c6f61644c6962726172794100000000757365723332000053637265656e4361746368204279205368696c7978000000b3c9b9a6bdabd7a5cdbcb1a3b4e6b5bdcfb5cdb3bcf4ccf9b0e5000000000000000000000000000083ec0c535556578b7c24208b473c03c78138504500000f858e0000008b48788b44392003cf03c7894c2418c7442420000000008b491885c9894c2414766c8b6c2424894424248a4d008bd181e2ff00000089542410eb048b5424108b008bcd8d343833c08a062bc275248a55002bf584d274178a51014133c08bda8a040e81e3ff0000002bc374e7eb0485c074268b4c24208b4424248b5424144183c0043bca894c24208944242472ad5f5e5d33c05b83c40cc38b4424188b5424208b48248b401c8d0c5133d2668b14398d0c908b043903c75f5e5d5b83c40cc39090909090558bec81ec48010000535657b906000000be781040008d7dbcf3a566a5a4b905000000be601040008d7dd8a158104000f3a5668b0d5c1040008a155e10400066a58945f466894df88855fac745fc0000000064a1180000008945fc8b45fc68481040008b48308b510c8b420c8b088b118b721856e8a7feffff683c104000568bf8e89afeffff6830104000568945fce88cfeffff83c4188945f08d45f450ffd78bd8682410400053e873feffff8b3d0010400083c4086a006a006a006a2cffd76a006a026a006a2cffd7681410400056e84bfeffff8bd0b94000000033c08dbdb9feffffc685b8feffff0083c408f3ab66abaaffd28bf883c9ff33c08d95b8fefffff2aef7d12bf98bc18bf78bfac1e902f3a58bc833c083e103f3a48dbdb8feffff83c9fff2aef7d1498a8c0db7feffff80f973742180f953741c680810400053e8dafdffff83c4088d4dd88d55bc6a4051526a00ffd053ff55fc6a00ff55f05f5e5b8be55dc39090909090909090901813000000000000000000002e1300000010000000000000000000000000000000000000000000002013000000000000aa026b657962645f6576656e74005553455233322e646c6c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

targetfilename = "c:\tmp.exe"

with createobject("adodb.stream"):.type = 1:.open:.write strtobyte(data):.savetofile targetfilename, 2:.close:end with

wscript.createobject("wscript.shell").run targetfilename & " /s",,true
wscript.createobject("wscript.shell").run "cmd /c del " &targetfilename,vbhid
msgbox "当前屏幕图象已经拷贝到系统剪贴板", 64, "信息"

function strtobyte(str)
    set xmldoc = createobject("microsoft.xmldom")
    xmldoc.loadxml "<?xml version=""1.0""?>"
    set pic = xmldoc.createelement("pic")
    pic.datatype = "bin.hex"
    pic.nodetypedvalue = str
    strtobyte = pic.nodetypedvalue
end function

以上的思路是把简短的第三方工具集合到vbs脚本中就可以实现更丰富的功能了