Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Locking a window


  • Please log in to reply
1 reply to this topic
steliyan
  • Members
  • 48 posts
  • Last active: Jan 29 2015 04:14 PM
  • Joined: 20 Apr 2007
Is it possible to lock window position, or I should check the position every XX seconds and if it's not where it's supposed to be, to move it back?

Thanks.

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
This is based on one of Skan's TipsNTricks: How to Disable ( Grey-out ) the Close Button?
#F2::DisableMove()
#F3::RedrawSysMenu()

DisableMove(hWnd="") {
 If hWnd=
    hWnd:=WinExist("A")
 hSysMenu:=DllCall("GetSystemMenu","Int",hWnd,"Int",FALSE)
 DllCall("RemoveMenu","Int",hSysMenu,"UInt",1,"Uint",0x400)
 DllCall("DrawMenuBar","Int",hWnd)
Return ""
}

RedrawSysMenu(hWnd="") {
 If hWnd=
    hWnd:=WinExist("A")
 DllCall("GetSystemMenu","Int",hWnd,"Int",TRUE)
 DllCall("DrawMenuBar","Int",hWnd)
Return ""
}
It removes the "Move" item from the system menu of the active window, which also prevents it from being moved by click-dragging the titlebar. It assumes the Move item is the second item in the menu, so mightn't work with windows that have customized system menus (this isn't common.) Actually, it shouldn't be used with such windows anyway, since RedrawSysMenu() reverts the system menu to defaults - destroying any custom menu items.

Strangely, removing the Move item doesn't prevent you from moving Console (command prompt / "DOS") windows...


Alternatively, if you're trying to lock your own GUI window, you can use OnMessage() to block WM_SYSCOMMAND (0x112) when wParam is SC_MOVE (0xF010).

My first attempt at disabling move was this:
CoordMode, Mouse, Screen

~LButton::
    MouseGetPos, x, y, win
    SendMessage, 0x84,, (x & 0xFFFF) | (y & 0xFFFF) << 16,, ahk_id %win%
    ; If the mouse is over a titlebar, unclick to prevent move.
    if ErrorLevel = 2
       Click Up
return
This prevents click-drag when the mouse is over the titlebar. :) You can still move the window with the arrow keys, though, by selecting Move from the window's system menu.