Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Centering a window?


  • Please log in to reply
6 replies to this topic
master131
  • Guests
  • Last active:
  • Joined: --
How can I center a window named Modern Warfare 2?

I've been playing around with the example here:
http://www.autohotke...nds/WinMove.htm

But I don't get it :D

DarkVamprism
  • Members
  • 125 posts
  • Last active: Apr 25 2017 10:28 AM
  • Joined: 03 Sep 2009
You could try this. Its untested but I think it should work

WinGetPos,,,ModW, ModH, Modern Warfare 2
NewX := (A_ScreenWidth / 2) - (ModW / 2)
NewY := (A_ScreenHeight / 2) - (ModH / 2)
WinMove, Modern Warfare,, %NewX%, %NewY%


Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
I use this handy function;
; Example:
; 1::CenterWin(WinExist("A"))	; centers active window

CenterWin(WinID) {	; by Learning one. Centers window on screen
	oldDHW := A_DetectHiddenWindows 
	DetectHiddenWindows, on
	WinGetPos,,, W, H, ahk_id %WinID%
	SysGet, WA, MonitorWorkArea
	WAWidth := WAright - WAleft, WAHeight := WABottom - WAtop
	WinMove, ahk_id %WinID%,, (WAWidth - w)/2, (WAHeight - h)/2
	DetectHiddenWindows, %oldDHW%
}


DarkVamprism
  • Members
  • 125 posts
  • Last active: Apr 25 2017 10:28 AM
  • Joined: 03 Sep 2009

; Example: 
; 1::CenterWin(WinExist("A"))   ; centers active window 

CenterWin(WinID) {   ; by Learning one. Centers window on screen 
   oldDHW := A_DetectHiddenWindows 
   DetectHiddenWindows, on 
   WinGetPos,,, W, H, ahk_id %WinID% 
   SysGet, WA, MonitorWorkArea 
   WAWidth := WAright - WAleft, WAHeight := WABottom - WAtop 
   WinMove, ahk_id %WinID%,, (WAWidth - [color=red]W[/color])/2, (WAHeight - [color=red]H[/color])/2
   DetectHiddenWindows, %oldDHW% 
}


Highlighted in red is the corrections,
If I remember right, both need to be the same casing

  • Guests
  • Last active:
  • Joined: --

Highlighted in red is the corrections,
If I remember right, both need to be the same casing


:?: :?: :?:

Did you try it both ways?

DarkVamprism
  • Members
  • 125 posts
  • Last active: Apr 25 2017 10:28 AM
  • Joined: 03 Sep 2009
I stand corrected. I couldn't test it before because my PC wouldn't work but turns out casing didn't matter, Although it is a good habit to use the same casing if referring to the same variable.

But anyway. Sorry Learning one :)

MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009
Related (a function that I've create a dew days ago):
WinMoveSpecial(p_Pos="",p_WinTtl="",p_WinTxt="",p_ExcTtl="",p_ExcTxt="") {
  WinGetPos,,,l_W,l_H,%p_WinTtl%,%p_WinTxt%,%p_ExcTtl%,%p_ExcTxt%
  l_X:=InStr(p_Pos,"LEFT",0) ? 0 : InStr(p_Pos,"RIGHT",0) ? A_ScreenWidth-l_W : (A_ScreenWidth-l_W)/2
  l_Y:=InStr(p_Pos,"TOP",0) ? 0 : InStr(p_Pos,"BOTTOM",0) ? A_ScreenHeight-l_H : (A_ScreenHeight-l_H)/2
  WinMove,%p_WinTtl%,%p_WinTxt%,%l_X%,%l_Y%,,,%p_ExcTtl%,%p_ExcTxt%
}

Usage:

First parameter: TOP | TOPRIGHT | RIGHT | BOTTOMRIGHT | BOTTOM | BOTTOMLEFT | LEFT | TOPLEFT | CENTER
(anything else will result in CENTER)
The other parameters: WinTitle, WinText, ExcludeTitle, ExcludeText


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio Fran├ža -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.