Determine window status (fullscreen, maximized, minimized)

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
spm_jhn
Posts: 2
Joined: Wed Feb 04, 2009 11:31 am

Determine window status (fullscreen, maximized, minimized)

Post by spm_jhn » Wed Feb 04, 2009 11:41 am

Greetings,

I wonder if there's a way to determine in phyton script whether the current window is maximized, minimized or in fullscreen.

Thanks,
johny

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Determine window status (fullscreen, maximized, minimized)

Post by Pako » Wed Feb 04, 2009 7:22 pm

spm_jhn wrote:I wonder if there's a way to determine in phyton script whether the current window is maximized, minimized or in fullscreen.
You can try this script (You must know the handle (hwnd) of window):

Code: Select all

from win32gui import GetWindowPlacement
#SW_SHOWNORMAL = 1
#SW_SHOWMINIMIZED = 2
#SW_SHOWMAXIMIZED = 3
def GetWindowStatus(hwnd):
    showList  = (None, "Normal", "Minimized", "Maximized")
    placement = GetWindowPlacement(hwnd)
    if placement[2] == (-1, -1):
        return "Fullscreen"
    else: 
        return showList[placement[1]]
I'm not sure, whether the detection of the state "Fullscreen" will always work correctly.
Pako

spm_jhn
Posts: 2
Joined: Wed Feb 04, 2009 11:31 am

Re: Determine window status (fullscreen, maximized, minimized)

Post by spm_jhn » Thu Feb 05, 2009 6:27 am

Well, in case of fullscreen, I still get the status Maximized. No matter, I accually just wanted the maximized status, and it works perfectly. Thanks a lot.

Btw, great software. I'm seriously considering of finishing my igorUsb project. :D

andyss
Posts: 32
Joined: Mon Aug 24, 2009 1:02 pm

Re: Determine window status (fullscreen, maximized, minimized)

Post by andyss » Thu Sep 10, 2009 6:52 am

Is there anyone that can give me a hand in trying to get this script working?

I'm trying to use eg.WindowMatcher to get the window handle (hwnd) to pass to this script to get the window state of Media Center. It seems to get the handle ok (I think) but it doesn't properly get the window state.

Code: Select all

from win32gui import GetWindowPlacement
#SW_SHOWNORMAL = 1
#SW_SHOWMINIMIZED = 2
#SW_SHOWMAXIMIZED = 3
FindWin = eg.WindowMatcher(u'ehshell.exe', None, None, None, None, None, False, 0.0, 0)
hwnd = FindWin()
def GetWindowStatus(hwnd):
    showList  = (None, "Normal", "Minimized", "Maximized")
    placement = GetWindowPlacement(hwnd)
    if placement[2] == (-1, -1):
        return "Fullscreen"
    else: 
        return showList[placement[1]]
I'm trying to display a volume OSD when Media Center is minimzed because Media Center has it's own volume OSD and I don't want my OSD displayed when Media Center is windowed or maximized, (hope that sounds clear).

Can one of the code gurus work this out?

Andy

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Determine window status (fullscreen, maximized, minimized)

Post by Pako » Thu Sep 10, 2009 8:02 am

Code: Select all

from win32gui import GetWindowPlacement
#SW_SHOWNORMAL = 1
#SW_SHOWMINIMIZED = 2
#SW_SHOWMAXIMIZED = 3
FindWin = eg.WindowMatcher(u'ehshell.exe', None, None, None, None, None, True, 0.0, 0)
hwnd = FindWin()
def GetWindowStatus(hwnd):
    showList  = (None, "Normal", "Minimized", "Maximized")
    placement = GetWindowPlacement(hwnd)
    if placement[2] == (-1, -1):
        return "Fullscreen"
    else: 
        return showList[placement[1]]
print GetWindowStatus(hwnd[0])
Pako

andyss
Posts: 32
Joined: Mon Aug 24, 2009 1:02 pm

Re: Determine window status (fullscreen, maximized, minimized)

Post by andyss » Thu Sep 10, 2009 8:56 pm

Okay I finally got this working to some extent with the help of Pako's code, I've also simplified it a bit:

Code: Select all

from win32gui import GetWindowPlacement
#SW_SHOWNORMAL = 1
#SW_SHOWMINIMIZED = 2
#SW_SHOWMAXIMIZED = 3
FindWin = eg.WindowMatcher(u'ehshell.exe', None, None, None, None, None, False, 0.0, 0)
hwnd = FindWin()
def GetWindowStatus(hwnd):
    showList  = (None, "Normal", "Minimized", "Maximized")
    placement = GetWindowPlacement(hwnd)
    return showList[placement[1]]
print 'mceWindow.' + GetWindowStatus(hwnd[0])
The only problem I am having with the script is if Media Center isn't running at all, then I would get the following error:

Code: Select all

Traceback (most recent call last):
Python script "131", line 11, in <module>
print 'mceWindow.' + GetWindowStatus(hwnd[0])
IndexError: list index out of range
...which isn't a big deal because I can just do a find window but was hoping to keep it in the script.

Thanks,

Andy

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Determine window status (fullscreen, maximized, minimized)

Post by Pako » Fri Sep 11, 2009 4:32 am

Code: Select all

if len(hwnd) > 0:
	print 'mceWindow.' + GetWindowStatus(hwnd[0])
else:
	print 'mceWindow.is_not_running'
Pako

andyss
Posts: 32
Joined: Mon Aug 24, 2009 1:02 pm

Re: Determine window status (fullscreen, maximized, minimized)

Post by andyss » Fri Sep 11, 2009 6:59 am

Thanks Pako, worked perfectly!!!

Andy

User avatar
jinxdone
Plugin Developer
Posts: 443
Joined: Tue Jan 02, 2007 4:08 pm

Re: Determine window status (fullscreen, maximized, minimized)

Post by jinxdone » Fri Sep 25, 2009 7:14 am

Here's a window minimize/restore toggle example I posted in another thread. It does basically the same thing detecting window state with the GetWindowLong and comparing it's result to the bitmask (0x20000000 = WS_MINIMIZE, you could check for other bits too).

Code: Select all

from eg.WinApi.Dynamic import GetWindowLong, ShowWindow, SW_RESTORE, SW_MAXIMIZE, SW_MINIMIZE
   
# Check that we have a window to try to minimize/restore
# Use Find Window action before this script!
if len(result) > 0:
    # Check if WS_MINIMIZE bit is set
    if(GetWindowLong(result[0], -16) & 0x20000000):
        eg.plugins.EventGhost.ShowOSD('Minimized ' + eg.WinApi.GetWindowText(eg.result[0]), None, (255, 255, 255), (0, 0, 0), 0, (0, 0), 0, 3.0, None)
        ShowWindow(result[0], SW_RESTORE)
    else:
        eg.plugins.EventGhost.ShowOSD('Maximized ' + eg.WinApi.GetWindowText(eg.result[0]), None, (255, 255, 255), (0, 0, 0), 0, (0, 0), 0, 3.0, None)
        ShowWindow(result[0], SW_MINIMIZE)
-jinxdone

molitar
Experienced User
Posts: 212
Joined: Fri Sep 11, 2009 6:44 am

Re: Determine window status (fullscreen, maximized, minimized)

Post by molitar » Fri Sep 25, 2009 3:00 pm

Yeah jinxdone script works great but I also do a jump to call a windows restore before I change maximize or minimize of the window. This way if I am in full screen before calling it my normal window view does not get messed up. If you have mpc remember window size like I do I don't like it auto resizing than minimizing window when it's full screen will easily mess up the normal window view. I tried just adding it to the code but it would not work maybe a wait statement needed to be put in the code itself to give time to do the restore before doing the minimize of screen so I just did a jump event to call the window restore, but I am curious on if a wait is what I needed to make the script work or something else?

Code: Select all

from eg.WinApi.Dynamic import GetWindowLong, ShowWindow, SW_RESTORE, SW_MAXIMIZE, SW_MINIMIZE
    
# Check that we have a window to try to minimize/restore
# Use Find Window action before this script!
if len(result) > 0:
    # Check if WS_MINIMIZE bit is set
    if(GetWindowLong(result[0], -16) & 0x20000000):
        #eg.plugins.EventGhost.ShowOSD('Minimized ' + eg.WinApi.GetWindowText(eg.result[0]), None, (255, 255, 255), (0, 0, 0), 0, (0, 0), 0, 3.0, None)
        print "Minimized!"
        ShowWindow(result[0], SW_RESTORE)
    else:
        #eg.plugins.EventGhost.ShowOSD('Maximized ' + eg.WinApi.GetWindowText(eg.result[0]), None, (255, 255, 255), (0, 0, 0), 0, (0, 0), 0, 3.0, None)
        print "Not Minimized."
        ShowWindow(result[0], SW_RESTORE)
        ShowWindow(result[0], SW_MINIMIZE)

Post Reply