我有批处理文件,当我编写代码时,它为我设置桌面环境。该文件名为:SetEnv.cmd
,它打开另外三个窗口:
以下是SetEnv.cmd
的内容
Explorer /n,c:\develop\jboss-4.2.3.GA\server\default\deploy
Explorer /n,c:\develop\Project\Mapping\deploy
cmd /c SetupEnvCmd.cmd
以下是SetupEnvCmd.cmd
的内容
cd C:\develop\jboss-4.2.3.GA\bin
run
每次我运行这个程序,我都要浪费时间重新排列和调整窗口大小。我不想运行最小化的窗口,因为在编写和测试代码时,我会与每个窗口进行多次交互。是否有任何方法可以控制从脚本中打开的窗口的位置和/或大小?
发布于 2012-05-01 02:03:11
尝试通过批处理文件从VBS (Windows脚本主机)脚本启动程序。如果您的VBS是这样的:
'FILENAME: SetEnv.vbs
Set Shell = WScript.CreateObject("WScript.Shell")
Shell.Run "Explorer /n,c:\develop\jboss-4.2.3.GA\server\default\deploy", 4, False
Shell.Run "Explorer /n,c:\develop\Project\Mapping\deploy", 4, False
4
意味着以其最近的大小/位置启动窗口。False
意味着它在执行脚本的下一行之前不会等待返回。不幸的是,这不能使您完全控制您的确切窗口大小/定位,但它应该记住最后的大小/定位。
更多信息在这里:Run.html
所以你的新SetEnv.cmd可能是:
@echo off
REM note there's a difference between cscript and wscript
REM wscript is usually the default launcher
cscript SetEnv.vbs
cd C:\develop\jboss-4.2.3.GA\bin
run
发布于 2018-09-23 19:36:00
对于不想下载第三方工具并拥有现代PowerShell (PS)的新用户来说,下面的方法是有用的。
脚本是基于帖子由Galler。所有的文件夹都有疑问,但你可以用你的。
使用PS脚本创建文件SetPositionAndSizeForExplorerWindow.ps1
:
# PowerShell script for opening Explorer windows and changing its position and size
# add .NET type
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Win32 {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string ClassName, IntPtr TitleApp);
}
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
"@
# set variable for RECT object
$rcWindow = New-Object RECT
#----- REPLACE WITH YOUR PATHs from HERE---------
# open first folder in separate Explorer process
explorer.exe "c:\develop\jboss-4.2.3.GA\server\default\deploy"
# magic sleep =) to wait starting explorer on slow machines
sleep -Milliseconds 1500
$h1 = (Get-Process | where {$_.mainWindowTitle -match "deploy"}).MainWindowHandle
# check if there is a window with this Title, if NO - return
if ($h1 -eq [IntPtr]::Zero) {return "Cannot find window with this Title"}
# bind RECT object with our window
[void][Win32]::GetWindowRect($h1,[ref]$rcWindow)
# set new position and size for window
[void][Win32]::MoveWindow($h1, 10, 10, 700, 345, $true)
# remember PID to exclude it for next window with the same name
$duplicate = (Get-Process | where {$_.mainWindowTitle -match "deploy"}).Id
# open second folder in separate Explorer process
explorer.exe "c:\develop\Project\Mapping\deploy"
sleep -Milliseconds 1500
$h1 = (Get-Process | where {$_.mainWindowTitle -match "deploy" -and $_.Id -ne $duplicate}).MainWindowHandle
if ($h1 -eq [IntPtr]::Zero) {return "Cannot find window with this Title"}
[void][Win32]::GetWindowRect($h1,[ref]$rcWindow)
[void][Win32]::MoveWindow($h1, 400, 20, 700, 335, $true)
# open cmd window with title SetupEnvCmd.cmd
cmd /c "start C:\develop\jboss-4.2.3.GA\bin\SetupEnvCmd.cmd"
sleep -Milliseconds 1500
# cmd.exe process has no own title, that's why we get PID searching by CommandLine process property
$process = "cmd.exe"
$cmdPID = Get-CimInstance Win32_Process -Filter "name = '$process'" | where {$_.CommandLine -match "SetupEnvCmd"} | select ProcessId
$h1 = (Get-Process | where {$_.Id -eq $cmdPID.ProcessId}).MainWindowHandle
if ($h1 -eq [IntPtr]::Zero) {return "Cannot find window with this Title"}
[void][Win32]::GetWindowRect($h1,[ref]$rcWindow)
[void][Win32]::MoveWindow($h1, 200, 400, 800, 400, $true)
把它移到C:\develop folder
。创建OpenDevelopEnv.bat
文件以运行此.ps1脚本(以避免深入研究PS的安全策略):
Powershell -executionpolicy RemoteSigned -File "C:\develop\SetPositionAndSizeForExplorerWindow.ps1"
运行OpenDevelopEnv.bat
用于多显示器配置,您可以使用负坐标,例如:对于打开左侧显示窗口(相对于主显示器),您可以编写:
[void][Win32]::MoveWindow($h1, -600, 400, 800, 400, $true)
Disadvantages:
在Windows 10 Pro 1803 x64/x86上使用PSVersion 5.1进行测试(管理员和常规用户)
发布于 2015-09-09 02:43:15
看看StartX。我没有使用它,但看起来您可以使用它来启动具有特定位置和大小的应用程序。
StartX,这是一个非常简单的实用程序,允许您从命令行调用CreateProcess API。
它的一些命令行参数:
StartX ["title"] [/Dpath] [/MIN] [/MAX] [/Px,y] [/Scx1,cy1]
x,y:指定创建新窗口时窗口左上角的x和y偏移量(以像素为单位)。偏移量来自屏幕的左上角。 cx1,cy2:指定创建新窗口时窗口的宽度和高度(以像素为单位)。
https://stackoverflow.com/questions/10392620
复制相似问题