Windows API, VB.Net help...catching Maximize

I am trying to catch when a user maximizes a form, set the coordinates to what I want them to be and then continue on.  I just want to be able to set the size of the window myself instead of the system controlling it.

I have the code below to catch the maximize, but I am not sure what to do from this point and have never done any api coding.

Thanks,
rgn
LVL 12
rgn2121Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rgn2121Author Commented:
oops...
    Protected Overrides Sub WndProc(ByRef m As Message)
        Select Case m.Msg

            Case Is = WM_SYSCOMMAND

                Select Case m.WParam.ToInt32

                    Case Is = SC_CLOSE
                        MsgBox("Closing")

                    Case Is = SC_MAXIMIZE
                        MsgBox("Maximize")

                    Case Is = SC_RESTORE
                        MsgBox("Restore")

                    Case Is = SC_MINIMIZE
                        MsgBox("Minimize")

                End Select
        End Select

        MyBase.WndProc(m)

    End Sub

Open in new window

0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Set the MaximizedBounds() Property on your Form:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.maximizedbounds.aspx

Simple example:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim wa As Rectangle = Screen.GetWorkingArea(Me)
        Me.MaximizedBounds = New Rectangle(wa.Location, New Size(wa.Width / 2, wa.Height)) ' left 1/2 of the working area
    End Sub

End Class

Open in new window

0
rgn2121Author Commented:
Thanks for the code, but I can't use this because the apps are running through Citrix.  I posted a question similar to this a while back and you gave me a link that led me to the code I have above, but I don't know what to do from there.
Citrix is treating dual screens like one large monitor, so I want to catch the call to maximize the window, determine the window dimensions myself and then carry on...
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Hmmm...don't think you can CHANGE the location/size when you receive the SC_MAXIMIZE command.

You can also do it this way:
Imports System.Runtime.InteropServices
Public Class Form1

    Private Structure POINTAPI
        Public x As Integer
        Public y As Integer
    End Structure

    Private Structure MINMAXINFO
        Public ptReserved As POINTAPI
        Public ptMaxSize As POINTAPI
        Public ptMaxPosition As POINTAPI
        Public ptMinTrackSize As POINTAPI
        Public ptMaxTrackSize As POINTAPI
    End Structure

    Private Const WM_GETMINMAXINFO As Integer = &H24

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)

        Select Case m.Msg
            Case WM_GETMINMAXINFO
                Dim wa As Rectangle = Screen.GetWorkingArea(Me)
                Dim mmi As MINMAXINFO = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(MINMAXINFO)), MINMAXINFO)

                ' set the size/location of "mmi":
                mmi.ptMaxPosition.x = wa.Location.X
                mmi.ptMaxPosition.y = wa.Location.Y
                mmi.ptMaxSize.x = wa.Size.Width / 2
                mmi.ptMaxSize.y = wa.Size.Height

                Marshal.StructureToPtr(mmi, m.LParam, True)
        End Select
    End Sub

End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgn2121Author Commented:
I will give this a shot and test...Citrix may limit me here so we will see...I will try it local and Citrix and let you know.
 
Thanks
 
0
rgn2121Author Commented:
Running the code above locally on dual screens both set at 1280 x 1024, the form displays somwhere off the right of the right monitor...
The ptMaxTrackSize when I debug is above 2500 for the X coord.
0
rgn2121Author Commented:
Maybe you meant for the code above to only be used if it was in the citrix situation...I will try that.
0
rgn2121Author Commented:
I will work through what you gave and post back when I can...I have some previous code to move around so that the local stuff is not messed up...Thanks again.
0
rgn2121Author Commented:
Okay...this is where I'm at.... It might be that I have no control over the form where citrix is concerned.  At least as far as the maximizing is concerned.  When running in Citrix, it still covers both screens...
 

Imports System.Runtime.InteropServices

Public Class Form1

    Private Const WM_GETMINMAXINFO As Integer = &H24
    Private _firstLoad As Boolean = True
    Private _isCitrix As Boolean = False
    Private _isMultiMonitor As Boolean = False

    Private Structure POINTAPI
        Public x As Integer
        Public y As Integer
    End Structure

    Private Structure MINMAXINFO
        Public ptReserved As POINTAPI
        Public ptMaxSize As POINTAPI
        Public ptMaxPosition As POINTAPI
        Public ptMinTrackSize As POINTAPI
        Public ptMaxTrackSize As POINTAPI
    End Structure

 
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)

        If _firstLoad Then

            'Check for a Citrix Session
            If Environment.GetEnvironmentVariable("SESSIONNAME") <> "Console" AndAlso _
                String.IsNullOrEmpty(Environment.GetEnvironmentVariable("CLIENTNAME")) Then
                _isCitrix = True
            End If

            'Check screen configuration
            Dim s As Screen
            For Each s In Screen.AllScreens
                If _isCitrix Then
                    If Screen.AllScreens.Length = 1 Then
                        'Assume anything greater than 1600 width in Citrix is 2 screens
                        If Screen.PrimaryScreen.Bounds.Width >= 1600 Then
                            _isMultiMonitor = True
                        End If

                    ElseIf Screen.AllScreens.Length > 1 Then
                        MsgBox("Citrix is seeing 2 screens!!")
                    End If
                ElseIf Screen.AllScreens.Length > 1 Then
                    _isMultiMonitor = True
                End If 'END _isCitrix
            Next

        End If 'END _firstLoad

        'Execute only if it is on dual screens and in citrix
        If _isCitrix AndAlso _isMultiMonitor Then

            Select Case m.Msg
                Case WM_GETMINMAXINFO
                    Dim wa As Rectangle = Screen.GetWorkingArea(Me)
                    Dim mmi As MINMAXINFO = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(MINMAXINFO)), MINMAXINFO)

                    ' set the size/location of "mmi":
                    mmi.ptMaxPosition.x = wa.Location.X
                    mmi.ptMaxPosition.y = wa.Location.Y
                    mmi.ptMaxSize.x = CInt(wa.Size.Width / 2)
                    mmi.ptMaxSize.y = wa.Size.Height

                    Marshal.StructureToPtr(mmi, m.LParam, True)
            End Select

        End If

        _firstLoad = False

    End Sub

    Private Sub SetLocalMultiScreenDimensions(ByVal scrn As Screen)

        _firstLoad = False

        If Me.Bounds.Width < scrn.Bounds.Width AndAlso _
            Me.Bounds.Height < scrn.Bounds.Height Then

            'Find the center of the designed form...
            Dim myX As Integer = CInt(Me.Bounds.Width / 2)
            Dim myY As Integer = CInt(Me.Bounds.Height / 2)

            'Find the location that will center the designed form...
            Dim pntX As Integer = CInt((scrn.Bounds.Location.X + (scrn.Bounds.Location.X / 2))) - myX
            Dim pnt As New System.Drawing.Point(pntX, CInt(scrn.Bounds.Height / 2) - myY)

            Me.Location = pnt


        Else
            Me.Bounds = scrn.Bounds
            Me.WindowState = FormWindowState.Normal
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not _isCitrix Then
            If Not _isMultiMonitor Then
                'Just let the form load
            Else
                Dim s As Screen
                For Each s In Screen.AllScreens
                    If Not s.Primary Then
                        SetLocalMultiScreenDimensions(s)
                        Exit For
                    End If 'END Not s.Primary
                Next
            End If
        End If
    End Sub
End Class

Open in new window

0
rgn2121Author Commented:
Any updates or thoughts...??
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
I don't have any experience with Citrix so I'm not sure where to go next...sorry.  =\
0
rgn2121Author Commented:
But you don't see any issues with the code correct...?  As long as someone more experienced than me feels comfortable with the code, I can at least feel more confident in the fact that the server guys would have to make a change if they do anything at all.
Thanks,
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
It looks ok to me...
0
rgn2121Author Commented:
This could not solve my problem because of Citrix.  The code provided did allow me to learn some API code and control window events locally.
Thanks...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.