windows 在自动热键中获取可用的屏幕区域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/956224/
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
Get available screen area in autohotkey
提问by Herms
I'm trying to write a few simple AutoHotkey scripts for moving windows around, and I'm having trouble getting the correct screen size values.
我正在尝试编写一些简单的 AutoHotkey 脚本来移动窗口,但无法获得正确的屏幕尺寸值。
I'm trying to get the size of the usable area on the screen (generally the full screen resolution minus the taskbar, and maybe any other docked windows like the sidebar in Vista). Neither of the methods I've found for getting the screen width seems to work.
我正在尝试获取屏幕上可用区域的大小(通常是全屏分辨率减去任务栏,也可能是任何其他停靠的窗口,例如 Vista 中的侧边栏)。我发现的两种获取屏幕宽度的方法似乎都不起作用。
None of the 3 methods I found to get the screen size are giving me the right values. Here's the test script I'm using (running on XP with the taskbar on the bottom at its default size):
我发现的 3 种获取屏幕尺寸的方法都没有给我正确的值。这是我正在使用的测试脚本(在 XP 上运行,底部的任务栏以其默认大小运行):
#7::
WinMove A,,0,0,A_ScreenWidth,A_ScreenHeight
return
#8::
;SM_CXMAXIMIZED and SM_CYMAXIMIZED
SysGet, ScreenWidth, 61
SysGet, ScreenHeight, 62
WinMove A,,0,0,ScreenWidth,ScreenHeight
return
#9::
;SM_CXFULLSCREEN and SM_CYFULLSCREEN
SysGet, ScreenWidth, 16
SysGet, ScreenHeight, 17
WinMove A,,0,0,ScreenWidth,ScreenHeight
return
#7 causes the window to take up the entire resolution, so it overlaps the taskbar.
#7 导致窗口占据整个分辨率,因此它与任务栏重叠。
#8 causes the width to be larger than the resolution (I see the window's right edge show up on my secondary monitor) and the height is slightly too large, so the window partially overlaps the taskbar area. Looks like this is correct except for not taking into account the window decorations at the edges.
#8 导致宽度大于分辨率(我看到窗口的右边缘显示在我的辅助显示器上)并且高度稍微太大,因此窗口部分重叠任务栏区域。看起来这是正确的,除了没有考虑边缘的窗户装饰。
#9 seems to have the correct width, but the height is too short. It looks like it's subtracting the taskbar's height from the resolution's height, but then subtracting another 30 pixels from it.
#9 似乎具有正确的宽度,但高度太短。看起来它是从分辨率的高度中减去任务栏的高度,然后再减去 30 个像素。
I could just use what I have in #9 and add 30 to the height I get, but that feels too much like a hack that would break in other configurations. It seems like there has to be a way to get the available screen size properly, but I can't find it in AutoHotkey.
我可以使用我在 #9 中拥有的东西,并在我得到的高度上增加 30,但这感觉太像一个会在其他配置中崩溃的黑客。似乎必须有一种方法才能正确获取可用的屏幕尺寸,但我在 AutoHotkey 中找不到它。
For reference, this seems to give me what I need in Java:
作为参考,这似乎给了我我在 Java 中需要的东西:
Toolkit.getDefaultToolkit().getScreenSize();
回答by Herms
I got something working in XP. It currently only takes into consideration the taskbar, so it probably won't do the right thing in Vista when the sidebar is visible. However, it does work regardless of where the toolbar is.
我在 XP 中得到了一些工作。它目前只考虑任务栏,所以当侧边栏可见时它可能不会在 Vista 中做正确的事情。但是,无论工具栏在哪里,它都可以工作。
Here are the methods I created, along with a simple ResizeAndCenter method that shows their use:
以下是我创建的方法,以及一个简单的 ResizeAndCenter 方法,显示了它们的用法:
; Gets the edge that the taskbar is docked to. Returns:
; "top"
; "right"
; "bottom"
; "left"
GetTaskbarEdge() {
WinGetPos,TX,TY,TW,TH,ahk_class Shell_TrayWnd,,,
if (TW = A_ScreenWidth) { ; Vertical Taskbar
if (TY = 0) {
return "top"
} else {
return "bottom"
}
} else { ; Horizontal Taskbar
if (TX = 0) {
return "left"
} else {
return "right"
}
}
}
GetScreenTop() {
WinGetPos,TX,TY,TW,TH,ahk_class Shell_TrayWnd,,,
TaskbarEdge := GetTaskbarEdge()
if (TaskbarEdge = "top") {
return TH
} else {
return 0
}
}
GetScreenLeft() {
WinGetPos,TX,TY,TW,TH,ahk_class Shell_TrayWnd,,,
TaskbarEdge := GetTaskbarEdge()
if (TaskbarEdge = "left") {
return TW
} else {
return 0
}
}
GetScreenWidth() {
WinGetPos,TX,TY,TW,TH,ahk_class Shell_TrayWnd,,,
TaskbarEdge := GetTaskbarEdge()
if (TaskbarEdge = "top" or TaskbarEdge = "bottom") {
return A_ScreenWidth
} else {
return A_ScreenWidth - TW
}
}
GetScreenHeight() {
WinGetPos,TX,TY,TW,TH,ahk_class Shell_TrayWnd,,,
TaskbarEdge := GetTaskbarEdge()
if (TaskbarEdge = "top" or TaskbarEdge = "bottom") {
return A_ScreenHeight - TH
} else {
return A_ScreenHeight
}
}
ResizeAndCenter(w, h)
{
ScreenX := GetScreenLeft()
ScreenY := GetScreenTop()
ScreenWidth := GetScreenWidth()
ScreenHeight := GetScreenHeight()
WinMove A,,ScreenX + (ScreenWidth/2)-(w/2),ScreenY + (ScreenHeight/2)-(h/2),w,h
}
回答by Chris McFarland
Have you tried using SysGet's MonitorWorkArea sub-command?
http://www.autohotkey.com/docs/commands/SysGet.htm
您是否尝试过使用 SysGet 的 MonitorWorkArea 子命令?
http://www.autohotkey.com/docs/commands/SysGet.htm