Code: Select all
WinMoveZ(hWnd, C, X, Y, W, H, Redraw:=0) { ; WinMoveZ v0.5 by SKAN on D35V/D361 @ tiny.cc/winmovez
Local V:=VarSetCapacity(R,48,0), A:=&R+16, S:=&R+24, E:=&R, NR:=&R+32, TPM_WORKAREA:=0x10000
C:=( C:=Abs(C) ) ? DllCall("SetRect", "Ptr",&R, "Int",X-C, "Int",Y-C, "Int",X+C, "Int",Y+C) : 0
DllCall("SetRect", "Ptr",&R+16, "Int",X, "Int",Y, "Int",W, "Int",H)
DllCall("CalculatePopupWindowPosition", "Ptr",A, "Ptr",S, "UInt",TPM_WORKAREA, "Ptr",E, "Ptr",NR)
X:=NumGet(NR+0,"Int"), Y:=NumGet(NR+4,"Int")
Return DllCall("MoveWindow", "Ptr",hWnd, "Int",X, "Int",Y, "Int",W, "Int",H, "Int",Redraw)
}
WinMoveZ(hWnd, C, X, Y, W, H [, Redraw])
Moves a window like WinMove, but confines the window within the work area of the target monitor.
- What WinMoveZ() will not do? : It will not move a window offscreen
- What WinMoveZ() will do? : Confine a window within the work area of target monitor.
- Which target monitor? : Whichever monitor POINT (X, Y) belongs to
- What if POINT doesn't belong to any monitor? : The monitor nearest to the POINT will house the window.
- Can you explain parameters? : Except second parameter others are passed to MoveWindow which is similar to WinMove
DllCall("MoveWindow", "Ptr",hWnd, "Int",X, "Int",Y, "Int",W, "Int",H, "Int",Redraw) - What is the second parameter C required for? : It is like a social distancing (in pixels) to be maintained between cursor hotspot and your POINT (X, Y)
This parameter should be used only if you're calling WinMoveZ() from a loop/timer, to move a window to follow your mouse. (Try example #2)
A value of 16 should be good. That is, 16x16 = 256 pixels surrounding the cursor hotspot will be excluded from work area. - Bordered windows are not moved correctly? : Apparently. I will update the topic if I find my own solution.
- Example #1 : Show a notification at right bottom corner of primary monitor
Code: Select all
#SingleInstance, Force Gui, New, -Caption +Border +AlwaysOnTop +Disabled +Owner +hWndhWnd Gui, Font, s20, Segoe UI Gui, Add, Text,, Hello World ! Gui, Show, Hide DetectHiddenWindows, On WinGetPos, X, Y, W, H, ahk_id %hWnd% WinMoveZ(hWnd, 0, A_ScreenWidth-1, A_ScreenHeight-1, W, H) DetectHiddenWindows, Off Gui, Show, NA Return
- Example #2 : A window follows the mouse while maintaining a 16 pixels distance.
Code: Select all
#NoEnv #SingleInstance, Force CoordMode, Mouse, Screen CoordMode, Pixel, Screen Gui New, -Caption +Border +hWndhWnd +Disabled +AlwaysOnTop Gui, Margin, 15, 30 Gui, Add, Edit, w60 Center, FFFFFF Gui, Show WinGetPos, X, Y, W, H, ahk_id %hWnd% PX:=X, PY:=Y Loop { MouseGetPos, X, Y If ! (X=PX and Y=PY) { WinMoveZ(hWnd, 16, X, Y, W, H), PX:=X, PY:=Y PixelGetColor, C, %X%, %Y%, RGB Gui, Color, % PC:=C GuiControl,,Edit1, % Format("{:06X}",C) } Sleep 50 } F2::Gui +AlwaysOnTop Esc:: ExitApp