
标题: [原创]XP下VBS中调用对话框 [打印本页]
作者: 3742668 时间: 2006-5-23 14:20 标题: [原创]XP下VBS中调用对话框
感觉本版关于VBS方面的贴子实在是少,那我先来丢块砖吧,咱也来个抛砖引玉,一砖激起数层浪。正文如下:
在实际应用中,对话框可以很大程度上使VBS脚本人性化,可以使脚本使用起来更加直观明了。下面就列出在XP系统下可以通过VBS调用的部分对话框,由于水平有限,不足之处希望各位予以斧正。
1.保存文件对话框。
参考对象:SAFRCFileDlg.FileSave
示例代码:CODE: [Copy to clipboard]
Set objDialog = CreateObject("SAFRCFileDlg.FileSave")
Set objFSO = CreateObject(Scripting.FileSystemObject")
objDialog.FileName = "歌曲"
objDialog.FileType = ".lrc"
intReturn = objDialog.OpenFileSaveDlg
If intReturn Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(objDialog.FileName & objDialog.FileType)
objFile.WriteLine Date
objFile.Close
Else
Wscript.Quit
End If
用FilaName属性来指定默认的保存文件名,可以用类似的Cstr(now),Cstr(date)等等来按时间日期来保存文件。
用FileType属性来指定默认的文件类型。
在调用OpenFileSaveDlg方法时,最好把返回值保存到一变量中,用它可以判断用户按下的是确定还是取消。
注意:保存文件的代码需要自行编写,本例中用的是Scripting.FileSystemObject对象来操作文件。
2.打开文件/文件夹对话框
参考对象:UserAccounts.CommonDialog,SAFRCFileDlg.FileOpen以及Shell.Application
示例代码:
打开文件1:CODE: [Copy to clipboard]
set objFile = CreateObject("SAFRCFileDlg.FileOpen")
intRet = objFile.OpenFileOpenDlg
wscript.echo intRet, objFile.FileName
打开文件2(支持类型过滤):CODE: [Copy to clipboard]
On Error Resume Next
strLoadFile = WScript.Arguments(0)
Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = "vbs File|*.vbs|All Files|*.*"
'objDialog.MaxFileSize = 10000
'objDialog.FilterIndex = 1
objDialog.InitialDir = ""
if IsEmpty(WScript.Arguments(0)) then
objDialog.ShowOpen
strLoadFile = objDialog.FileName
end if
Wscript.Echo strLoadFile
'支持拖放
打开文件3(多个文件):CODE: [Copy to clipboard]
Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = "VBScript Scripts|*.vbs|All Files|*.*"
objDialog.Flags = &H0200
objDialog.FilterIndex = 1
objDialog.InitialDir = "e:\"
intResult = objDialog.ShowOpen
If intResult = 0 Then
Wscript.Quit
Else
arrFiles = Split(objDialog.FileName, " ")
For i = 1 to Ubound(arrFiles)
strFile = arrFiles(0) & arrFiles(i)
Wscript.Echo strFile
Next
End If
打开文件夹:CODE: [Copy to clipboard]
Const MY_COMPUTER = &H11&
Const WINDOW_HANDLE = 0
Const OPTIONS = 0
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(MY_COMPUTER)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder _
(WINDOW_HANDLE, "选择文件夹:", OPTIONS, strPath)
If objFolder Is Nothing Then
Wscript.Quit
End If
Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path
Wscript.Echo objPath
太多了,就不一一详细说明了,有代码应该看得明白了吧,如果有疑问可以问问我,知无不言。
3.系统对话框调用
参考对象:Shell.Application
它的方法太多了,如果一个个贴出来的话太麻烦了,我简单地贴一个功能的代码,然后把具体的一些方法贴出来。
示例代码:CODE: [Copy to clipboard]
Set objShell = WScript.CreateObject("Shell.Application")
With objShell
'.TileHorizontally'所有窗口水平平排列
'.TileVertically'所有窗口垂直平排列
'.CascadeWindows'所有窗口一样大小又上至下又左到右排列
'.BrowseForFolder(1,"C:\",1) '前面已经有了
'.FindComputer'弹出查找计算机
'.EjectPC'注销,不过实际测试有点问题
.FindFiles'弹出查找文件
.MinimizeAll'所有以打开窗口最小化
.Explore("c:\com")'打开Windows 资源管理器,可以查定文件夹名
.filerun'弹出开始菜单中的“动行”
'.UndoMinimizeALL'取消所有以打开窗口最小化
'.open("c:\com")'打到指定文件夹
'.RefreshMenu
.SetTime'弹出日期/时间 属性
.TrayProperties'弹出任务栏属性窗口
end With
还有部分方法,等我整理出来再贴上来。
4.利用IE对象来实现图形界面。
参考对象:InternetExplorer.Application
示例代码:CODE: [Copy to clipboard]
With CreateObject("InternetExplorer.Application")
MsgBox "显示IE"
.visible = True
MsgBox "隐藏ie"
.visible = False
MsgBox "关闭ie"
.quit
end With
由于这个对象的方法,属性,以及其他调用太过庞大,而且要想把它应用得出神入化还需要一定的DHTML知识,所以就简单地贴贴部分属性以方法算了,另外具体与DHTML结合的应用可以参考 批处理:枚举硬盘上的电影rmvb格式并生成网页 。
挂一漏万,呵呵。如果大家觉得值得一看的话,这几天就再贴几篇关于用VBS操作数据库,操作文件,网络应用等代码。
作者: 3742668 时间: 2006-5-23 14:22
忘记贴IE的部分属性与方法列表了,下面是我从网上搜集的一些:
| Quote: |
Property activeElement As IHTMLElement
Property alinkColor
Property all As IHTMLElementCollection
Property anchors As IHTMLElementCollection
Property applets As IHTMLElementCollection
Function attachEvent(ByVal event As String, ByVal pdisp As Object) As Boolean
Property bgColor
Property body As IHTMLElement
Property charset As String
Property childNodes As Object
Sub clear
Sub close
Property cookie As String
Function createDocumentFragment As IHTMLDocument2
Function createElement(ByVal eTag As String) As IHTMLElement
Function createstyleSheet([ByVal bstrHref As String], [ByVal lIndex As Long-1]) As IHTMLstyleSheet
Function createTextNode(ByVal text As String) As IHTMLDOMNode
Property defaultCharset As String
Property designMode As String
Sub detachEvent(ByVal event As String, ByVal pdisp As Object)
Property dir As String
Property documentElement As IHTMLElement
Property domain As String
Function elementFromPoint(ByVal x As Long, ByVal y As Long) As IHTMLElement
Property embeds As IHTMLElementCollection
Function execCommand(ByVal cmdID As String, [ByVal showUI As BooleanFalse], [ByVal value]) As Boolean
Function execCommandShowHelp(ByVal cmdID As String) As Boolean
Property expando As Boolean
Property fgColor
Property fileCreatedDate As String
Property fileModifiedDate As String
Property fileSize As String
Property fileUpdatedDate As String
Property forms As IHTMLElementCollection
Property frames As IHTMLFramesCollection2
Function getElementById(ByVal v As String) As IHTMLElement
Function getElementsByName(ByVal v As String) As IHTMLElementCollection
Function getElementsByTagName(ByVal v As String) As IHTMLElementCollection
Property images As IHTMLElementCollection
Property lastModified As String
Property linkColor
Property links As IHTMLElementCollection
Property location As IHTMLLocation
Property mimeType As String
Property nameProp As String
Property onafterupdate
Property onbeforeeditfocus
Property onbeforeupdate
Property oncellchange
Property onclick
Property oncontextmenu
Property ondataavailable
Property ondatasetchanged
Property ondatasetcomplete
Property ondblclick
Property ondragstart
Property onerrorupdate
Property onhelp
Property onkeydown
Property onkeypress
Property onkeyup
Property onmousedown
Property onmousemove
Property onmouseout
Property onmouseover
Property onmouseup
Property onpropertychange
Property onreadystatechange
Property onrowenter
Property onrowexit
Property onrowsdelete
Property onrowsinserted
Property onselectstart
Property onstop
Function open([ByVal url As String"text/html"], [ByVal name], [ByVal features], [ByVal replace]) As Object
Property parentWindow As IHTMLWindow2
Property plugins As IHTMLElementCollection
Property protocol As String
Function queryCommandEnabled(ByVal cmdID As String) As Boolean
Function queryCommandIndeterm(ByVal cmdID As String) As Boolean
Function queryCommandState(ByVal cmdID As String) As Boolean
Function queryCommandSupported(ByVal cmdID As String) As Boolean
Function queryCommandText(ByVal cmdID As String) As String
Function queryCommandvalue(ByVal cmdID As String)
Property readyState As String
Sub recalc([ByVal fForce As BooleanFalse])
Property referrer As String
Sub releaseCapture
Property scripts As IHTMLElementCollection
Property security As String
Property selection As IHTMLSelectionObject
Property styleSheets As IHTMLstyleSheetsCollection
Property title As String
Function toString As String
Property uniqueID As String
Property url As String
Property vlinkColor
Sub write([ParamArray ByVal psarray() As Variant])
Sub writeln([ParamArray ByVal psarray() As Variant]) |
|
作者: doscc 时间: 2006-5-23 14:44
好贴.
大部份记不住.收藏了
作者: electronixtar 时间: 2006-5-23 15:16
斗胆给楼主挑毛病~~嘿嘿
第一个程序
少了 " ,因该是CODE: [Copy to clipboard]
CreateObject("Scripting
似乎 .lrc 的后缀过滤没效果啊~~呵呵
我也喜欢看MSDN上Shell部分的源码~~
BagPipe编vbs很强啊,为什么不出来冒个泡啊?
[ Last edited by electronixtar on 2006-5-23 at 15:19 ]
作者: 3742668 时间: 2006-5-25 23:40
谢谢指正。
对于.lrc的问题,其实它并不是用来过滤的,而是在保存的时候获得文件名后缀的。
对了,还漏掉了msgbox,inputbox以及WScript.Shell对象的Popup方法,不过这几个都是非常简单的了。
作者: electronixtar 时间: 2006-12-22 01:18
今天找资料又找到这里来了,斑竹写的真是经典啊
作者: redtek 时间: 2006-12-22 01:26
看到这么精彩的贴子于是后面悄悄地跟着electronixtar兄也过来了~:)))
顶~!!!吐血收藏!!!
作者: ccwan 时间: 2006-12-22 02:00
终于有机会顶一贴了,高兴!
作者: wydos 时间: 2006-12-22 05:41
收藏了!!
版主能不做个类似savetext的保存网页的快捷右键
作者: eech 时间: 2006-12-22 07:05
为版主的精神所感动.顶一下.我会努力学好批处理.
| 欢迎光临 中国DOS联盟论坛 (http://cndos.fam.cx/forum/) |
Powered by Discuz! 2.5 |