首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >批处理文件如何运行程序并设置窗口的位置和大小?

批处理文件如何运行程序并设置窗口的位置和大小?
EN

Stack Overflow用户
提问于 2012-05-01 01:47:55
回答 4查看 109.6K关注 0票数 35

我有批处理文件,当我编写代码时,它为我设置桌面环境。该文件名为:SetEnv.cmd,它打开另外三个窗口:

  1. Windows资源管理器的一个实例,设置为应用服务器的部署目录。
  2. Windows资源管理器的第二个实例,该实例设置为写入部署文件的目录。
  3. 启动应用程序服务器的控制台窗口。

以下是SetEnv.cmd的内容

代码语言:javascript
运行
复制
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的内容

代码语言:javascript
运行
复制
cd C:\develop\jboss-4.2.3.GA\bin
run

每次我运行这个程序,我都要浪费时间重新排列和调整窗口大小。我不想运行最小化的窗口,因为在编写和测试代码时,我会与每个窗口进行多次交互。是否有任何方法可以控制从脚本中打开的窗口的位置和/或大小?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-05-01 02:03:11

尝试通过批处理文件从VBS (Windows脚本主机)脚本启动程序。如果您的VBS是这样的:

代码语言:javascript
运行
复制
'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可能是:

代码语言:javascript
运行
复制
@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
票数 5
EN

Stack Overflow用户

发布于 2018-09-23 19:36:00

对于不想下载第三方工具并拥有现代PowerShell (PS)的新用户来说,下面的方法是有用的。

脚本是基于帖子由Galler。所有的文件夹都有疑问,但你可以用你的。

使用PS脚本创建文件SetPositionAndSizeForExplorerWindow.ps1

代码语言:javascript
运行
复制
# 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的安全策略):

代码语言:javascript
运行
复制
Powershell -executionpolicy RemoteSigned -File "C:\develop\SetPositionAndSizeForExplorerWindow.ps1"

运行OpenDevelopEnv.bat

用于多显示器配置,您可以使用负坐标,例如:对于打开左侧显示窗口(相对于主显示器),您可以编写:

代码语言:javascript
运行
复制
[void][Win32]::MoveWindow($h1, -600, 400, 800, 400, $true)

Disadvantages:

  • 必须安装PowerShell;
  • 必须关闭所需的窗户;
  • 文件夹在单独的进程中启动;
  • 利用睡眠时间在慢速机器上正确工作。

在Windows 10 Pro 1803 x64/x86上使用PSVersion 5.1进行测试(管理员和常规用户)

票数 6
EN

Stack Overflow用户

发布于 2015-09-09 02:43:15

看看StartX。我没有使用它,但看起来您可以使用它来启动具有特定位置和大小的应用程序。

StartX,这是一个非常简单的实用程序,允许您从命令行调用CreateProcess API。

它的一些命令行参数:

代码语言:javascript
运行
复制
StartX ["title"] [/Dpath] [/MIN] [/MAX] [/Px,y] [/Scx1,cy1]

x,y:指定创建新窗口时窗口左上角的x和y偏移量(以像素为单位)。偏移量来自屏幕的左上角。 cx1,cy2:指定创建新窗口时窗口的宽度和高度(以像素为单位)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10392620

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档