Maximize Window if program is already running

PROKA

Junior Contributor
Joined
Sep 3, 2003
Messages
249
Location
Bucharest
Let's suppose I have an instance of my program already running. I can detect that by using :

Visual Basic:
If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
            Return True


If it's true I want it to do 2 things . One is to close my current app ( Application.Exit() ) and the other ... is to Maximize the window of the instance that is already running. How do I achieve that ?
 
Hi!

I believe I have almost your answer... but I'm having some troubles convencing the API to work... :confused:

Heres the code:
Visual Basic:
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
    Public Sub main()
        Const SW_MAXIMIZE = 3
        Const SW_RESTORE = 9
        Const SHOWNORMAL = 1

        If Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName).Length > 1 Then
            'MessageBox.Show("The application is already running")

            Dim hwnd As Long = Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)(0).MainWindowHandle.ToInt32
            MessageBox.Show(hwnd)

            ShowWindow(hwnd, SHOWNORMAL)
        Else
            Dim f1 As New Form1
            f1.Text = f1.Handle.ToInt32

            Application.Run(f1)
        End If

The problems here are:
1- The ShowWindow API doesn't seem to work... but the window handle I is correct, as you can compare the messagebox with the windows title text...
2- If the program is minimized, aperantly the hwnd = 0. :confused:

Alex :p
 
I think I got it!...

Visual Basic:
Imports System.Windows.Forms

Module StartUp
    Declare Function ShowWindow Lib "user32" (ByVal hWnd As IntPtr, ByVal nCmdShow As Long) As Long

    Public Sub main()
        Const SW_MAXIMIZE = 3
        Const SW_RESTORE = 9
        Const SHOWNORMAL = 1

        If Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName).Length > 1 Then
            Dim hwnd As IntPtr = _
                Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)(0).MainWindowHandle
            Call ShowWindow(hwnd, SHOWNORMAL)
        Else
            Dim f1 As New Form1
            f1.Text = f1.Handle.ToInt32

            Application.Run(f1)
        End If

    End Sub

End Module
 
Ok dude... Last update...

Visual Basic:
Imports System.Windows.Forms

Module StartUp
    Declare Function ShowWindow Lib "user32" (ByVal hWnd As IntPtr, ByVal nCmdShow As Long) As Long

    Public Sub main()
        Const SW_MAXIMIZE = 3
        Const SW_RESTORE = 9
        Const SHOWNORMAL = 1
        Const SW_SHOW = 5

        Dim processes() As Diagnostics.Process = Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)
        If processes.Length > 1 Then

            Dim mProcess As Diagnostics.Process
            For Each p As Diagnostics.Process In processes
                If mProcess Is Nothing Then
                    mProcess = p
                Else
                    If p.StartTime < mProcess.StartTime Then mProcess = p
                End If
            Next

            Dim hwnd As IntPtr = mProcess.MainWindowHandle

            Call ShowWindow(hwnd, SW_SHOW)
        Else
                Application.Run(New Form1)
        End If

    End Sub

End Module


Changes made:
Visual Basic:
            Dim mProcess As Diagnostics.Process
            For Each p As Diagnostics.Process In processes
                If mProcess Is Nothing Then
                    mProcess = p
                Else
                    If p.StartTime < mProcess.StartTime Then mProcess = p
                End If
            Next
The previous code versions I posted didn't allwas work because, in the processes list, the oldest process isn't allways the one on Index = 0 of the collection. this loop serches for the first one...

Visual Basic:
Const SW_SHOW = 5
I was using SW_RESTORE, this one also works, just doesn't activate the form.
SW_SHOW shows the form and activates it...

It's donne...

Alex :p
 
Just one final touch...

You can combine calls:
Visual Basic:
...
            Call ShowWindow(hwnd, SW_RESTORE)
            Call ShowWindow(hwnd, SW_SHOW)
            Call ShowWindow(hwnd, SW_MAXIMIZE)
...
This will:
1- Allways show the Form
2- Activate the Form
3- Maximize the Form

Alex :p
 
PROKA said:
Thank you very much, Alex. Thanks for doing that for me on Christmas Day :) Happy New year dude !

It's my gift for you! :D

By the way... I tested another call combination that really works the way I wanted (show the form in its current size and position):
Visual Basic:
     Const SW_SHOWMINIMIZED = 2

...

     Call ShowWindow(hwnd, SW_SHOWMINIMIZED)
     Call ShowWindow(hwnd, SHOWNORMAL)
     Call ShowWindow(hwnd, SW_SHOW)

The issue is that if the form is just behind another forms, SHOWNORMAL & SW_SHOW don't Activate the it! :confused: It only gets activated if the form is minimized...
So I minimize it before showing! :eek:

If you want a reference to the ShowWindow API see at
Mentalis

Alex :p
 
I was busy for hollidays ( drinkin' 'n stuff and I didn't find the time to check ur code, but I did now and it seems Ok, except it has a little problem ... On the line
Visual Basic:
Call ShowWindow(hwnd, SW_SHOWMINIMIZED)
I get the error "Value of type 'System.IntPtr' cannot be converted to 'Long' " and if I try with 'Convert.toInt64' before, I get "Invalid cast" :(
 
Back
Top