Skip to content

Instantly share code, notes, and snippets.

@ha2ne2
Created November 4, 2025 02:46
Show Gist options
  • Select an option

  • Save ha2ne2/d78798eb3687d60e3f1f35e6c71ceee4 to your computer and use it in GitHub Desktop.

Select an option

Save ha2ne2/d78798eb3687d60e3f1f35e6c71ceee4 to your computer and use it in GitHub Desktop.
; ------- keyshortcut.ahk (AHK v2) -------
; F13: 最大化 <-> 復元
F13::ToggleMaxRestore()
; F14: 最前面固定 <-> 解除
F14::ToggleTopmost()
; F15: 中央80%トグル
F15::CenterPctToggle()
; ==== 実装 ====
ToggleMaxRestore() {
hwnd := WinExist("A")
if !hwnd
return
isZoomed := DllCall("user32\IsZoomed", "ptr", hwnd, "int")
if (isZoomed) {
WinRestore(hwnd)
} else {
if (WinGetMinMax(hwnd) = 1) {
WinRestore(hwnd)
Sleep 10
}
WinMaximize(hwnd)
}
}
ToggleTopmost() {
hwnd := WinExist("A")
if !hwnd
return
HWND_TOPMOST := -1
HWND_NOTOPMOST := -2
SWP_NOSIZE := 0x1
SWP_NOMOVE := 0x2
SWP_NOACTIVATE := 0x10
ex := DllCall("user32\GetWindowLongW", "ptr", hwnd, "int", -20, "int") ; GWL_EXSTYLE=-20
isTop := (ex & 0x8) != 0 ; WS_EX_TOPMOST
DllCall("user32\SetWindowPos", "ptr", hwnd
, "ptr", isTop ? HWND_NOTOPMOST : HWND_TOPMOST
, "int", 0, "int", 0, "int", 0, "int", 0
, "uint", SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE)
}
; ウィンドウごとの元位置を記録
global CenterState := Map()
CenterPctToggle() {
; ===== 設定:ここだけ変えればOK =====
percent := 0.80 ; 0.80=80%(0.70 などに変更可)
; ====================================
hwnd := WinExist("A")
if !hwnd
return
; 現在位置
WinGetPos(&x, &y, &w, &h, hwnd)
if (w <= 0 || h <= 0)
return
; 所属モニタ(作業領域)=ウィンドウ中心で判定
cx := x + w // 2, cy := y + h // 2
mon := MonitorGetPrimary(), n := MonitorGetCount()
Loop n {
i := A_Index
MonitorGetWorkArea(i, &L, &T, &R, &B)
if (cx >= L && cx < R && cy >= T && cy < B) {
mon := i
break
}
}
MonitorGetWorkArea(mon, &L, &T, &R, &B)
; 目標(中央 percent%)
tW := Round((R - L) * percent)
tH := Round((B - T) * percent)
tX := L + ((R - L) - tW) // 2
tY := T + ((B - T) - tH) // 2
; ほぼ中央%か?(±2px)
near := Abs(x - tX) <= 2 && Abs(y - tY) <= 2 && Abs(w - tW) <= 2 && Abs(h - tH) <= 2
if (near && CenterState.Has(hwnd)) {
; 元に戻す
st := CenterState[hwnd] ; ← st は “オブジェクト” を入れてあります
if (st.WasMax)
WinMaximize(hwnd)
else
WinMove(st.X, st.Y, st.W, st.H, hwnd)
CenterState.Delete(hwnd)
return
}
; これから中央%に:状態保存(オブジェクトで保持)
wasMax := DllCall("user32\IsZoomed", "ptr", hwnd, "int")
if (wasMax) {
WinRestore(hwnd)
Sleep 10
WinGetPos(&x, &y, &w, &h, hwnd)
}
CenterState[hwnd] := { X: x, Y: y, W: w, H: h, WasMax: wasMax } ; ← ここを {} に
; 移動
WinMove(tX, tY, tW, tH, hwnd)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment