How To Detect In Code If A Window Is Maximized

Hi,

I have an app where the user can set the windows to either the restored default setting or maximized. That setting is stored in a database and is displayed in a Textbox named txtWindowSize. I am using the code below to detect what the saved setting is.

[code] If txtWindowSize.Text=“Maximum” Then
frmContents.Maximize
End IF

If txtWindowSize.Text=“Default” Then
frmContents.Restore
End If[/code]

The problem is that when the window is already maximized and the code frmContents.Maximize is fired, it actually makes the window smaller returning it to the restored size.

So if the window is already maximized running the code frmContents.Maximize actually makes the window small again.

Is there a way I can detect whether the window is already maximized so I only run the code if the window is in the restored small state?

Any suggestions would be appreciated.

Try this:

Sub DetermineWindowHeight(Extends MyWindow as Window)
 
  If MyWindow.Bounds.Height >= MyWindow.GetScreen.AvailableHeight Then 
    
    Dim r as New REALbasic.Rect
    
    r.Left = MyWindow.Bounds.Left
    r.Height = MyWindow.GetScreen.AvailableHeight
    r.Width = MyWindow.Bounds.Width
    r.top = MyWindow.GetScreen.AvailableTop
    
    MyWindow.Bounds = r
  
End Sub

Window.GetScreen is a method that determines which screen has the window.

So basically check if the bounds of the window are the same size as the AvailableHeight and AvailableWidth of the screen.

Hi Jon,

Thanks so much for taking the time to provide that code. I really appreciate it. I actually did find a simple solution that works fine. You create a string variable and assign what the current screen size is when you leave the window. When your return to the window the screen size will look at the variable and if it is different, it will maximize or minimize the window.

This is the code I used.

[code] If txtWindowSize.Text=“Maximum” Then
If CH=“Default” Then
frmContents.Maximize
End If
End IF

If txtWindowSize.Text=“Default” Then
frmContents.Restore
End If[/code]

The txtWindowSize text reads the current setting from the database and sets CH to the current value. When you return to the window this code will see whether the value stored in the CH variable is either Default or Maximize. If it is Default it will maximize it.

Thanks again.

[quote=208238:@Jon Ogden]Try this:
Window.GetScreen is a method that determines which screen has the window.
[/quote]

Jon,

How do you determine what screen has the window?

Function WhichScreen(W as window) As Integer for i as integer = 0 to ScreenCount-1 if W.Left > Screen(i).Left and W.Left < Screen(i).Left+Screen(i).Width _ and W.Top > Screen(i).Top and W.Top < Screen(i).Top+Screen(i).Height then return i end if next End Function

If a window is across the boundaries of a screen, for instance between 0 and 1, what counts is the upper left corner of the window.

Nice! Thank you.

[quote=208208:@James Redway]Hi,

I have an app where the user can set the windows to either the restored default setting or maximized. That setting is stored in a database and is displayed in a Textbox named txtWindowSize. I am using the code below to detect what the saved setting is.

[code] If txtWindowSize.Text=“Maximum” Then
frmContents.Maximize
End IF

If txtWindowSize.Text=“Default” Then
frmContents.Restore
End If[/code]

The problem is that when the window is already maximized and the code frmContents.Maximize is fired, it actually makes the window smaller returning it to the restored size.

So if the window is already maximized running the code frmContents.Maximize actually makes the window small again.

Is there a way I can detect whether the window is already maximized so I only run the code if the window is in the restored small state?

Any suggestions would be appreciated.[/quote]

Not sure if I understand your request. But are you already following the event: Window.Maximize (http://documentation.xojo.com/index.php/Window.Maximize_event)

if it’s for Windows, I’m using this:

[code]
Declare Function IsZoomed Lib “user32.dll” (ByVal hwnd As integer) As integer
Declare Function IsIconic Lib “user32.dll” (ByVal hwnd As integer) As integer

Dim IsMaximized as boolean = (IsZoomed(self.handle) <> 0)
Dim IsMinimized as boolean = (IsIconic(self.handle) <> 0)[/code]

I just posted this on another thread, but I’ll repeat it here because it seems to be a common question:

IMHO, the easiest and best solution these days is to use MonkeyBreadSoftware’s IsZoomedMBS. It works on both Mac (Cocoa and later) and Windows.

To maximize a window:

wnd.IsZoomedMBS = True

To get the maximized status of a window:

dim IsMax as boolean = wnd.IsZoomedMBS