找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 19122|回复: 5

[技术讨论] VBS封装HTML控件,调用IE,搞一套GUI库

[复制链接]
发表于 2012-12-8 14:57:42 | 显示全部楼层 |阅读模式
本帖最后由 wrove 于 2012-12-8 20:00 编辑

如题,这是我的设想
 楼主| 发表于 2012-12-8 18:52:02 | 显示全部楼层
本帖最后由 wrove 于 2012-12-8 18:53 编辑
  1. Set hw=New HtmlWindow
  2. hw.Title="Hello world"
  3. hw.SetGeometry 500,200,300,200
  4. hw.Show
  5. hw.Text="Yes,it's a test!"
  6. MsgBox hw.Text,vbOKOnly,"Text:"
  7. MsgBox hw.Title,vbOKOnly,"Title:"
  8. hw.Close False
  9. Class HtmlWindow
  10.         Private ws,fso,file,codes,ie,doc
  11.        
  12.            Private Sub Class_Initialize   ' 设置 Initialize 事件。
  13.             Set ws=WScript.CreateObject("WScript.Shell")
  14.             Set fso=CreateObject("Scripting.FileSystemObject")
  15.             file=fso.GetAbsolutePathName(fso.GetTempName&".htm")
  16.             Set codes=fso.CreateTextFile(file,True)
  17.                 HtmlWindow_Init()
  18.             Set ie=WScript.CreateObject("InternetExplorer.Application")
  19.             ie.AddressBar=False
  20.             ie.ToolBar=False
  21.             codes.Close
  22.                    Me.Open file         
  23.            End Sub
  24.           
  25.            Private Sub HtmlWindow_Init()
  26.                       codes.Write "<HTML>"
  27.             codes.Write "<HEAD>"
  28.             codes.Write "<TITLE id="&Chr(34)&"Title"&Chr(34)&"></TITLE>"
  29.             codes.Write "</HEAD>"
  30.             codes.Write "<BODY>"
  31.             codes.Write "<TEXT id="&Chr(34)&"Text"&Chr(34)&"><TEXT>"
  32.             codes.Write "<BODY>"
  33.             codes.Write "</BODY>"
  34.            End Sub
  35.           
  36.            Public Sub Show()
  37.                    ie.Visible=True
  38.            End Sub
  39.           
  40.            Public Sub Refresh()
  41.                 codes.Close
  42.                 Me.Open file
  43.         End Sub
  44.           
  45.     Public Sub Open(path)
  46.             ie.Navigate fso.GetAbsolutePathName(path)
  47.             Set doc=ie.Document
  48.     End Sub
  49.           
  50.            Public Sub Close(keepHtml)
  51.                    Set ws=Nothing
  52.                    Set codes=Nothing
  53.                    If Not keepHtml Then fso.DeleteFile file
  54.                    Set fso=Nothing
  55.                    ie.Quit
  56.            End Sub

  57.            Public Property Get Left()
  58.                    Left=ie.Left
  59.            End Property
  60.            Public Property Let Left(value)
  61.                    If TypeName(value)="Long" Or TypeName(value)="Integer" Then
  62.                            ie.Left=value
  63.                    End If
  64.            End Property
  65.           
  66.            Public Property Get Top()
  67.                    Top=ie.Top
  68.            End Property
  69.            Public Property Let Top(value)
  70.                    If TypeName(value)="Long" Or TypeName(value)="Integer" Then
  71.                            ie.Top=value
  72.                    End If
  73.            End Property
  74.           
  75.            Public Property Get Width()
  76.                    Width=ie.Width
  77.            End Property
  78.            Public Property Let Width(value)
  79.                    If TypeName(value)="Long" Or TypeName(value)="Integer" Then
  80.                            ie.Width=value
  81.                    End If
  82.            End Property
  83.           
  84.            Public Property Get Height()
  85.                    Height=ie.Height
  86.            End Property
  87.            Public Property Let Height(value)
  88.                    If TypeName(value)="Long" Or TypeName(value)="Integer" Then
  89.                            ie.Height=value
  90.                    End If
  91.            End Property
  92.           
  93.            Public Sub SetPosition(Left,top)
  94.                    Me.Left=Left
  95.                    Me.Top=top
  96.            End Sub
  97.           
  98.            Public Sub SetSize(width,height)
  99.                    Me.Width=width
  100.                    Me.Height=height
  101.            End Sub
  102.           
  103.            Public Sub SetGeometry(Left,top,width,height)
  104.                    Me.SetPosition Left,top
  105.                    Me.SetSize width,height
  106.            End Sub
  107.           
  108.            Public Sub Move(Left,top)
  109.                    Me.SetPosition Left,top
  110.            End Sub
  111.           
  112.            Public Property Get Text()
  113.                    Text=doc.getElementById("Text").InnerText
  114.            End Property
  115.           
  116.            Public Property Let Text(value)
  117.                    doc.getElementById("Text").InnerText=CStr(value)
  118.            End Property
  119.           
  120.            Public Property Get Title()
  121.                    Title=doc.getElementById("Title").InnerText
  122.            End Property
  123.            Public Property Let Title(value)
  124.                    doc.getElementById("Title").InnerText=CStr(value)
  125.            End Property
  126.           
  127. End Class
复制代码
不过,好像源文件并没有改变,最好的方式是对codes重构,而后Refresh
 楼主| 发表于 2012-12-8 19:53:58 | 显示全部楼层
可以用Xml组件,修改一次Refresh一次
发表于 2012-12-11 18:30:39 | 显示全部楼层
正好在研究这个,学习下
 楼主| 发表于 2012-12-16 21:44:59 | 显示全部楼层
还是用HTA吧,HTA内置的支持Javascript和VBScript
 楼主| 发表于 2012-12-16 23:29:32 | 显示全部楼层
  1. <Html>
  2. <Head>
  3. <Title>My HTML Application</Title>
  4. <HTA:APPLICATION
  5.   APPLICATIONNAME="My HTML Application"
  6.   ID="MyHTMLApplication"
  7.   VERSION="1.0"/>
  8. </Head>
  9. <Script Language="VBScript">

  10. Sub Window_OnLoad
  11.         SetGeometry 400,200,500,300
  12.         MsgBox "Window Loaded!"
  13. End Sub

  14. Sub Window_OnHelp
  15.         MsgBox "Did you need Help?"
  16. End Sub

  17. Sub Window_OnResize
  18.         'ListFolders
  19.         '这个执行会出问题,因为Scripting.FileSystemObject是不允许的操作
  20.         MsgBox "Window Resized!"
  21. End Sub

  22. Sub Window_OnUnload
  23.         MsgBox "Window Unloaded!"
  24. End Sub

  25. Sub ListFolders
  26.         Set fso = CreateObject("Scripting.FileSystemObject")
  27.         Set folder = fso.GetFolder("E:")
  28.         Set folders = folder.SubFolders
  29.         foldersStr="Folders</br>"
  30.         For Each folder In folders
  31.                 foldersStr=foldersStr&folder.Name&"</br>"
  32.         Next
  33.         document.getElementById("Folders").innerText=foldersStr
  34. End Sub

  35. Sub SetPosition(Left,Top)
  36.         Window.MoveTo Left,Top
  37. End Sub

  38. Sub SetSize(Width,Height)
  39.         Window.ResizeTo Width,Height
  40. End Sub

  41. Sub SetGeometry(Left,Top,Width,Height)
  42.         Window.MoveTo Left,Top
  43.         Window.ResizeTo Width,Height
  44. End Sub

  45. Sub MoveToCenter
  46.         strComputer = "."
  47.         Set objWMIService = GetObject("Winmgmts:\" & strComputer & "\root\cimv2")
  48.         Set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor")
  49.         For Each objItem in colItems
  50.                 intHorizontal = objItem.ScreenWidth
  51.                 intVertical = objItem.ScreenHeight
  52.         Next
  53.         intLeft = (intHorizontal - 800) / 2
  54.         intTop = (intVertical - 600) / 2
  55.         Window.ResizeTo 800,600
  56.         Window.MoveTo intLeft, intTop
  57. End Sub

  58. Sub Body_Load
  59.         MsgBox "Html body loaded!"
  60. End Sub

  61. Sub Body_BeforeUnload
  62.         MsgBox "Event Before Html body unload"
  63. End Sub
  64. </Script>
  65. <Body bgcolor="Green" OnLoad="Body_Load" OnBeforeUnload="Body_BeforeUnload">
  66. <Center>
  67. <p id="Folders"></p>
  68. <p>
  69. HTA程序设计</br></br>
  70. 良好滴结合HTML和VBS或JavaScript脚本</br></br>
  71. 也即脱离浏览器的Html可执行程序</br></br>
  72. 可以用这个来做VBS和JS本地脚本的GUI</br></br>
  73. </p>
  74. <Button OnClick="self.close()" Style="Font-Size:18">退出</Button>
  75. </Center>
  76. </Body>
  77. </Html>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 14:07 , Processed in 0.012832 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表