【发布时间】:2016-04-25 21:17:03
【问题描述】:
我有一个多屏幕计算机系统。有时,出于我不明白的原因,对话框出现在错误的监视器上。例如,我将在监视器 A 中运行一个程序,然后在监视器 D 中打开一个 OK 框。这非常令人沮丧。
我在这里找到了一个名为“PositionDialogs.vbs”的 VBS 脚本:https://www.realtimesoft.com/ultramon/scripts/
Const SNAP_TO_MONITOR = False 'set this to True to ensure dialogs aren't placed between two monitors
Const INTERVAL = 2 'number of seconds the script waits before enumerating open windows again
Set sys = CreateObject("UltraMon.System")
Set wnd = CreateObject("UltraMon.Window")
Set wndParent = CreateObject("UltraMon.Window")
'create the two maps used to store positioned windows
Set arrAdd = CreateObject("Scripting.Dictionary")
Set arrLookup = CreateObject("Scripting.Dictionary")
Do While True
'enumerate all application windows
For Each w In wnd.GetAppWindows(True)
If w.HWndParent <> 0 Then
wndParent.HWnd = w.HWndParent
move = True
If arrLookup.Exists(w.HWnd) = True Then move = False
arrAdd.Add w.HWnd, 0
If move = True Then
If SNAP_TO_MONITOR = False Then
If w.Monitor <> wndParent.Monitor Then
w.Monitor = wndParent.Monitor
w.ApplyChanges 1 + 2 'WNDCHANGE_RESIZE_TO_FIT + WNDCHANGE_CLIP_TO_WORKSPACE
End If
Else
Set parentMon = sys.Monitors(wndParent.Monitor - 1)
parentLeft = parentMon.WorkLeft
parentTop = parentMon.WorkTop
parentRight = parentLeft + parentMon.WorkWidth
parentBottom = parentTop + parentMon.WorkHeight
dlgLeft = w.Left
dlgTop = w.Top
dlgRight = dlgLeft + w.Width
dlgBottom = dlgTop + w.Height
If dlgLeft < parentLeft Then
w.Left = parentLeft
ElseIf dlgRight > parentRight Then
w.Left = parentRight - w.Width
End If
If dlgTop < parentTop Then
w.Top = parentTop
ElseIf dlgBottom > parentBottom Then
w.Top = parentBottom - w.Height
End If
w.ApplyChanges 0
End If
End If
End If
Next
'swap maps, then clear arrAdd. this way we don't have entries for windows which no longer exist
Set temp = arrLookup
Set arrLookup = arrAdd
Set arrAdd = temp
Set temp = Nothing
arrAdd.RemoveAll
WScript.Sleep INTERVAL * 1000
Loop
这会将对话框移动到调用它的任何监视器。 我使用批处理文件在 Windows 启动时运行它,它作为一个进程运行。我的问题是,除非我单击 X 将其关闭,否则显示的控制台窗口不会消失。
bath 文件如下所示:
wscript PositionDialogs.vbs
exit
我假设我可以在脚本中添加一些内容以使其在将自身加载到内存后关闭?如果有,是什么?
【问题讨论】:
-
尝试使用
cscript而不是wscript... -
它连续运行。
-
然后在“开始-运行”对话框中输入(Winkey + R)
-
正如@Noodles 指出的那样,该脚本将连续运行。循环中没有条件可以打破它,因为循环条件只是
True,它将继续运行并运行。在循环中的某处,您需要定义Exit Do或将循环条件更改为在某个点退出循环。 -
来自the link的该脚本的描述 - “在与父应用程序相同的监视器上定位对话框窗口,例如文件打开对话框。脚本在后台持续运行,要终止它打开任务管理器,然后终止 wscript.exe 进程。需要 UltraMon 3.1.0 或更高版本。"
标签: batch-file vbscript