在自动热键中获取可用的屏幕区域 [英] Get available screen area in autohotkey

查看:98
本文介绍了在自动热键中获取可用的屏幕区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一些简单的AutoHotkey脚本来移动窗口,但是我无法获取正确的屏幕尺寸值.

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.

我正在尝试获取屏幕上可用区域的大小(通常是全屏分辨率减去任务栏,也许还有其他停靠的窗口,例如Vista中的侧栏).我发现的两种方法都无法获得屏幕宽度.

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.

我发现获得屏幕尺寸的3种方法都没有给我正确的值.这是我正在使用的测试脚本(在XP上运行,其任务栏位于其默认大小的底部):

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):

#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使窗口占据整个分辨率,因此它与任务栏重叠.

#7 causes the window to take up the entire resolution, so it overlaps the taskbar.

#8导致宽度大于分辨率(我在辅助监视器上看到了窗口的右边缘)并且高度稍大,因此窗口与任务栏区域部分重叠.看起来这是正确的,除了不考虑边缘的窗户装饰.

#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.

#9似乎具有正确的宽度,但高度太短.看起来它是从分辨率的高度中减去任务栏的高度,然后再从中减去30个像素.

#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,但这感觉就像是在其他配置中会损坏的hack.似乎必须要有一种方法来正确获得可​​用的屏幕尺寸,但是我在AutoHotkey中找不到它.

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.

作为参考,这似乎可以满足我在Java中的需求:

For reference, this seems to give me what I need in Java:

Toolkit.getDefaultToolkit().getScreenSize();

推荐答案

我在XP中工作了.它目前仅考虑任务栏,因此,在侧栏可见时,它可能不会在Vista中做正确的事情.但是,无论工具栏在哪里,它都可以工作.

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.

这是我创建的方法,以及显示其用法的简单ResizeAndCenter方法:

Here are the methods I created, along with a simple ResizeAndCenter method that shows their use:

; 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
}

这篇关于在自动热键中获取可用的屏幕区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆