如何准确找到窗户尺寸和位置,包括装饰?

Mac*_*cha 47 command-line x11 window-geometry

我一直在尝试找出用于小脚本的窗口大小。我目前的技术是wmctrl -lG用来找出尺寸。然而,问题是这样的:

它给出的 x 和 y 数字用于窗口装饰的左上角,而高度和宽度仅用于内容区域。这意味着如果窗口装饰添加 20px 的高度和 2px 的宽度,wmctrl 将报告一个窗口为 640x480,即使它在屏幕上占据 660x482。这是一个问题,因为我的脚本的下一步是使用该区域来告诉 ffmpeg 记录屏幕。我想避免在当前设置中对窗口装饰的大小进行硬编码。

适合的是获取窗口装饰大小的方法,以便我可以使用它们来确定 640x480 内容区域的位置,或者直接获取内容区域位置的方法,而不是窗口装饰的位置.

Pet*_*r.O 45

以下脚本将为您提供左上角的屏幕坐标和窗口大小(没有任何装饰)。. . . xwininfo -id $(xdotool getactivewindow)包含足够的信息给你。


#!/bin/bash
# Get the coordinates of the active window's
#    top-left corner, and the window's size.
#    This excludes the window decoration.
  unset x y w h
  eval $(xwininfo -id $(xdotool getactivewindow) |
    sed -n -e "s/^ \+Absolute upper-left X: \+\([0-9]\+\).*/x=\1/p" \
           -e "s/^ \+Absolute upper-left Y: \+\([0-9]\+\).*/y=\1/p" \
           -e "s/^ \+Width: \+\([0-9]\+\).*/w=\1/p" \
           -e "s/^ \+Height: \+\([0-9]\+\).*/h=\1/p" )
  echo -n "$x $y $w $h"
#
Run Code Online (Sandbox Code Playgroud)

  • 不,这会返回几何 * 没有 * 装饰,这显然不是问题所问的。如果你不想包含装饰,有更简单的方法来做到这一点,例如`xdotool getactivewindow getwindowgeometry` (2认同)

小智 22

获取当前窗口大小和位置的更简单方法:

xdotool getwindowfocus getwindowgeometry
Run Code Online (Sandbox Code Playgroud)

并获取选定的窗口大小和位置:

xdotool selectwindow getwindowgeometry
Run Code Online (Sandbox Code Playgroud)

  • `xdotool getwindowfocus getwindowgeometry --shell` 也可用于解析。 (5认同)
  • 我相信这个答案不包括窗户装饰,因此没有回答这个问题。 (4认同)
  • @Jonathan `getwindowfocus` 作用于当前窗口。如果您想对任何窗口执行该命令,我认为您可能需要将该命令绑定到某个键盘快捷键(因为如果您从终端手动执行该命令,则始终会选择终端窗口)。 (2认同)

Col*_*nan 7

可以扩展接受的答案以获得整个窗口:

entire=false
x=0
y=0
w=0
h=0
b=0  # b for border
t=0  # t for title (or top)

# ... find out what user wants then 

eval $(xwininfo -id $(xdotool getactivewindow) |
  sed -n -e "s/^ \+Absolute upper-left X: \+\([0-9]\+\).*/x=\1/p" \
         -e "s/^ \+Absolute upper-left Y: \+\([0-9]\+\).*/y=\1/p" \
         -e "s/^ \+Width: \+\([0-9]\+\).*/w=\1/p" \
         -e "s/^ \+Height: \+\([0-9]\+\).*/h=\1/p" \
         -e "s/^ \+Relative upper-left X: \+\([0-9]\+\).*/b=\1/p" \
         -e "s/^ \+Relative upper-left Y: \+\([0-9]\+\).*/t=\1/p" )
if [ "$entire" = true ]
then                     # if user wanted entire window, adjust x,y,w and h
    let x=$x-$b
    let y=$y-$t
    let w=$w+2*$b
    let h=$h+$t+$b
fi
echo "$w"x"$h" $x,$y
Run Code Online (Sandbox Code Playgroud)

虽然很容易,但结果证明在 Ubuntu 14.04 中不适用于 Unity,因为相对信息全部为 0。我问在 Unity 中获取完整的窗口尺寸(包括装饰)并得到了很好的答案。这是我如何使用该答案:

aw=$(xdotool getactivewindow)
eval $(xwininfo -id "$aw" |
      sed -n -e "s/^ \+Absolute upper-left X: \+\([0-9]\+\).*/x=\1/p" \
             -e "s/^ \+Absolute upper-left Y: \+\([0-9]\+\).*/y=\1/p" \
             -e "s/^ \+Width: \+\([0-9]\+\).*/w=\1/p" \
             -e "s/^ \+Height: \+\([0-9]\+\).*/h=\1/p" )
if [ "$entire" = true ]
then
    extents=$(xprop _NET_FRAME_EXTENTS -id "$aw" | grep "NET_FRAME_EXTENTS" | cut -d '=' -f 2 | tr -d ' ')
    bl=$(echo $extents | cut -d ',' -f 1) # width of left border
    br=$(echo $extents | cut -d ',' -f 2) # width of right border
    t=$(echo $extents | cut -d ',' -f 3)  # height of title bar
    bb=$(echo $extents | cut -d ',' -f 4) # height of bottom border

    let x=$x-$bl
    let y=$y-$t
    let w=$w+$bl+$br
    let h=$h+$t+$bb
fi
Run Code Online (Sandbox Code Playgroud)

第二种方法适用于 Unity 和 Xfce,也适用于 Gnome。


Dag*_*elf 5

如果您想用鼠标选择窗口,只需运行

$ xwininfo
Run Code Online (Sandbox Code Playgroud)

然后单击您想要的窗口。它提供了大量信息和几何图形,其中包括可以复制粘贴到其他程序(例如 xterm)的格式的装饰。例如。

xwininfo: Please select the window about which you
          would like information by clicking the
          mouse in that window.

xwininfo: Window id: 0x160000d "watch"

  Absolute upper-left X:  1184
  Absolute upper-left Y:  215
  Relative upper-left X:  0
  Relative upper-left Y:  0
  Width: 484
  Height: 615
  Depth: 24
  Visual: 0x21
  Visual Class: TrueColor
  Border width: 0
  Class: InputOutput
  Colormap: 0x20 (installed)
  Bit Gravity State: NorthWestGravity
  Window Gravity State: NorthWestGravity
  Backing Store State: NotUseful
  Save Under State: no
  Map State: IsViewable
  Override Redirect State: no
  Corners:  +1184+215  -2812+215  -2812-1150  +1184-1150
  -geometry 80x47+1178+188
Run Code Online (Sandbox Code Playgroud)

然后您可以将以下内容放入 .xinitrc 或等效文件中:

xterm -geometry 80x47+1178+188 -bg black -fg white -T sensors -e watch -n10 -d "sensors;"
Run Code Online (Sandbox Code Playgroud)

-geometry几乎每个 X 应用程序都会解释该选项。

如果您没有获得准确的几何形状,那是因为相关窗口正在绘制窗口系统不知道的自己的装饰,因此您必须检查相关应用程序的设置,并进行相应调整。

在 Debian 上,这是来自:

$ dpkg -S xwininfo
x11-utils: /usr/bin/xwininfo
x11-utils: /usr/share/man/man1/xwininfo.1.gz
Run Code Online (Sandbox Code Playgroud)

所以就

$ apt install x11-utils
Run Code Online (Sandbox Code Playgroud)

如果你没有它。