How to: Maximize window from the Main function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MJB

    #1

    How to: Maximize window from the Main function?

    In the main function of my form I check to see if the application is already
    running. If it is, I throw up a message box telling the user. What I would
    like to do is also maximize the application, but I am unable to do so. Does
    anyone have any experience with this.

    TIA,
    Matt


  • joe

    #2
    How to: Maximize window from the Main function?

    This won't set the window to maximized, but it has
    basically the same affect. I think there is


    Dim ScrnRec As Rectangle
    ScrnRec = Screen.PrimaryS creen.Bounds()

    Me.Top = ScrnRec.Top
    Me.Left = ScrnRec.Left
    Me.Width = ScrnRec.Width
    Me.Height = ScrnRec.Height


    You could also try...

    Me.WindowState= FormWindowState .Maximized
    [color=blue]
    >-----Original Message-----
    >In the main function of my form I check to see if the[/color]
    application is already[color=blue]
    >running. If it is, I throw up a message box telling the[/color]
    user. What I would[color=blue]
    >like to do is also maximize the application, but I am[/color]
    unable to do so. Does[color=blue]
    >anyone have any experience with this.
    >
    >TIA,
    >Matt
    >
    >
    >.
    >[/color]

    Comment

    • gary hitch

      #3
      Re: How to: Maximize window from the Main function?

      try this:
      this.WindowStat e = FormWindowState .Maximiz
      but you cant do it from Main - use the constructor or or the OnLoad event or
      whatever


      "MJB" <mb2@email.co m> wrote in message
      news:ORV7CH7eDH A.2184@TK2MSFTN GP10.phx.gbl...[color=blue]
      > In the main function of my form I check to see if the application is[/color]
      already[color=blue]
      > running. If it is, I throw up a message box telling the user. What I[/color]
      would[color=blue]
      > like to do is also maximize the application, but I am unable to do so.[/color]
      Does[color=blue]
      > anyone have any experience with this.
      >
      > TIA,
      > Matt
      >
      >[/color]


      Comment

      • Ignacio Machin

        #4
        Re: How to: Maximize window from the Main function?

        Hi,

        I assume that you will kill the second instance and activate the first one,
        if so all you have to do is call SetActiveWindow API:

        [DllImport("user 32.dll")]
        IntPtr SetActiveWindow ( IntPtr hWnd);

        This will do the trick.

        You need the hWnd of the first app, if you need code to get it let me know.

        Hope this help,

        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation

        "MJB" <mb2@email.co m> wrote in message
        news:ORV7CH7eDH A.2184@TK2MSFTN GP10.phx.gbl...[color=blue]
        > In the main function of my form I check to see if the application is[/color]
        already[color=blue]
        > running. If it is, I throw up a message box telling the user. What I[/color]
        would[color=blue]
        > like to do is also maximize the application, but I am unable to do so.[/color]
        Does[color=blue]
        > anyone have any experience with this.
        >
        > TIA,
        > Matt
        >
        >[/color]


        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: How to: Maximize window from the Main function?

          Hello,

          "Ignacio Machin" <ignacio.mach in AT dot.state.fl.us > schrieb:[color=blue]
          > I assume that you will kill the second instance and activate the first[/color]
          one,[color=blue]
          > if so all you have to do is call SetActiveWindow API:[/color]

          You will have to attach the window to the thread's message queue
          ('AttachThreadI nput'):

          <msdn>
          The window must be attached to the calling thread's message queue.
          </msdn>

          --
          Herfried K. Wagner
          MVP · VB Classic, VB.NET



          Comment

          • Fergus Cooney

            #6
            Re: How to: Maximize window from the Main function?

            Hi Matt,

            Have my application class. :-)

            You can call it from Sub Main, Sub MainForm_Load or sometime later.

            Regards,
            Fergus

            <code>
            Imports System.Diagnost ics.Process

            Public Class clsApp

            '============== =============== =============== =============== ========
            Public Shared Function tThereIsAnInsta nceOfThisProgra mAlreadyRunning _
            (Optional tToActivateTheP revInstance As Boolean = False, _
            Optional ProgramTitle As String = "?") As Boolean
            Dim sProcessName As String
            Dim aoProcList() As System.Diagnost ics.Process

            sProcessName = GetCurrentProce ss.ProcessName
            aoProcList = GetProcessesByN ame (sProcessName) 'At least 1.

            If aoProcList.Leng th = 1 Then
            'There's just me.
            Return False
            End If

            If tToActivateTheP revInstance Then
            ActivateMyBette rHalf (ProgramTitle)
            End If

            'Another me beat me to it.
            Return True
            End Function

            '============== =============== =============== =============== ========
            Public Shared Sub ActivateMyBette rHalf _
            (Optional ProgramTitle As String = "?", _
            Optional tToCallAppExit As Boolean = False)
            Dim MainForm As Form = Nothing

            If ProgramTitle = "?" Then
            MainForm = Form.ActiveForm 'Assumes that it's the main Form.
            If MainForm Is Nothing Then
            Throw New Exception ("No ProgramTitle and no Main Form??")
            End If
            ProgramTitle = MainForm.Text
            MainForm.Text = "About to die" 'So AppActivate avoids self.
            End If

            MsgBox ("This is " & ProgramTitle & "(2) saying ""Bye, bye"" :-(")
            AppActivate (ProgramTitle)

            If MainForm Is Nothing = False Then
            MainForm.Close
            End If

            If tToCallAppExit Then
            Application.Exi t
            End If

            'Or leave it to the caller to close down further as necessary.
            End Sub

            End Class

            '============== =============== =============== =============== ========
            Public Module MainMod
            Public Sub Main
            MsgBox ("Point 1")
            If clsApp.tThereIs AnInstanceOfThi sProgramAlready Running (True,
            "Form1") Then
            Return
            End If

            MsgBox ("Point 2")
            If clsApp.tThereIs AnInstanceOfThi sProgramAlready Running Then
            clsApp.Activate MyBetterHalf ("Form1")
            Return
            End If

            Dim F As New Form1
            F.Show
            Application.Run ()
            End Sub
            End Module

            '============== =============== =============== =============== ========
            Private Sub Form1_Load(ByVa l sender As System.Object, _
            ByVal e As System.EventArg s) Handles MyBase.Load

            MsgBox ("Point 3")
            If clsApp.tThereIs AnInstanceOfThi sProgramAlready Running Then
            'Form.ActiveFor m is not yet valid. So give the name.
            clsApp.Activate MyBetterHalf (Me.Text)
            Me.Close
            Return
            End If
            End Sub

            '============== =============== =============== =============== ========
            Private Sub btnTest_Click(B yVal sender As System.Object, _
            ByVal e As System.EventArg s) Handles btnTest.Click
            MsgBox ("Point 4")
            If clsApp.tThereIs AnInstanceOfThi sProgramAlready Running Then
            clsApp.Activate MyBetterHalf (,True)
            End If
            End Sub
            </code>


            Comment

            • Herfried K. Wagner [MVP]

              #7
              Re: How to: Maximize window from the Main function?

              Hello,

              "Fergus Cooney" <filter-1@tesco.net> schrieb:[color=blue]
              > aoProcList = GetProcessesByN ame (sProcessName) 'At least 1.[/color]

              Notice that _different_ applications can have the same name. I would use a
              'Mutex' for detecting other instances of the application:



              --
              Herfried K. Wagner
              MVP · VB Classic, VB.NET



              Comment

              • Fergus Cooney

                #8
                Re: How to: Maximize window from the Main function?

                Hi Herfried,

                Mutex looks good. Simple too. :-)

                Now, how do you activate the other instance? Is it possible to associate a
                value with a mutex?

                Regards,
                Fergus


                Comment

                • Herfried K. Wagner [MVP]

                  #9
                  Re: How to: Maximize window from the Main function?

                  Hello,

                  "Fergus Cooney" <filter-1@tesco.net> schrieb:[color=blue]
                  > Mutex looks good. Simple too. :-)
                  >
                  > Now, how do you activate the other instance? Is it possible
                  > to associate a value with a mutex?[/color]

                  I think that's not easy. You can use 'AppActivate' with the other
                  instance's process ID (see documentation for 'AppActivate'). I recently
                  posted a 'PrevInstance' function which returned the 'Process' object for the
                  other instance by looping through the process list and comparing process
                  name and file name.

                  --
                  Herfried K. Wagner
                  MVP · VB Classic, VB.NET



                  Comment

                  • Ignacio Machin

                    #10
                    Re: How to: Maximize window from the Main function?

                    Hi,

                    A mutex is a good idea, now the tricky part is set the focus to that other
                    process , I proposed using SetActiveWindow API call but as you mentioned you
                    need to be attached to the calling thread's message queue.

                    I'm using SetForegroundWi ndow to do this in a pocketPC application and it
                    works great:
                    [DllImport("user 32dll",EntryPoi nt="SetForegrou ndWindow")]

                    public static extern bool SetForegroundWi ndow(IntPtr hWnd);


                    Hope this help,

                    --
                    Ignacio Machin,
                    ignacio.machin AT dot.state.fl.us
                    Florida Department Of Transportation


                    "Herfried K. Wagner [MVP]" <hirf.nosp@m.ac tivevb.de> wrote in message
                    news:ulbi06DfDH A.1736@TK2MSFTN GP12.phx.gbl...[color=blue]
                    > Hello,
                    >
                    > "Fergus Cooney" <filter-1@tesco.net> schrieb:[color=green]
                    > > aoProcList = GetProcessesByN ame (sProcessName) 'At least 1.[/color]
                    >
                    > Notice that _different_ applications can have the same name. I would use[/color]
                    a[color=blue]
                    > 'Mutex' for detecting other instances of the application:
                    >
                    >[/color]
                    http://groups.google.de/groups?selm=...TNGP09.phx.gbl[color=blue]
                    >
                    > --
                    > Herfried K. Wagner
                    > MVP · VB Classic, VB.NET
                    > http://www.mvps.org/dotnet
                    >
                    >[/color]


                    Comment

                    • Herfried K. Wagner [MVP]

                      #11
                      Re: How to: Maximize window from the Main function?

                      Hello,

                      "Ignacio Machin" <ignacio.mach in AT dot.state.fl.us > schrieb:[color=blue]
                      > now the tricky part is set the focus to that other
                      > process , I proposed using SetActiveWindow API call
                      > but as you mentioned you need to be attached to the
                      > calling thread's message queue.[/color]

                      You'll find an implementation in VB6 written by Karl E. Peterson here:



                      --
                      Herfried K. Wagner
                      MVP · VB Classic, VB.NET



                      Comment

                      Working...