Autohotkey个人即用脚本分享(持续更新ing)

本楼为通用脚本

1. 快速选取并调用OnTopReplica置顶窗口局部

点击按钮后,左键画个区域,即可将该窗口这一区域置顶显示

1

24.9.12位置修复版

代码

注意,需要用到v2版uia库,下载地址:GitHub - Descolada/UIA-v2: UIAutomation library for AHK v2, based on thqby's UIA library

#Requires AutoHotkey v2
#include <UIA>
#Warn All, Off
#SingleInstance force

; Select Screen Region with Mouse
LButton:: ;Mouse to select with Red
{
	global titileH:=0
	Area := SelectScreenRegion("LButton", "Red", 30)
	WinGetPosEx(Area.id, &winX, &winY, &WinW, &winH, &Offset_X, &Offset_Y)
	;msgbox " " Offset_X " " Offset_Y
	;WinGetClientPos &winX, &winY, &WinW, &winH, "ahk_id " Area.id

    h:=0
    try
    {
    El := UIA.ElementFromHandle("ahk_id " Area.id)
    loc:=El.ElementFromPath("c").BoundingRectangle
    x:=loc.l, y:=loc.t, w:=(loc.r-loc.l), h:=(loc.b-loc.t)
    ;msgbox(h)
    }
  tooltip h
  Run '"G:\Program Files (x86)\OnTopReplica\OnTopReplica\OnTopReplica.exe" --windowId=' Area.id ' --size=' Area.W ',' Area.H ' --position=' Area.X-2 ',' Area.Y-2 ' --opacity=200 --chromeOff --region=' Area.X-winX-1 ',' Area.Y-winY-h-2 ',' Area.W+2 ',' Area.H+2
  exitapp
}


SelectScreenRegion(Key, Color := "Lime", Transparent:= 80)
{
	CoordMode("Mouse", "screen")
	MouseGetPos(&sX, &sY, &winid)
	ssrGui := Gui("+AlwaysOnTop -caption +Border +ToolWindow +LastFound -DPIScale")
	WinSetTransparent(Transparent)
	ssrGui.BackColor := Color
	Loop
	{
		Sleep 10
		MouseGetPos(&eX, &eY)
		W := Abs(sX - eX), H := Abs(sY - eY)
		X := Min(sX, eX), Y := Min(sY, eY)
		ssrGui.Show("x" X " y" Y " w" W " h" H)
	} Until !GetKeyState(Key, "p")
	ssrGui.Destroy()
	Return { X: X, Y: Y, W: W, H: H, X2: X + W, Y2: Y + H ,id: winid}
}

/*OnTopReplica支持的所有参数
      --windowId=HWND        Window handle (HWND) to be cloned.
      --windowTitle=TITLE    Partial TITLE of the window to be cloned.
      --windowClass=CLASS    CLASS of the window to be cloned.
  -v, --visible              If set, only clones windows that are visible.
      --size=WIDTH,HEIGHT    Target WIDTH,HEIGHT of the cloned thumbnail, or
      --width=VALUE          Target WIDTH of cloned thumbnail, or
      --height=VALUE         Target HEIGHT of cloned thumbnail.
      --position=X,Y         Target X,Y of the OnTopReplica window.
      --screenPosition=TR|TL|C|BR|BL
                             Resolution independent window position on
                               current screen, with locking. Values:
                               TR|TL|C|BR|BL.
  -r, --region=X,Y,W,H       Region X,Y,W,H of the cloned window.
  -p, --padding=LEFT,TOP,RIGHT,BOTTOM
                             Region padding LEFT,TOP,RIGHT,BOTTOM of the
                               clone.
  -o, --opacity=0-255        Opacity of the window: 0-255.
      --clickForwarding      Enables click forwarding.
      --clickThrough         Enables click through.
      --chromeOff            Disables the window's chrome (border).
      --fs, --fullscreen     Starts up in fullscreen mode.
  -h, --help, -?             Show command line help.
	*/


	;------------------------------
;
; Function: WinGetPosEx
;
; Description:
;
;   Gets the position, size, and offset of a window. See the *Remarks* section
;   for more information.
;
;   https://www.autohotkey.com/boards/viewtopic.php?f=6&t=3392
;
; Parameters:
;
;   hWindow - Handle to the window.
;
;   X, Y, Width, Height - Output variables. [Optional] If defined, these
;       variables contain the coordinates of the window relative to the
;       upper-left corner of the screen (X and Y), and the Width and Height of
;       the window.
;
;   Offset_X, Offset_Y - Output variables. [Optional] Offset, in pixels, of the
;       actual position of the window versus the position of the window as
;       reported by GetWindowRect.  If moving the window to specific
;       coordinates, add these offset values to the appropriate coordinate
;       (X and/or Y) to reflect the true size of the window.
;
; Returns:
;
;   If successful, a RECTPlus buffer object is returned.
;   The first 16 bytes contains a RECT structure that contains the dimensions of the
;   bounding rectangle of the specified window.
;   The dimensions are given in screen coordinates that are relative to the upper-left
;   corner of the screen.
;   The next 8 bytes contain the X and Y offsets (4-byte integer for X and
;   4-byte integer for Y).
;
;   Also if successful (and if defined), the output variables (X, Y, Width,
;   Height, Offset_X, and Offset_Y) are updated.  See the *Parameters* section
;   for more more information.
;
;   If not successful, FALSE is returned.
;
; Requirement:
;
;   Windows 2000+
;
; Remarks, Observations, and Changes:
;
; * Starting with Windows Vista, Microsoft includes the Desktop Window Manager
;   (DWM) along with Aero-based themes that use DWM.  Aero themes provide new
;   features like a translucent glass design with subtle window animations.
;   Unfortunately, the DWM doesn't always conform to the OS rules for size and
;   positioning of windows.  If using an Aero theme, many of the windows are
;   actually larger than reported by Windows when using standard commands (Ex:
;   WinGetPos, GetWindowRect, etc.) and because of that, are not positioned
;   correctly when using standard commands (Ex: gui Show, WinMove, etc.).  This
;   function was created to 1) identify the true position and size of all
;   windows regardless of the window attributes, desktop theme, or version of
;   Windows and to 2) identify the appropriate offset that is needed to position
;   the window if the window is a different size than reported.
;
; * The true size, position, and offset of a window cannot be determined until
;   the window has been rendered.  See the example script for an example of how
;   to use this function to position a new window.
;
; * 20150906: The "dwmapi\DwmGetWindowAttribute" function can return odd errors
;   if DWM is not enabled.  One error I've discovered is a return code of
;   0x80070006 with a last error code of 6, i.e. ERROR_INVALID_HANDLE or "The
;   handle is invalid."  To keep the function operational during this types of
;   conditions, the function has been modified to assume that all unexpected
;   return codes mean that DWM is not available and continue to process without
;   it.  When DWM is a possibility (i.e. Vista+), a developer-friendly messsage
;   will be dumped to the debugger when these errors occur.
;
; Credit:
;
;   Idea and some code from *KaFu* (AutoIt forum)
;
;-------------------------------------------------------------------------------
WinGetPosEx(hWindow, &X := "", &Y := "", &Width := "", &Height := "", &Offset_X := "", &Offset_Y := "") {
   Static S_OK := 0x0,
          DWMWA_EXTENDED_FRAME_BOUNDS := 9
   ;-- Get the window's dimensions
   ;   Note: Only the first 16 bytes of the RECTPlus structure are used by the
   ;   DwmGetWindowAttribute and GetWindowRect functions.
   RECTPlus := Buffer(24,0)
   Try {
      DWMRC := DllCall("dwmapi\DwmGetWindowAttribute",
                       "Ptr",  hWindow,                     ;-- hwnd
                       "UInt", DWMWA_EXTENDED_FRAME_BOUNDS, ;-- dwAttribute
                       "Ptr",  RECTPlus,                    ;-- pvAttribute
                       "UInt", 16,                          ;-- cbAttribute
                       "UInt")
   }
   Catch {
      Return False
   }
   ;-- Populate the output variables
   X := NumGet(RECTPlus,  0, "Int") ; left
   Y := NumGet(RECTPlus,  4, "Int") ; top
   R := NumGet(RECTPlus,  8, "Int") ; right
   B := NumGet(RECTPlus, 12, "Int") ; bottom
   Width    := R - X ; right - left
   Height   := B - Y ; bottom - top
   OffSet_X := 0
   OffSet_Y := 0
   ;-- Collect dimensions via GetWindowRect
   RECT := Buffer(16, 0)
   DllCall("GetWindowRect", "Ptr", hWindow,"Ptr", RECT)
   ;-- Right minus Left
   GWR_Width := NumGet(RECT,  8, "Int") - NumGet(RECT, 0, "Int")
   ;-- Bottom minus Top
   GWR_Height:= NumGet(RECT, 12, "Int") - NumGet(RECT, 4, "Int")
   ;-- Calculate offsets and update output variables
   NumPut("Int", Offset_X := (Width  - GWR_Width)  // 2, RECTPlus, 16)
   NumPut("Int", Offset_Y := (Height - GWR_Height) // 2, RECTPlus, 20)
   Return RECTPlus
}
4 个赞