在VBScript中如何自定义设置msgbox弹窗的显示位置
VBScript设置MsgBox弹窗自定义坐标实现方案
前置说明
VBScript 原生MsgBox函数没有提供自定义弹窗显示坐标的参数,无法直接传入你生成的xpos、ypos参数调整位置,要实现需求可以用模拟弹窗的方案实现,效果和原生MsgBox一致,可直接在记事本中编写保存为.vbs文件运行。
另外你现有代码的xpos计算存在逻辑错误:计算xpos时依然调用了y轴的max、min变量,没有使用你定义的max1、min1,后面的实现代码已经修复了该问题。
完整实现代码
Dim max,min,ypos max=13000 min=0 Randomize ypos = Int((max-min+1)*Rnd+min) Dim max1,min1,xpos max1=23250 min1=0 Randomize ' 修复原代码xpos计算变量引用错误 xpos = Int((max1-min1+1)*Rnd+min) ' 构建自定义位置弹窗 Set ie = CreateObject("InternetExplorer.Application") ie.Navigate "about:blank" ie.ToolBar = 0 ie.StatusBar = 0 ie.Width = 300 ' 弹窗宽度可自定义 ie.Height = 150 ' 弹窗高度可自定义 ie.Left = xpos ie.Top = ypos ie.Document.Title = "Text" ' 对应原MsgBox的标题 ie.Document.Body.InnerHTML = "<p>Subtext</p><button onclick='document.all.ok.value=1;window.close()' style='width:80px;margin:20px auto;display:block'>确定</button>" ie.Document.Body.Style.TextAlign = "center" ie.Document.Body.Style.FontFamily = "微软雅黑" ie.Document.Body.Style.PaddingTop = "20px" ie.Document.Write "<input type='hidden' id='ok' value='0'>" ie.Visible = 1 ' 阻塞等待弹窗关闭,和原生MsgBox行为一致 Do While ie.Visible = True WScript.Sleep 100 Loop ' 可读取点击状态,和原代码box变量作用一致 box = ie.Document.All.ok.Value Set ie = Nothing
可选调整项
- 如果需要更多按钮类型(比如是/否、取消),只需要修改
innerHTML里的按钮代码和对应的返回值判断逻辑即可 - 弹窗的宽高、文字样式、边距都可以通过修改ie对象的属性和页面CSS自定义
内容的提问来源于stack exchange,提问作者Hudson Ding
相关产品推荐
相关产品推荐

