WinMove to position according detected monitor's resolution Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

WinMove to position according detected monitor's resolution

Post by Avastgard » 19 Dec 2023, 07:49

I use my notebook on two different monitors, one at home (2560x1080) and one at work (1920x1080).

I modified @evilC's TapHoldManager script to make it so when I double tap F2, the last active windows explorer window gets placed in a certain position on the screen. Here is the code snippet:

Code: Select all

FuncF2(isHold, taps, state)
{
  if (taps)
  {
    if (taps == 1)
      Send {F2}
    else if (taps == 2)
       {
      WinMove, ahk_class CabinetWClass ahk_exe explorer.exe, ,1400, 225, , , ,
      ;~ WinMove, ahk_class CabinetWClass ahk_exe explorer.exe, ,970, 210, , , ,
      WinActivate, ahk_class CabinetWClass ahk_exe explorer.exe
    }
    return
  }
}
Since both monitors have different resolutions and I want the window to be placed roughly in the same position, I need to assign different coordinates on each monitor to achieve this. Line 9 is the position I use for my home monitor, whereas line 10 is the one I use for my work monitor.

Every time I switch from one monitor to the other, I have to comment out the other line so the positioning works fine.

I'm wondering if there is a way to make the code recognize what is the active monitor (or, better yet, the resolution of the active monitor, so it works on other monitors) and apply the corresponding WinMove coordinates accordingly. Is such a thing possible? Or is there a better way to achieve what I want?


Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: WinMove to position according detected monitor's resolution

Post by Avastgard » 19 Dec 2023, 08:39

Thanks as always, @mikeyww, I believe this code will work on both monitors (only tested on home monitor, though):

EDIT: tested on both ones and it works as intended.

Code: Select all

FuncF2(isHold, taps, state)
{
  if (taps)
  {
    if (taps == 1)
      Send {F2}
    else if (taps == 2)
    {
      If (A_ScreenWidth == 2560)
      WinMove, ahk_class CabinetWClass ahk_exe explorer.exe, ,1400, 225, , , ,
      else if (A_ScreenWidth == 1920)
      WinMove, ahk_class CabinetWClass ahk_exe explorer.exe, ,970, 210, , , ,
    WinActivate, ahk_class CabinetWClass ahk_exe explorer.exe
    }
    return
  }
}
Last edited by Avastgard on 28 Dec 2023, 08:48, edited 1 time in total.

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: WinMove to position according detected monitor's resolution  Topic is solved

Post by mikeyww » 19 Dec 2023, 08:54

Sure. Just a rearrangement of code here, nothing different in terms of effect.

Code: Select all

#Requires AutoHotkey v1.1.33
coord := {2560: [1400, 225]
        , 1920: [ 970, 210]}
If WinExist("ahk_class CabinetWClass") {
 WinMove coord[A_ScreenWidth][1], coord[A_ScreenWidth][2]
 WinActivate
}

Post Reply

Return to “Ask for Help (v1)”