restore application from systray

  • Thread starter Thread starter shivaranjani.s.adimulam
  • Start date Start date
S

shivaranjani.s.adimulam

Hi,

I have a problem regarding restoring previous instance of an
application minimized in systray.So that when I try to run the
application it should maximize its previous instance instead of
creating a new one.

The part of code is like -

I have one function which returns the instance of the
previous process as
Public Function GetProcess() As Process
'to get the process for the already running application
Dim processes() As Process
Dim procName As String

procName = Process.GetCurrentProcess.ProcessName
processes = Process.GetProcessesByName(procName)
For Each p As Process In processes
If p.Id <> Process.GetCurrentProcess.Id Then
Return p
End If
Next
End Function

In the below function I am trying to display the window but it
doesn't work the main window handle is 0 in this case.

Public Function MultipleInstancesRunning() As Boolean
Dim pInstance As Process
pInstance = GetProcess()
If Not pInstance Is Nothing Then
Dim handle As IntPtr = pInstance.MainWindowHandle
If Not IntPtr.Zero.Equals(handle) Then
ShowWindow(handle, SW_SHOWMAXIMIZED)
SetForegroundWindow(handle)
End If
Return True
Else
Return False
End If
End Function

Is anything wrong with this method? Do anybody knows any other
alternative?
 
Please either attach your project or paste in the full form's code. That way
we (anyone in the newsgroup) can see the code you have so far. It will make
life easier for a solution

Me peronally would start a new application in the System Tray if one doesn't
exist. Then on double-click of the tray, do something like this:

Dim frm As Form1

If Not (frm Is Nothing) Then
frm.Show() 'or frm.ShowDialog()
Else
frm = New Form1
frm.Show() ' or frm.ShowDialog()
End If

(frm.Show() would be better. Although you are starting the program from a
module to run the tray & then the form. In this case you should use
ShowDialog())

An even better method would be to create a boolean value:

In Module (Declarions):

Dim blnDoesFormExist As Boolean = False

Then behind the System Tray Icon double-click event():

If Not blnDoesFormExist = True Then
blnDoesFormExist = True
Dim frm As New Form1
frm.ShowDialog()
Else
Me.Show()
End If

Do something like that & forget the ShowWindow or FindWindow API's. In a
nutshell: Why complicate things?

Either try my approach or paste in the code like I suggested firstly. It
would give everyone an good overall picture

---------------------------
If that's your real Gmail address then I suggest you spoof it to stop
SPAMMERS
---------------------------

I hope this helps

Crouchie1998
BA (HONS) MCP MCSE
 
This code will take a program minimized into the system tray and restore it
to the screen with the focus instead of opening a new window.

Bobbo
__________________________________________
Public Class Form1
Inherits System.Windows.Forms.Form

Declare Auto Function FindWindow Lib "user32.dll" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr

Declare Auto Function ShowWindow Lib "user32.dll" _
(ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Boolean

#Region " Windows Form Designer generated code "
#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Const SW_RESTORE As Integer = 9
Dim SearchText As String = Me.Text
If UBound(System.Diagnostics.Process.GetProcessesByName( _
System.Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
Me.Text = Me.Text & "X"
Dim Handle As IntPtr = FindWindow(Nothing, SearchText)
ShowWindow(Handle, SW_RESTORE)
End
End If
End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top