This is my first released AHK script. It's just something I put together for my own use and thought others out there might find it helpful, or as a starting place to make their own. Right now it's just a script, no GUI, and is only meant for use with one or two monitors. It was developed on a 16:9 monitor and I'm not sure how it will handle on monitors with other aspect ratios, but if you find anything wonky please let me know.
Background:
I wanted to use Vista's Windows Media Center to watch TV while I'm doing other things with my computer including playing full-screen games. I have a 37" 1080p HDTV that has multiple inputs which I've hooked up so I can essentially use its PIP function as a second monitor. Windows Media Center has a very slick interface but there are some huge holes in its usability from a practical standpoint. It does not play nicely in full-screen mode, locking your mouse to its window if you have multiple monitors for example, and there's a lengthy delay whenever you try to Alt-Tab to escape its grasp. Also, Windows in general won't let you view two full-screen applications at the same time even if you have multiple monitors. One has to be windowed, and since games tend to run better when they're full-screen, that's another reason to run Media Center in a window. But how to make it at least look like it's full-screen on the second monitor? Well it's possible with AHK to remove the window borders and titlebar, as well as set its size and position.
I also wanted to be able to mute only Media Center, leaving my other programs and games unmuted. This is also possible in Vista, using the Volume Mixer and AHK to control it.
Finally there were a few issues with running Media Center in a window that I had to work around in order to have as seamless an experience as possible. First of all what happens to the window on the secondary monitor when the resolution on the primary monitor changes (e.g. when launching a full-screen game that is set at a lower resolution for performance reasons)? It shifts, rather annoyingly, and gets split between the two monitors. The other problem was how the cursor never goes away when it's hovering over a Media Center window. The normal behavior in full-screen is that the cursor disappears after ~5 seconds. Well I was able to work around both of these issues, at least to the best of my limited AHK scripting abilities.
Features:
- Virtual full-screen modes provide seamless mouse movement across screens and allows full-screen Media Center usage on second monitor while playing full-screen games on primary monitor
- Causes the Mute button to only affect Windows Media Center as long as it's running. Works without stealing focus from active application/game window, using a minimized Volume Mixer window. Automatically launches and closes the Volume Mixer when Media Center is launched/closed.
- Uses a context-specific hotkey (default: Shift-3) to cycle between 3 different windowed modes, 4 if you have a secondary monitor:
- Virtual full-screen on primary monitor (full-size window without borders)
- Small window in the bottom-right corner (windowed with borders)
- Small window in the bottom-right corner (windowed without borders)
- Virtual full-screen on secondary monitor (full-size window without borders)
- Listens for resolution changes on the primary monitor and automatically adjusts the positioning of the Media Center window to fit properly when using the virtual full-screen mode on secondary monitor.
- Hides the cursor when it hovers over the Media Center window for 5 seconds.
To do/wishlist:
- Create a GUI for customization and ease of use
- More/different windowing modes/placements
- Ability to intercept remote control keys and pass them to Media Center without stealing focus from active window.
- Ability to adjust mixer volumes and muting with hotkeys (master, active window, and Media Center)
- Compatibility with 3+ monitors
If anyone has any ideas or code samples on how to do any of the above, I would really be interested!
And if you have feedback/questions/problems regarding this script just post, PM or email!
Finally of course, here is the code so far. Feel free to customize it or use it any way you wish. I only ask that you please post your improvements/tweaks to the code here for all to see and use.
Code:
/*
Vista Media Center Multitool
Created by Blahman (blah238 at gmail dot com)
v0.1
11/9/2008
Features:
- Virtual full-screen modes provide seamless mouse movement across screens and allows full-screen Media Center usage on second monitor while playing full-screen games on primary monitor
- Causes the Mute button to only affect Windows Media Center as long as it's running. Works without stealing focus from active application/game window, using a minimized Volume Mixer window. Automatically launches and closes the Volume Mixer when Media Center is launched/closed.
- Uses a context-specific hotkey (default: Shift-3) to cycle between 3 different windowed modes, 4 if you have a secondary monitor:
- Virtual full-screen on primary monitor (full-size window without borders)
- Small window in the bottom-right corner (windowed with borders)
- Small window in the bottom-right corner (windowed without borders)
- Virtual full-screen on secondary monitor (full-size window without borders)
- Listens for resolution changes on the primary monitor and automatically adjusts the positioning of the Media Center window to fit properly when using the virtual full-screen mode on secondary monitor.
- Hides the cursor when it hovers over the Media Center window for 5 seconds.
Feel free to customize this code or use it any way you wish. I only ask that you please post your improvements/tweaks to the code on the AHK forums for all to see and use.
*/
#SingleInstance, Force
SetDefaultMouseSpeed, 0
CoordMode, Mouse, Screen
cycle = 1 ; counter to keep track of the windowing mode to use next in a cycle
WaitForWMC:
Process, Wait, ehshell.exe ; Waits for Windows Media Center to be run
WMCPID = %ErrorLevel%
Process, Exist, SndVol.exe ; Vista's Volume Mixer is used to mute Media Center independently from other apps
SndVolPID = %ErrorLevel%
If SndVolPID = 0
Run, SndVol.exe, , Min, SndVolPID
ScrW := A_ScreenWidth
ScrH := A_ScreenHeight
SetTimer, CheckForResChange, On ; Sets a Timer that listens for primary monitor resolution changes
Gosub, MouseHideOn ; Sets a Timer that hides the cursor if it hovers over Media Center's window
; All timers use the default 250ms resolution -- feel free to speed them up if desired
; Cleans up whenever Media Center is closed
Process, WaitClose, %WMCPID%
WinClose, ahk_pid %SndVolPID%
SetTimer, CheckForResChange, Off
Gosub, MouseHideOff
Goto WaitForWMC
return
; Adjusts window when primary monitor resolution changes, e.g. when launching a full-screen game that uses a different resolution than the desktop
CheckForResChange:
if (A_ScreenWidth <> ScrW or A_ScreenHeight <> ScrH)
{
SysGet, monitorcount, MonitorCount
if (monitorcount = 1 or cycle <> 5) ; Only interested when using virtual full-screen on second monitor
return
sleep, 2500 ; sometimes resolution changes take a while...
SysGet, Mon2, Monitor, 2
Mon2W := Mon2Right - Mon2Left
Mon2H := Mon2Bottom - Mon2Top
WinGetPos, WMCx, WMCy,,, ahk_pid %WMCPID%
if (WMCx <> Mon2Left or WMCy <> Mon2Top)
WinMove, ahk_pid %WMCPID%,, Mon2Left, Mon2Top, Mon2W, Mon2H
ScrW := A_ScreenWidth
ScrH := A_ScreenHeight
}
return
#IfWinActive, ahk_class eHome Render Window ; This is the classname of the Windows Media Center application
+3::
; "Restores" the window if it is in true full-screen mode
WinGet, winstyle, Style, ahk_pid %WMCPID%
if (winstyle = 0x16000000) or (winstyle & 0x20000000)
{
PostMessage, 0x112, 0xF120,,, ahk_pid %WMCPID%
sleep, 1500
}
SysGet, monitorcount, MonitorCount
SysGet, Wk1, MonitorWorkArea, 1 ; used to avoid placing the window on top of the taskbar
If monitorcount = 2 ; provides 4 different windowing modes, the last places it on the second monitor in virtual full-screen
{
SysGet, Mon2, Monitor, 2
Mon2W := Mon2Right - Mon2Left
Mon2H := Mon2Bottom - Mon2Top
if cycle in 1,5 ; virtual full-screen on primary monitor
{
WinSet, Style, -0xC40000, ahk_pid %WMCPID%
WinMove, ahk_pid %WMCPID%, , 0, 0, A_ScreenWidth, A_ScreenHeight
WinActivate, ahk_pid %WMCPID%
cycle = 2
return
}
if cycle = 2 ; small window with borders, bottom-right of primary monitor
{
WinSet, Style, +0xC40000, ahk_pid %WMCPID%
WinMove, ahk_pid %WMCPID%, , A_ScreenWidth - (A_ScreenWidth - Wk1Right) - 831, A_ScreenHeight - (A_ScreenHeight - Wk1Bottom) - 495, 831, 495
cycle = 3
return
}
if cycle = 3 ; small window without borders, bottom-right of primary monitor
{
WinSet, Style, -0xC40000, ahk_pid %WMCPID%
WinMove, ahk_pid %WMCPID%, , A_ScreenWidth - (A_ScreenWidth - Wk1Right) - 831, A_ScreenHeight - (A_ScreenHeight - Wk1Bottom) - 467, 831, 467
cycle = 4
return
}
if cycle = 4 ; virtual full-screen on secondary monitor
{
WinSet, Style, -0xC40000, ahk_pid %WMCPID%
WinMove, ahk_pid %WMCPID%, , Mon2Left, Mon2Top, Mon2W, Mon2H
cycle = 5
return
}
}
Else ; only provides modes for the primary monitor
{
if cycle in 1,4,5
{
WinSet, Style, -0xC40000, ahk_pid %WMCPID%
WinMove, ahk_pid %WMCPID%, , 0, 0, A_ScreenWidth, A_ScreenHeight
WinActivate, ahk_pid %WMCPID%
cycle = 2
return
}
if cycle = 2
{
WinSet, Style, +0xC40000, ahk_pid %WMCPID%
WinMove, ahk_pid %WMCPID%, , A_ScreenWidth - (A_ScreenWidth - Wk1Right) - 831, A_ScreenHeight - (A_ScreenHeight - Wk1Bottom) - 495, 831, 495
cycle = 3
return
}
if cycle = 3
{
WinSet, Style, -0xC40000, ahk_pid %WMCPID%
WinMove, ahk_pid %WMCPID%, , A_ScreenWidth - (A_ScreenWidth - Wk1Right) - 831, A_ScreenHeight - (A_ScreenHeight - Wk1Bottom) - 467, 831, 467
cycle = 4
return
}
}
return
; Mutes Windows Media Center independently using Vista's Volume Mixer
#IfWinExist, ahk_class eHome Render Window
sc120:: ; scancode for the Mute key
Process, Exist, SndVol.exe
SndVolPID = %ErrorLevel%
If SndVolPID = 0
{
Run, SndVol.exe, , Min, SndVolPID
Sleep, 120
}
ControlSend, Mute for Windows Media Center, {Space}, ahk_pid %SndVolPID% ; Toggles the mute control for Media Center
return
/*
The following subroutines hide the cursor (placing it in the bottom left corner of the primary monitor)
when it idles over the Media Center window for ~5 seconds, and then returns it to the previous coordinates when the
user begins to move the mouse again.
*/
MouseHideOn:
SetTimer, WaitForMouseIdle, On
return
MouseHideOff:
SetTimer, WaitForMouseIdle, Off
SetTimer, WaitForMouseMove, Off
DllCall("SetCursorPos", int, PosX1, int, PosY1) ; A DllCall is more reliable than MouseMove in multi-monitor setups
return
WaitForMouseIdle:
if A_TimeIdle > 5000
{
MouseGetPos, PosX1, PosY1
WinGetPos, WMCx, WMCy, WMCw, WMCh, ahk_pid %WMCPID%
if ((PosX1 > WMCx and PosX1 < WMCx + WMCw) and (PosY1 > WMCy and PosY1 < WMCy + WMCh)) ; Checks if cursor is positioned over the window
{
DllCall("SetCursorPos", int, 0, int, A_ScreenHeight) ; Places cursor in bottom-left corner of primary monitor
MouseGetPos, PosX2, PosY2
SetTimer, WaitForMouseIdle, Off
SetTimer, WaitForMouseMove, On
}
}
return
WaitForMouseMove:
MouseGetPos, PosX3, PosY3
if (PosX3 <> PosX2 or PosY3 <> PosY2)
{
DllCall("SetCursorPos", int, PosX1, int, PosY1) ; Returns cursor to its previous position
SetTimer, WaitForMouseMove, Off
SetTimer, WaitForMouseIdle, On
}
return