473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to: Maximize window from the Main function?

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 15 '05 #1
9 1621
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 15 '05 #2
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 15 '05 #3
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 15 '05 #4
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 15 '05 #5
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 15 '05 #6
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 15 '05 #7
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 15 '05 #8
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 15 '05 #9
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 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: MJB | last post by:
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...
1
by: Sanjay Seshasainam | last post by:
How can I maximize a process window I am using the Process class in System.Diagnostics. I can identify the specific process I want using the window title after that I want to maximize the...
0
by: Laura Zeafla via .NET 247 | last post by:
I used the .NET forms application wizard. From the form youclick a button and it calls a C++ function through its dll. This function is supposed to pop-up a command prompt window. Something about...
8
by: jrefactors | last post by:
I want to maximize the browser window when I open a new window. Now I do the following, but different monitor size will yield different width and height values. ...
7
by: Colleyville Alan | last post by:
I have an app that uses Access to grab various PowerPoint slides using the followhyperlink command. I have set the PPT window to run in a minimized state: FollowHyperlink link Set oPres =...
1
by: Brandon | last post by:
Hello there. I'm currently working on a moderately complex Visual C# windows application that I have run into a bit of a problem on. To start things off, the application has normally been run...
2
by: jj | last post by:
I have a single threaded application in c# that scans through a file system. While doing this if I try to minimize, maximize buttons on the top,right hand corner of my form do not do as they are...
5
by: Mrozu | last post by:
Hi When I maximize a form in VB.Net 2003 the bottom of the form gets hidden by the start bar (so my status bar is invisible). How can I get my app to maximize to the usable screen area above...
2
by: mac | last post by:
Hi! Is there anyone knows on how to maximze the Web form during run time? please help me to the code of what im going to do with this. Thanks and Best Regards
8
by: Boki | last post by:
Hi All, I want to force a window ( form, handle ) become foreground, I use: FindWindow and SetForegroundWindow
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.