By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,890 Members | 1,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,890 IT Pros & Developers. It's quick & easy.

How to: Maximize window from the Main function?

P: n/a
MJB
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
Nov 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
try this:
this.WindowState = FormWindowState.Maximiz
but you cant do it from Main - use the constructor or or the OnLoad event or
whatever
"MJB" <mb*@email.com> wrote in message
news:OR**************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #2

P: n/a
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" <mb*@email.com> wrote in message
news:OR**************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #3

P: n/a
Hello,

"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> schrieb:
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:


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
Nov 20 '05 #4

P: n/a
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>
Nov 20 '05 #5

P: n/a
Hello,

"Fergus Cooney" <fi******@tesco.net> schrieb:
aoProcList = GetProcessesByName (sProcessName) 'At least 1.


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
Nov 20 '05 #6

P: n/a
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
Nov 20 '05 #7

P: n/a
Hello,

"Fergus Cooney" <fi******@tesco.net> schrieb:
Mutex looks good. Simple too. :-)

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


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
Nov 20 '05 #8

P: n/a
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]" <hi*******@m.activevb.de> wrote in message
news:ul**************@TK2MSFTNGP12.phx.gbl...
Hello,

"Fergus Cooney" <fi******@tesco.net> schrieb:
aoProcList = GetProcessesByName (sProcessName) 'At least 1.
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

Nov 20 '05 #9

P: n/a
Hello,

"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> schrieb:
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.


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
Nov 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.