[AHK] AutoHotKey 윈도우 실제 작업 영역 구하기 + coordmode 차이점
안녕하세요.
이번엔 오토핫키로 윈도우 작업 영역을 구하는 방법을 공유하고자 합니다.
오토핫키를 사용하다보면 coordmode에서 전체화면 모드 Screen으로 선언하고 작업을 합니다.
사실 일장일단이 있지만 전체화면 모드로 해야 헷갈리지 않고 좋더군요.
아무튼 이 방법은 창이 전체화면이던지 아니던지 문제가 없다는 장점이 있습니다.
또한 윈도우 테마(XP,Win7등)에 영향을 받지 않는다는 장점이 있죠.
그래서 어떤 환경에서든지 같은 결과를 만들어낸다는 장점이 있습니다.
인벤 피온3의 일락일협님이 제작하신 포메저장 프로그램도 오토핫키로 하는데,
단점은 전체화면에서만 제대로 작동한다는 점이죠.
이 방법을 사용한다면 전체화면이던 창모드이던 상관이 없게됩니다.
사실 자기만 쓸거면 이런건 쓸데가 없습니다.
하지만 다른 사람들도 사용하게 한다면 범용성을 생각해야하고 그래서 이런 방법도 강구하게 된거죠.
일단 CoordMode 부터 알아봅시다.
CoordMode의 AutoHotKey 공식 레퍼런스 설명입니다.
CoordMode
Sets coordinate mode for various commands to be relative to either the active window or the screen.
CoordMode, ToolTip|Pixel|Mouse|Caret|Menu [, Screen|Window|Client]
Parameters
Param1 | ToolTip: Affects ToolTip. Pixel: Affects PixelGetColor, PixelSearch, and ImageSearch. Mouse: Affects MouseGetPos, Click, and MouseMove/Click/Drag. Caret: Affects the built-in variables A_CaretX and A_CaretY. Menu: Affects the Menu Show command when coordinates are specified for it. |
Param2 | If Param2 is omitted, it defaults to Screen. Screen: Coordinates are relative to the desktop (entire screen). Relative: Coordinates are relative to the active window. Window [v1.1.05+]: Synonymous with Relative and recommended for clarity. Client [v1.1.05+]: Coordinates are relative to the active window's client area, which excludes the window's title bar, menu (if it has a standard one) and borders. Client coordinates are less dependent on OS version and theme. |
Remarks
If this command is not used, all commands except those documented otherwise (e.g. WinMove and InputBox) use coordinates that are relative to the active window.
Every newly launched thread (such as a hotkey, custom menu item, or timed subroutine) starts off fresh with the default setting for this command. That default may be changed by using this command in the auto-execute section (top part of the script).
Related
Click, MouseMove, MouseClick, MouseClickDrag, MouseGetPos, PixelGetColor, PixelSearch, ToolTip, Menu
Example
CoordMode, ToolTip, Screen ; Place ToolTips at absolute screen coordinates: CoordMode, ToolTip ; Same effect as the above because "screen" is the default.
툴팁 좌표 기준, 픽셀서치/이미지 서치의 좌표기준, 마우스 좌표기준 등을 지정합니다.
기준은 Screen(전체화면), Relative(상대적), Window(윈도창), Client(실제 작업 영역)
사실 Client로 기준을 잡으면 됩니다만 WinGetPos 명령에서 X,Y만 제대로 써먹을수 있을뿐 특히 Height(높이)값은 뭐가 뭔지 헷갈리고 Width나 Height를 활용하기 까다롭습니다.
제 수학적인 머리가 단차원적인지라 그냥 기본값(Defualt)인 Screen(전체화면) 기준이 가장 좋은것 같습니다.
간단하게 Screen, Relative, Window, Client의 X,Y값 차이를 알아보겠습니다.
GIF 파일로 만들어본 CoordMode 별 x,y 값 차이입니다.
실제 오토핫키로 매크로를 짜다보면 Relative나 Window, Client는 잘 사용되지 않더군요.
습관인지 타성에 쩔어서 그런지 만들어놓은 100여개의 ahk파일들 모두 Screen 기준으로 만들은걸 보면요.
아무튼 제가 사용하는 실제 윈도우 작업 영역 계산하는 방법입니다.
관건은 SysGet입니다.
시스템의 속성값이나 멀티모니터, 해상도등을 구하는 명령어인데요.
SysGet,wFrame,32
SysGet,wCaption,4
wFrame,wCaption은 변수 지정으로 구한 값을 변수로 저장하는거고
그 뒤에 32, 4 이 숫자는 ahk 도움말에 보면 리스트가 나옵니다.
32는 Border값, 4는 제목 표시줄 높이값입니다.
윈도우 창의 구성은 테두리+제목 표시줄(캡션)에 둘러쌓여있습니다.
따라서 WinGetPos로 얻은 값에서 (WinGetPos,x,y,w,h,A)
위 SysGet으로 얻은 테두리, 제목 표시줄 두께를 적절히 더하고 빼서 영역을 계산합니다.
X=x+wFrame
Y=y+wFrame+wCaption
Width=w-wFrame*2
Height=h-wCaption-wFrame*2
이렇게 계산하여 얻은 영역 좌표를 표시하면 아래와 같습니다.
분홍색 부분이 계산되어 나온 영역입니다.
이렇게 계산하여 나온 영역은 창을 최대화하던지 마음대로 크기를 조절하던지, 테마를 변경하여 테두리가 두껍던지 얇던지 상관없이 정확한 영역값으로 매크로 작업을 가능하게 합니다.
예제를 위해 만든 스크립트 코드입니다.
사용자 정의 함수 RealWinSize()를 WinGetPos 대신 사용하면 됩니다.
어떤크기던지, 최대화 상태 상관없이 정확한 작업영역을 계산할 수 있다면 어느 컴퓨터에서 실행하던 동일한 결과를 얻을 수 있습니다.
#NoEnv
#SingleInstance force
RealwinSize(ByRef Xpos,ByRef Ypos,ByRef Width,ByRef Height){
WinGetPos,x,y,w,h,A ; A는 현재 활성화된 창
SysGet,wFrame,32 ; 현재 시스템의 Border값 (창 테두리 두께)
SysGet,wCaption,4 ; 현재 시스템의 Caption값 (제목 표시줄 높이)
Xpos:=x+wFrame
Ypos:=y+wFrame+wCaption
Width:=w-wFrame*2
Height:=h-wCaption-wFrame*2
return
}
^Home::
RealwinSize(x,y,w,h)
Gui, 1:New
Gui, 1:-Caption -Border +AlwaysOnTop
Gui, 1:color, 0xFF44AA
Gui, 1:Show,% "x" x " y" y " w" w " h" h,Area
WinSet, Transparent, 60, Area
Sleep,5000
Gui, 1:Destroy
return