windows 通过命令行设置应用程序的窗口位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7977322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set The Window Position of an application via command line
提问by Inside Man
I have an application which start in 0x0 position of my desktop. I want to open it in center of my desktop. I do not want to open it and use a move command to move it into center, just want my app to start immediately in center position.
我有一个从桌面 0x0 位置开始的应用程序。我想在我的桌面中央打开它。我不想打开它并使用移动命令将它移动到中心,只想让我的应用程序立即在中心位置启动。
Is there any way to do this via command prompt? Any other way?
有没有办法通过命令提示符来做到这一点?还有什么办法吗?
采纳答案by sorpigal
You'll need an additional utility such as cmdow.exeto accomplish this. Look specifically at the /mov
switch. You can either launch your program from cmdow
or run it separately and then invoke cmdow
to move/resize it as desired.
您将需要一个额外的实用程序,例如cmdow.exe来完成此操作。具体看一下/mov
开关。您可以从中启动程序cmdow
或单独运行它,然后cmdow
根据需要调用以移动/调整它的大小。
回答by FuzzyWuzzy
Have found that AutoHotKeyis very good for window positioning tasks.
发现AutoHotKey非常适合窗口定位任务。
Here is an example script. Call it notepad.ahkand then run it from the command line or double click on it.
这是一个示例脚本。将其命名为 notepad.ahk,然后从命令行运行它或双击它。
Run, notepad.exe
WinWait, ahk_class Notepad
WinActivate
WinMove A,, 10, 10, A_ScreenWidth-20, A_ScreenHeight-20
It will start an application (notepad) and then adjust the window size so that it is centered in the window with a 10 pixel border on all sides.
它将启动一个应用程序(记事本),然后调整窗口大小,使其在窗口中居中,所有边都有 10 像素的边框。
回答by Bill K
I just found this question while on a quest to do the same thing.
我刚刚在寻求做同样的事情时发现了这个问题。
After some experimenting I came across an answer that works the way the OQ would want and is simple as heck, but not very general purpose.
经过一些试验,我发现了一个符合 OQ 想要的方式的答案,而且非常简单,但不是很通用。
Create a shortcut on your desktop or elsewhere (you can use the create-shortcut helper from the right-click menu), set it to run the program "cmd.exe" and run it. When the window opens, position it where you want your window to be. To save that position, bring up the properties menu and hit "Save".
在桌面或其他地方创建一个快捷方式(您可以使用右键菜单中的创建快捷方式助手),将其设置为运行程序“cmd.exe”并运行它。当窗口打开时,将其放置在您希望窗口所在的位置。要保存该位置,请调出属性菜单并点击“保存”。
Now if you want you can also set other properties like colors and I highly recommend changing the buffer to be a width of 120-240 and the height to 9999 and enable quick edit mode (why aren't these the defaults!?!)
现在,如果您愿意,您还可以设置其他属性,例如颜色,我强烈建议将缓冲区的宽度更改为 120-240,将高度更改为 9999 并启用快速编辑模式(为什么这些不是默认值!?!)
Now you have a shortcut that will work. Make one of these for each CMD window you want opened at a different location.
现在你有一个可以工作的快捷方式。为要在不同位置打开的每个 CMD 窗口制作其中一个。
Now for the trick, the windows CMD START command can run shortcuts. You can't programmatically reposition the windows before launch, but at least it comes up where you want and you can launch it (and others) from a batch file or another program.
现在为了诀窍,windows CMD START 命令可以运行快捷方式。您无法在启动前以编程方式重新定位窗口,但至少它出现在您想要的位置,并且您可以从批处理文件或其他程序启动它(和其他)。
Using a shortcut with cmd /c you can create one shortcut that can launch ALL your links at once by using a command that looks like this:
使用带有 cmd /c 的快捷方式,您可以创建一个快捷方式,该快捷方式可以使用如下所示的命令一次启动所有链接:
cmd /c "start cmd_link1 && start cmd_link2 && start cmd_link3"
This will open up all your command windows to your favorite positions and individually set properties like foreground color, background color, font, administrator mode, quick-edit mode, etc... with a single click. Now move that one "link" into your startup folder and you've got an auto-state restore with no external programs at all.
这将打开所有命令窗口到您喜欢的位置,并通过单击单独设置前景色、背景色、字体、管理员模式、快速编辑模式等属性。现在将那个“链接”移动到您的启动文件夹中,您就拥有了一个完全没有外部程序的自动状态恢复。
This is a pretty straight-forward solution. It's not general purpose, but I believe it will solve the problem that most people reading this question are trying to solve.
这是一个非常直接的解决方案。这不是通用目的,但我相信它会解决大多数阅读此问题的人试图解决的问题。
I did this recently so I'll post my cmd file here:
我最近做了这个,所以我会在这里发布我的 cmd 文件:
cd /d C:\shortucts
for %%f in (*.lnk *.rdp *.url) do start %%f
exit
Late EDIT: I didn't mention that if the original cmd /c command is run elevated then every one of your windows can (if elevation was selected) start elevated without individually re-prompting you. This has been really handy as I start 3 cmd windows and 3 other apps all elevated every time I start my computer.
后期编辑:我没有提到如果原始 cmd /c 命令运行提升,那么您的每个窗口都可以(如果选择了提升)开始提升,而无需单独重新提示您。这非常方便,因为我每次启动计算机时都会启动 3 个 cmd 窗口和 3 个其他应用程序。
回答by GCD56
Bill K.'s answer was the most elegant if you just want to start a window at startup or start from a shortcut on the desktop.
如果您只想在启动时启动一个窗口或从桌面上的快捷方式启动,Bill K. 的回答是最优雅的。
Just open the window where you want it, right click and choose properties. select Layout uncheck "let system position window" and click OK.
只需打开您想要的窗口,右键单击并选择属性。选择布局取消选中“让系统定位窗口”,然后单击“确定”。
Window will now open just where you want it. You can set font and window colors at the same time on other tabs. sweet.
窗口现在将在您想要的地方打开。您可以在其他选项卡上同时设置字体和窗口颜色。甜的。
回答by dgo
This probably should be a comment under the cmdow.exe
answer, but here is a simple batch file I wrote to allow for fairly sophisticated and simple control over all windows that you can see in the taskbar.
这可能应该是cmdow.exe
答案下的评论,但这是我编写的一个简单的批处理文件,用于对您可以在任务栏中看到的所有窗口进行相当复杂和简单的控制。
First step is to run cmdow /t
to display a list of those windows. Look at what the image name is in the column Image
, then command line:
第一步是运行cmdow /t
以显示这些窗口的列表。查看列中的图像名称是什么Image
,然后是命令行:
mycmdowscript.cmd imagename
Here are the contents of the batch file:
以下是批处理文件的内容:
:: mycmdowscript.cmd
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET IMAGE=%1
SET ACTION=/%2
SET REST=1
SET PARAMS=
:: GET ANY ADDITIONAL PARAMS AND STORE THEM IN A VARIABLE
FOR %%I in (%*) DO (
IF !REST! geq 3 (
SET PARAMS=!PARAMS! %%I
)
SET /A REST+=1
)
FOR /F "USEBACKQ tokens=1,8" %%I IN (`CMDOW /t`) DO (
IF %IMAGE%==%%J (
:: you now have access to the handle in %%I
cmdow %%I %ACTION% !PARAMS!
)
)
ENDLOCAL
@echo on
EXIT /b
example usage
示例用法
:: will set notepad to 500 500
mycmdowscript.cmd notepad siz 500 500
You could probably rewrite this to allow for multiple actions on a single command, but I haven't tried yet.
您可能会重写它以允许对单个命令执行多个操作,但我还没有尝试过。
For this to work, cmdow.exemust be located in your path. Beware that when you download this, your AV program might yell at you. This tool has (I guess) in the past been used by malware authors to manipulate windows. It is not harmful by itself.
为此,cmdow.exe必须位于您的路径中。请注意,当您下载此文件时,您的 AV 程序可能会对您大喊大叫。这个工具(我猜)过去曾被恶意软件作者用来操纵窗口。它本身并无害处。
回答by Etay Gudai 972-522-3322-47
Thanks To FuzzyWuzzy, set the following code ( Quick & Dirty Example for 1920x1080 screen resolution - without automatic width and height calculation or function use etc ) in AutoHotKeyto achive the following :
由于FuzzyWuzzy,设置以下代码(快速和肮脏的实施例为1920×1080的屏幕分辨率-无自动宽度和高度的计算或功能使用等)中的AutoHotkey才达到的情况如下:
v_cmd = c:\tempst_Monitor.ps1
Run, Powershell.exe -executionpolicy remotesigned -File %v_cmd%
SetTitleMatchMode 2
SetTitleMatchMode Fast
WinWait, PowerShell
Sleep, 1000
;A = Active window - [x,y,width,height]
WinMove A,, 0, 0,1920,500
v_cmd = c:\tempnd_Monitor.ps1
Run, Powershell.exe -executionpolicy remotesigned -File %v_cmd%
WinWait, PowerShell
Sleep, 1000
;A = Active window - [x,y,width,height]
WinMove A,, 0, 500,960,400
v_cmd = c:\temprd_Monitor.ps1
Run, Powershell.exe -executionpolicy remotesigned -File %v_cmd%
WinWait, PowerShell
Sleep, 1000
;A = Active window - [x,y,width,height]
WinMove A,, 960, 500,960,400
SMALL EDITsame code with Auto X / Y screen size calculation [ 4 monitors ], yet, can be used for 3 / 2 monitors as well.
SMALL EDIT与自动 X / Y 屏幕尺寸计算 [4 台显示器]相同的代码,但也可用于 3 / 2 台显示器。
Screen_X = %A_ScreenWidth%
Screen_Y = %A_ScreenHeight%
v_cmd = c:\tempst_Monitor.ps1
Run, Powershell.exe -executionpolicy remotesigned -File %v_cmd%
SetTitleMatchMode 2
SetTitleMatchMode Fast
WinWait, PowerShell
Sleep, 1000
;A = Active window - [x,y,width,height]
WinMove A,, 0, 0,Screen_X/2,Screen_Y/2
v_cmd = c:\tempnd_Monitor.ps1
Run, Powershell.exe -executionpolicy remotesigned -File %v_cmd%
WinWait, PowerShell
Sleep, 1000
;A = Active window - [x,y,width,height]
WinMove A,, Screen_X/2, 0,Screen_X/2,Screen_Y/2
v_cmd = c:\temprd_Monitor.ps1
Run, Powershell.exe -executionpolicy remotesigned -File %v_cmd%
WinWait, PowerShell
Sleep, 1000
;A = Active window - [x,y,width,height]
WinMove A,, 0, Screen_Y/2,Screen_X/2,Screen_Y/2
v_cmd = c:\tempth_Monitor.ps1
Run, Powershell.exe -executionpolicy remotesigned -File %v_cmd%
WinWait, PowerShell
Sleep, 1000
;A = Active window - [x,y,width,height]
WinMove A,, Screen_X/2, Screen_Y/2,Screen_X/2,Screen_Y/2
回答by user3260720
If you are happy to run a batch file along with a couple of tiny helper programs, a complete solution is posted here:
How can a batch file run a program and set the position and size of the window? - Stack Overflow (asked: May 1, 2012)
如果您乐于运行批处理文件和几个小帮助程序,这里发布了一个完整的解决方案:
批处理文件如何运行程序并设置窗口的位置和大小?- Stack Overflow(问:2012 年 5 月 1 日)