Advertisement
omegastripes

wsh_vbs_gui_via_mshta_createwindow_with_events_handlers.vbs

Jan 20th, 2018
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VBScript 10.72 KB | None | 0 0
  1. Option Explicit
  2.  
  3. ' Base64-encoded background image
  4. Const BGI = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWIAAAB2CAYAAADybJlDAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAAUjSURBVHja7N05ztxGEIBRjqHciZ34Bn3/w9QNnPsE49QwtPwc9lLd9V4kAdKwyQE+Fjnb6/1+XwCs85tDACDEAEIMgBADCDEAQgwgxAAIMYAQAyDEAEIMgBADCDEAQgwgxACM980hgD28Xq/Td7Hd/PdxzHPri+FBiBPH9hPbBVqIQYh3D+/2YRZiEOIT47tVmIUYhLhCfFMHWYhBiKsFOF2UhRiEuHKAUwRZiEGIBXhxkIUYhFiAFwdZiEGIBXhxjIUYhDhLhCP5OocFWYhBiGeHLaxdiEGI54YsNljj0n0SYhDiEYGLhGtKG2MhBiHOFuCdXiDsEmMhhtoh7hW9SLKOLWMsxFA3xD3iVznA3Y6FEEPNED8NoAB3PC5CDPVCvDLCFT4gcvv4CDHUCvGTEFYKcMw8Vn48FOpYEeG28RQ87cRjIoYaE/GnMdxpCo7O2336eH9f1/WPEIMQr4jwyo8Zt4HbaqOOoRCDEGePcCzc9pQYCzGcHeJZER45ia6ewmP0MfViHZxrxwjHle9XltvoYyTEIMIZIvwkwDNvvfT6/0IMpInw0wn4SYTj4Xa63T4RYjANr4hwj1sQKybhIc+NEIMIz4xwr3vAvSI8eyo2EQNLAphhGo1Ej92EGESyW0AWhbD3fkaHbT86lkIMNc2KcO+3ox3500lCDPWm4ZkRXjnx31lDTDgeP3wcIYZaEZ712Bk+lBG7bFOIoZbYdBsz3g0SC/a/CTGw6lZAlok/xZqEGGrFsg163FW3AnqvY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item B", "Item C", "Item D", "Item E")
  10.  
  11. ' Create HTA window wrapper
  12. With New clsSmallWrapperForm
  13.     ' Setup window
  14.     .ShowInTaskbar = "yes"
  15.     .Title = "Test HTA UserForm"
  16.     .BackgroundImage = BGI
  17.     .Width = 354
  18.     .Height = 118
  19.     .Visible = False
  20.     ' Create window
  21.     .Create
  22.     ' Assign handlers
  23.     Set .Handlers = New clsSmallWrapperHandlers
  24.     ' Add ListBox
  25.     With .AddElement("ListBox1", "SELECT")
  26.         .size = 6
  27.         .multiple = True
  28.         .style.left = "15px"
  29.         .style.top = "10px"
  30.         .style.width = "250px"
  31.     End With
  32.     .AppendTo "Form"
  33.     ' Add ListBox items
  34.     For i = 0 To UBound(aItems)
  35.         .AddElement , "OPTION"
  36.         .AddText aItems(i)
  37.         .AppendTo "ListBox1"
  38.     Next
  39.     ' Add OK Button
  40.     With .AddElement("Button1", "INPUT")
  41.         .type = "button"
  42.         .value = "OK"
  43.         .style.left = "285px"
  44.         .style.top = "10px"
  45.         .style.width = "50px"
  46.         .style.height = "20px"
  47.     End With
  48.     .AppendTo "Form"
  49.     ' Add Cancel Button
  50.     With .AddElement("Button2", "INPUT")
  51.         .type = "button"
  52.         .value = "Cancel"
  53.         .style.left = "285px"
  54.         .style.top = "40px"
  55.         .style.width = "50px"
  56.         .style.height = "20px"
  57.     End With
  58.     .AppendTo "Form"
  59.     ' Add Label
  60.     With .AddElement("Label1", "SPAN")
  61.         .style.left = "15px"
  62.         .style.top = "98px"
  63.         .style.width = "350px"
  64.     End With
  65.     .AddText "Choose items"
  66.     .AppendTo "Form"
  67.     ' Show window
  68.     .Visible = True
  69.     ' Wait window closing or user choise
  70.     Do While .ChkDoc And Not .Handlers.Selected
  71.         WScript.Sleep 100
  72.     Loop
  73.     ' Read results from array .Handlers.SelectedItems
  74.     If .Handlers.Selected Then
  75.         MsgBox "Selected " & (UBound(.Handlers.SelectedItems) + 1) & " Item(s)" & vbCrLf & Join(.Handlers.SelectedItems, vbCrLf)
  76.     Else
  77.         MsgBox "Window closed"
  78.     End If
  79.     ' The rest part of code ...
  80.    
  81. End With
  82.  
  83. Class clsSmallWrapperHandlers
  84.    
  85.     ' Handlers class implements events processing
  86.     ' Edit code to provide the necessary behavior
  87.     ' Keep conventional VB handlers names: Public Sub <ElementID>_<EventName>()
  88.    
  89.     Public oswForm ' mandatory property
  90.    
  91.     Public Selected
  92.     Public SelectedItems
  93.    
  94.     Private Sub Class_Initialize()
  95.         Selected = False
  96.         SelectedItems = Array()
  97.     End Sub
  98.    
  99.     Public Sub ListBox1_Click()
  100.         Dim vItem
  101.         With CreateObject("Scripting.Dictionary")
  102.             For Each vItem In oswForm.Window.ListBox1.childNodes
  103.                 If vItem.Selected Then .Item(vItem.innerText) = ""
  104.             Next
  105.             SelectedItems = .Keys()
  106.         End With
  107.         oswForm.Window.Label1.style.color = "buttontext"
  108.         oswForm.Window.Label1.innerText = (UBound(SelectedItems) + 1) & " selected"
  109.     End Sub
  110.    
  111.     Public Sub Button1_Click()
  112.         Selected = UBound(SelectedItems) >= 0
  113.         If Selected Then
  114.             oswForm.Window.close
  115.         Else
  116.             oswForm.Window.Label1.style.color = "darkred"
  117.             oswForm.Window.Label1.innerText = "Choose at least 1 item"
  118.         End If
  119.     End Sub
  120.    
  121.     Public Sub Button2_Click()
  122.         oswForm.Window.close
  123.     End Sub
  124.    
  125. End Class
  126.  
  127. Class clsSmallWrapperForm
  128.    
  129.     ' Utility class for HTA window functionality
  130.     ' Do not modify
  131.    
  132.     ' HTA tag properties
  133.     Public Border ' thick | dialog | none | thin
  134.     Public BorderStyle ' normal | complex | raised | static | sunken
  135.     Public Caption ' yes | no
  136.     Public ContextMenu ' yes | no
  137.     Public Icon ' path
  138.     Public InnerBorder ' yes | no
  139.     Public MinimizeButton ' yes | no
  140.     Public MaximizeButton ' yes | no
  141.     Public Scroll ' yes | no | auto
  142.     Public Selection ' yes | no
  143.     Public ShowInTaskbar ' yes | no
  144.     Public SysMenu ' yes | no
  145.     Public WindowState ' normal | minimize | maximize
  146.    
  147.     ' Form properties
  148.     Public Title
  149.     Public BackgroundImage
  150.     Public Width
  151.     Public Height
  152.     Public Left
  153.     Public Top
  154.     Public Self
  155.    
  156.     Dim oWnd
  157.     Dim oDoc
  158.     Dim bVisible
  159.     Dim oswHandlers
  160.     Dim oLastCreated
  161.    
  162.     Private Sub Class_Initialize()
  163.         Set Self = Me
  164.         Set oswHandlers = Nothing
  165.         Border = "thin"
  166.         ContextMenu = "no"
  167.         InnerBorder = "no"
  168.         MaximizeButton = "no"
  169.         Scroll = "no"
  170.         Selection = "no"
  171.     End Sub
  172.    
  173.     Private Sub Class_Terminate()
  174.         On Error Resume Next
  175.         oWnd.Close
  176.     End Sub
  177.    
  178.     Public Sub Create()
  179.         ' source http://forum.script-coding.com/viewtopic.php?pid=75356#p75356
  180.         Dim sName, sAttrs, sSignature, oShellWnd, oProc
  181.         sAttrs = ""
  182.         For Each sName In Array("Border", "Caption", "ContextMenu", "MaximizeButton", "Scroll", "Selection", "ShowInTaskbar", "Icon", "InnerBorder", "BorderStyle", "SysMenu", "WindowState", "MinimizeButton")
  183.             If Eval(sName) <> "" Then sAttrs = sAttrs & " " & sName & "=" & Eval(sName)
  184.         Next
  185.         If Len(sAttrs) >= 240 Then Err.Raise 450, "<HTA:APPLICATION" & sAttrs & " />"
  186.         sSignature = Mid(Replace(CreateObject("Scriptlet.TypeLib").Guid, "-", ""), 2, 16)
  187.         Set oProc = CreateObject("WScript.Shell").Exec("mshta ""about:<script>moveTo(-32000,-32000);document.title='*'</script><hta:application" & sAttrs & " /><object id='s' classid='clsid:8856F961-340A-11D0-A96B-00C04FD705A2'><param name=RegisterAsBrowser value=1></object><script>s.putProperty('" & sSignature & "',document.parentWindow);</script>""")
  188.         Do
  189.             If oProc.Status > 0 Then Err.Raise 507, "mshta.exe"
  190.             For Each oShellWnd In CreateObject("Shell.Application").Windows
  191.                 On Error Resume Next
  192.                 Set oWnd = oShellWnd.GetProperty(sSignature)
  193.                 If Err.Number = 0 Then
  194.                     On Error Goto 0
  195.                     With oWnd
  196.                         Set oDoc = .document
  197.                         With .document
  198.                             .open
  199.                             .close
  200.                             .title = Title
  201.                             .getElementsByTagName("head")(0).appendChild .createElement("style")
  202.                             .styleSheets(0).cssText = "* {font:8pt tahoma;position:absolute;}"
  203.                             .getElementsByTagName("body")(0).id = "Form"
  204.                         End With
  205.                         .Form.style.background = "buttonface"
  206.                         If BackgroundImage <> "" Then
  207.                             .Form.style.backgroundRepeat = "no-repeat"
  208.                             .Form.style.backgroundImage = "url(" & BackgroundImage & ")"
  209.                         End If
  210.                         If IsEmpty(Width) Then Width = .Form.offsetWidth
  211.                         If IsEmpty(Height) Then Height = .Form.offsetHeight
  212.                         .resizeTo .screen.availWidth, .screen.availHeight
  213.                         .resizeTo Width + .screen.availWidth - .Form.offsetWidth, Height + .screen.availHeight - .Form.offsetHeight
  214.                         If IsEmpty(Left) Then Left = CInt((.screen.availWidth - Width) / 2)
  215.                         If IsEmpty(Top) Then Top = CInt((.screen.availHeight - Height) / 2)
  216.                         bVisible = IsEmpty(bVisible) Or bVisible
  217.                         Visible = bVisible
  218.                         .execScript "var smallWrapperThunks = (function(){" &_
  219.                             "var thunks,elements={};return {" &_
  220.                                 "parseHandlers:function(h){" &_
  221.                                     "thunks=h;for(var key in thunks){var p=key.toLowerCase().split('_');if(p.length==2){elements[p[0]]=elements[p[0]]||{};elements[p[0]][p[1]]=key;}}}," &_
  222.                                 "forwardEvents:function(e){" &_
  223.                                     "if(elements[e.id.toLowerCase()]){for(var key in e){if(key.search('on')==0){var q=elements[e.id.toLowerCase()][key.slice(2)];if(q){eval(e.id+'.'+key+'=function(){thunks.'+q+'()}')}}}}}}})()"
  224.                         If Not oswHandlers Is Nothing Then
  225.                             .smallWrapperThunks.parseHandlers oswHandlers
  226.                             .smallWrapperThunks.forwardEvents .Form
  227.                         End If
  228.                     End With
  229.                     Exit Sub
  230.                 End If
  231.                 On Error Goto 0
  232.             Next
  233.             WScript.Sleep 100
  234.         Loop
  235.     End Sub
  236.    
  237.     Public Property Get Handlers()
  238.         Set Handlers = oswHandlers
  239.     End Property
  240.    
  241.     Public Property Set Handlers(oHandlers)
  242.         Dim oElement
  243.         If Not oswHandlers Is Nothing Then Set oswHandlers.oswForm = Nothing
  244.         Set oswHandlers = oHandlers
  245.         Set oswHandlers.oswForm = Me
  246.         If ChkDoc Then
  247.             oWnd.smallWrapperThunks.parseHandlers oswHandlers
  248.             For Each oElement In oDoc.all
  249.                 If oElement.id <> "" Then oWnd.smallWrapperThunks.forwardEvents oElement
  250.             Next
  251.         End If
  252.     End Property
  253.    
  254.     Public Sub ForwardEvents(oElement)
  255.         If ChkDoc Then oWnd.smallWrapperThunks.forwardEvents oElement
  256.     End Sub
  257.    
  258.     Public Function AddElement(sId, sTagName)
  259.         Set oLastCreated = oDoc.createElement(sTagName)
  260.         If VarType(sId) <> vbError Then
  261.             If Not(IsNull(sId) Or IsEmpty(sId)) Then oLastCreated.id = sId
  262.         End If
  263.         oLastCreated.style.position = "absolute"
  264.         Set AddElement = oLastCreated
  265.     End Function
  266.    
  267.     Public Function AppendTo(vNode)
  268.         If Not IsObject(vNode) Then Set vNode = oDoc.getElementById(vNode)
  269.         vNode.appendChild oLastCreated
  270.         ForwardEvents oLastCreated
  271.         Set AppendTo = oLastCreated
  272.     End Function
  273.    
  274.     Public Function AddText(sText)
  275.         oLastCreated.appendChild oDoc.createTextNode(sText)
  276.     End Function
  277.    
  278.     Public Property Get Window()
  279.         Set Window = oWnd
  280.     End Property
  281.    
  282.     Public Property Get Document()
  283.         Set Document = oDoc
  284.     End Property
  285.    
  286.     Public Property Get Visible()
  287.         Visible = bVisible
  288.     End Property
  289.    
  290.     Public Property Let Visible(bWindowVisible)
  291.         bVisible = bWindowVisible
  292.         If ChkDoc Then
  293.             If bVisible Then
  294.                 oWnd.moveTo Left, Top
  295.             Else
  296.                 oWnd.moveTo -32000, -32000
  297.             End If
  298.         End If
  299.     End Property
  300.    
  301.     Public Function ChkDoc()
  302.         On Error Resume Next
  303.         ChkDoc = CBool(TypeName(oDoc) = "HTMLDocument")
  304.     End Function
  305.    
  306. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement