Connecting Tech Pros Worldwide Forums | Help | Site Map

How to: Maximize window from the Main function?

MJB
Guest
 
Posts: n/a
#1: Nov 22 '05
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
Guest
 
Posts: n/a
#2: Nov 22 '05

re: 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.PrimaryScreen.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]
gary hitch
Guest
 
Posts: n/a
#3: Nov 22 '05

re: How to: Maximize window from the Main function?


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


"MJB" <mb2@email.com> wrote in message
news:ORV7CH7eDHA.2184@TK2MSFTNGP10.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]


Ignacio Machin
Guest
 
Posts: n/a
#4: Nov 22 '05

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("user32.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.com> wrote in message
news:ORV7CH7eDHA.2184@TK2MSFTNGP10.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]


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#5: Nov 22 '05

re: How to: Maximize window from the Main function?


Hello,

"Ignacio Machin" <ignacio.machin 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
('AttachThreadInput'):

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

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet


Fergus Cooney
Guest
 
Posts: n/a
#6: Nov 22 '05

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.Diagnostics.Process

Public Class clsApp

'================================================= ==================
Public Shared Function tThereIsAnInstanceOfThisProgramAlreadyRunning _
(Optional tToActivateThePrevInstance As Boolean = False, _
Optional ProgramTitle As String = "?") As Boolean
Dim sProcessName As String
Dim aoProcList() As System.Diagnostics.Process

sProcessName = GetCurrentProcess.ProcessName
aoProcList = GetProcessesByName (sProcessName) 'At least 1.

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

If tToActivateThePrevInstance Then
ActivateMyBetterHalf (ProgramTitle)
End If

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

'================================================= ==================
Public Shared Sub ActivateMyBetterHalf _
(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.Exit
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.tThereIsAnInstanceOfThisProgramAlreadyRunni ng (True,
"Form1") Then
Return
End If

MsgBox ("Point 2")
If clsApp.tThereIsAnInstanceOfThisProgramAlreadyRunni ng Then
clsApp.ActivateMyBetterHalf ("Form1")
Return
End If

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

'================================================= ==================
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

MsgBox ("Point 3")
If clsApp.tThereIsAnInstanceOfThisProgramAlreadyRunni ng Then
'Form.ActiveForm is not yet valid. So give the name.
clsApp.ActivateMyBetterHalf (Me.Text)
Me.Close
Return
End If
End Sub

'================================================= ==================
Private Sub btnTest_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnTest.Click
MsgBox ("Point 4")
If clsApp.tThereIsAnInstanceOfThisProgramAlreadyRunni ng Then
clsApp.ActivateMyBetterHalf (,True)
End If
End Sub
</code>


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#7: Nov 22 '05

re: How to: Maximize window from the Main function?


Hello,

"Fergus Cooney" <filter-1@tesco.net> schrieb:[color=blue]
> aoProcList = GetProcessesByName (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:

http://groups.google.de/groups?selm=...TNGP09.phx.gbl

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet


Fergus Cooney
Guest
 
Posts: n/a
#8: Nov 22 '05

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


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#9: Nov 22 '05

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
http://www.mvps.org/dotnet


Ignacio Machin
Guest
 
Posts: n/a
#10: Nov 22 '05

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 SetForegroundWindow to do this in a pocketPC application and it
works great:
[DllImport("user32dll",EntryPoint="SetForegroundWin dow")]

public static extern bool SetForegroundWindow(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.activevb.de> wrote in message
news:ulbi06DfDHA.1736@TK2MSFTNGP12.phx.gbl...[color=blue]
> Hello,
>
> "Fergus Cooney" <filter-1@tesco.net> schrieb:[color=green]
> > aoProcList = GetProcessesByName (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]


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#11: Nov 22 '05

re: How to: Maximize window from the Main function?


Hello,

"Ignacio Machin" <ignacio.machin 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:

http://www.mvps.org/vb/code/ForceFore.zip

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet


Closed Thread