WinMoveZ() : Moves, but confines a window to the work area of target monitor

Post your working scripts, libraries and tools
User avatar
SKAN
Posts: 820
Joined: 29 Sep 2013, 16:58

WinMoveZ() : Moves, but confines a window to the work area of target monitor

01 Jun 2020, 10:21

The function:

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
    
User avatar
Cerberus
Posts: 169
Joined: 12 Jan 2016, 15:46

Re: WinMoveZ() : Moves, but confines a window to the work area of target monitor

01 Jun 2020, 17:32

This looks very useful! great work. So it says "confine": suppose the window would be placed partly outside the screen, what would happen: Would the window keep the same size but be moved such that it is entirely inside the screen? Or will it be resized at the sides that partly fall outside the window? (I would be really interested in the latter for a script I'm working on.)
User avatar
Delta Pythagorean
Posts: 567
Joined: 13 Feb 2017, 13:44
GitHub: DelPyth
Location: Somewhere in the US

Re: WinMoveZ() : Moves, but confines a window to the work area of target monitor

01 Jun 2020, 18:50

With how compact you confine your functions, I'd almost expect you to create a full UI in less than a few lines :D

- [AHK].......: 1.1.33.02 Unicode 64-bit
- [OS].........: Windows 10.0.18362
- [GITHUB]...: github.com/DeltaPyth
- [PAYPAL]....: paypal.me/DelPyth
- [DISCORD]..: Delta#3324

Remember to use [code]CODE[/code] for your multi-line scripts.
Stay safe, stay inside, and remember to wash your hands for 20 seconds!
User avatar
SKAN
Posts: 820
Joined: 29 Sep 2013, 16:58

Re: WinMoveZ() : Moves, but confines a window to the work area of target monitor

01 Jun 2020, 18:54

@Cerberus
will it be resized at the sides that partly fall outside the window?
No
Would the window keep the same size but be moved such that it is entirely inside the screen?
Yes. This function is loosely based on the functionality of Tooltip command.
A Tooltip will never go offscreen. for eg: Tooltip, Hello World, % A_ScreenWidth-1, A_ScreenWidth-1
will confine it inside the right bottom of the primary monitor.

Also, if you run the following code on a multi-monitor setup..

Code: Select all

Loop
{
  Tooltip, Hello World
  Sleep 0
}
.. you will notice that the Tooltip is always confined to whichever monitor the mouse cursor is on... without losing its dimensions.
In other words, a Tooltip doesn't seem to span between monitors. It simply jumps over to the monitor the cursor is in.

Thanks for the feedback. :)
User avatar
SKAN
Posts: 820
Joined: 29 Sep 2013, 16:58

Re: WinMoveZ() : Moves, but confines a window to the work area of target monitor

01 Jun 2020, 19:20

Delta Pythagorean wrote:how compact you confine your functions,
Confine to 11 lines or less.. So that "Expand view" doesn't appear on the [code] box :D
User avatar
SirSocks
Posts: 204
Joined: 26 Oct 2018, 08:14

Re: WinMoveZ() : Moves, but confines a window to the work area of target monitor

01 Jun 2020, 22:15

@SKAN you write amazing code! This seems very useful! Thank you so much for sharing your code with the community. :clap:
By the way I can't get enough of xstr().

Return to “Scripts and Functions”

Who is online

Users browsing this forum: axibert, gwarble, PuzzledGreatly, robodesign, tidbit and 21 guests