2010andyliuxp 发表于 2021-4-22 14:17:32

VBS操作IE,如果让IE窗口最大化(难了我两天了)

要用IE作一个输入密码的登录窗口,可是总是无法让IE窗口激活至前台,总是在任务栏闪烁,
发送按键时灵时不灵,太不稳定,求教高手指点。
option explicit
Dim ObjWsh,objIe,StrIepw,flag
'Dim screenWidth,ScreenHeight,width,height
Set objIE = WScript.CreateObject("InternetExplorer.Application","Event_")
Set objWsh = WScript.CreateObject("wscript.shell")
'Const Readystate_complete = 4
With objIE   
.MenuBar = 0 '不显示IE对象菜单栏
.StatusBar = 0 '不显示IE对象状态栏
.AddressBar = 0 '不显示IE对象地址栏
.ToolBar = 0 '不显示IE对象工具栏
'.FullScreen=0 '全屏化IE对象
.Height = 250 '设置IE对象高度
.Top = 250 '设置IE对象高度
.left = 650 '设置IE对象高度
.Width = 281 '设置IE对象宽度
'        screenWidth = .document.parentWindow.screen.availWidth
'        screenHeight = .document.parentWindow.screen.availHeight
'        if .width>screenWidth then width=screenWidth
'        if .height>screenHeight then height=screenHeight
'        .Width = width
'        .Height = height
'        .left = Fix((screenWidth - width)/2)
'        .top = Fix((screenHeight - height)/2)
.Visible = 1 '设置是否可见
.Resizable = 0 '设置IE对象大小是否可以被改动
.Navigate "about:blank" '设置IE对象默认指向的页面
.Document.Write "<HTML><title>Login form</title><BODY scroll='no'><br /><br /><center>Please input password:<br /><br /><input type='password' id='px' password value=''><br /><br />"
.Document.Write "<center><input type=BUTTON name=certain id=certain value=Confim>"
End With
Set objIE.Document.getElementById("certain").OnClick=GetRef("ClickBtn")
'Objwsh.sendkeys "(^{esc}m)"
'do while objIe.busy or objIe.ReadyState <> Readystate_Complete
'        wscript.sleep 100
'Loop
shell.minimizeall
strIepw = ""
flag = true
WScript.Sleep 1500
Objwsh.sendkeys "%{TAB}"
'WScript.Sleep 1500
do while flag
if objwsh.appActivate("Login form - Internet Explorer") = true then
'Objwsh.sendkeys "{ENTER}"
flag = false
'Objwsh.sendkeys "% {TAB}"
''Objwsh.sendkeys "% {f4}"
Objwsh.sendkeys "{TAB}"
Objwsh.sendkeys "{TAB}"
else
        flag = true
        wscript.sleep 200
end if
loop
'Objwsh.sendkeys "abc"
'Set objIe.Document.Body.All.ok.OnClick = GetRef("OkClick") 'ok按钮的点击事件绑定"okClick"过程
Do
WScript.Sleep 200
Loop
Sub ClickBtn()
        strIepw = objIE.Document.getElementById("px").Value
'if objIE.Document.getElementById("px").Value <> "9527" then
''If objIe.Document.Body.All.px.Value <> "123456" Then
'strIepw = "密码错误"
'Else
'strIepw = "密码正确"
'End If
        if not isnull(strIePw) then msgbox strIepw
'objIe.Quit()
End Sub

Sub Event_OnQuit
        'MsgBox strIepw, vbSystemModal
WScript.Quit
End Sub
'set kw = oIE.Document.getElementById("pwd")
'kw.value = "test"
'
'Do
'WScript.Sleep 200
'Loop
'
'Sub aaa
'Dim str
'str = oIE.Document.getElementById("pwd").Value & VbCrLf
''str = str & oIE.Document.getElementById("input2").Value & VbCrLf
''str = str & oIE.Document.getElementById("input3").Value
'WScript.Echo str
'End Sub


2010andyliuxp 发表于 2021-4-22 14:25:40

本帖最后由 2010andyliuxp 于 2021-4-22 14:30 编辑

解决了,思路出了问题,一直在想将IE提至前台,
换个思路,把所有其它窗口最小化,IE不就在最前了吗?
呵呵,
Set shell = CreateObject("Shell.Application")
shell.ToggleDesktop
Set shell = Nothing

实测IE仍在任务栏闪烁,仍需提至前台。

2010andyliuxp 发表于 2021-4-22 14:27:07

2010andyliuxp 发表于 2021-4-22 14:25
解决了,思路出了问题,一直在想将IE提至前台,
换个思路,把所有其它窗口最小化,IE不就在最前了吗?
呵 ...

如果各位高人有什么不动系统桌面,将IE提到最顶层窗口的高招,还请指点。
这样用VBs作的系统,输入密码的问题算暂告一段落了。

liu_zhenfeng 发表于 2021-4-22 14:38:54

试试AutoHotKey

2010andyliuxp 发表于 2021-4-22 14:45:38

liu_zhenfeng 发表于 2021-4-22 14:38
试试AutoHotKey

不想再加第三方方案了,实在解决不了就算了,反正自己用,多点一下的事情。

2010andyliuxp 发表于 2021-4-22 14:50:15

根据实验的情况来看,这种方式启动的IE默认是最小化的。不知怎样把他变成常规窗口,模拟点击也可以,但不知怎样确定程式的坐标。

plusv 发表于 2021-4-22 15:38:20

本帖最后由 plusv 于 2021-4-22 15:56 编辑

Try SetForegroundWindow API.
A handle to the window that should be activated and brought to the foreground.




9zhmke 发表于 2021-4-22 15:45:24

这个界面也和你的想法差不多:http://bbs.wuyou.net/forum.php?mod=viewthread&tid=419719

2011slkj 发表于 2021-4-22 15:56:47

学习了你的代码部分

2010andyliuxp 发表于 2021-4-22 17:59:18

plusv 发表于 2021-4-22 15:38
Try SetForegroundWindow API.
A handle to the window that should be activated and brought to the for ...

这个不行呀,要借道OFFICE才可以,造成环境不通用。单纯vbs用不了API

2010andyliuxp 发表于 2021-4-22 18:00:26

9zhmke 发表于 2021-4-22 15:45
这个界面也和你的想法差不多:http://bbs.wuyou.net/forum.php?mod=viewthread&tid=419719

参考了一下,可惜他是用run的方法,用RUN的方法很简单,可是没有内建对象轻量。

2010andyliuxp 发表于 2021-4-22 18:01:44

这个脚本呢未来会去github开源,所以尽量想做得完美一些。

9zhmke 发表于 2021-4-22 19:24:46

2010andyliuxp 发表于 2021-4-22 18:00
参考了一下,可惜他是用run的方法,用RUN的方法很简单,可是没有内建对象轻量。

你仔细看,虽然是RUN了,但后面还是内建回来了的,因为直接调用IE会调用出被其他浏览器接管的默认浏览器,之所以要用RUN,目的是为了避开默认成其他浏览器后无法运行,不信的话你试试在默认的非IE浏览器上试试?

2010andyliuxp 发表于 2021-4-23 09:02:22

9zhmke 发表于 2021-4-22 19:24
你仔细看,虽然是RUN了,但后面还是内建回来了的,因为直接调用IE会调用出被其他浏览器接管的默认浏览器 ...

谢谢,其实我有看到,只是看到一大堆码,有点繁,不想研究。{:1_186:}
页: [1]
查看完整版本: VBS操作IE,如果让IE窗口最大化(难了我两天了)