NotifyIcon and SetForegroundWindow

  • Thread starter Thread starter John
  • Start date Start date
J

John

I use NotifyIcon to minimize my app to the system tray. I also check
everytime my app runs for the previous instance, if it is running, call
SetForegroundWindow to bring its window up front and exit the second
instance.

The problem is it doesn't bring the app out of the system tray. It works
fine if the app is NOT in the system tray. How do I bring it out of the
system tray and make it an active window? Thanks
 
In my C# apps, I do it like this:


this.ShowInTaskBar = true;
this.WindowState = FormWindowState.Normal;
this.Activate();

If you're application is likely to be either normal or maximized, then you
have to add a class field for tracking state:

FormWindowState lastState;

and then, at or near the end of your resize event handler:

lastState = this.WindowState;

with that done, you can set the WindowState to what it was last when you
restore it, rather than just to Normal as in my first lines above.

Hope that helps,

Dale
 
* "John said:
I use NotifyIcon to minimize my app to the system tray. I also check
everytime my app runs for the previous instance, if it is running, call
SetForegroundWindow to bring its window up front and exit the second
instance.

The problem is it doesn't bring the app out of the system tray. It works
fine if the app is NOT in the system tray. How do I bring it out of the
system tray and make it an active window? Thanks

Post your declaration of 'SetForegroundWindow'. How do you get the
handle of the other instance?
 
This is what I have (I use some of the code from this use group and/or other
use groups)
<System.Runtime.InteropServices.DllImport("user32.dll", _

EntryPoint:="SetForegroundWindow", _

CallingConvention:=Runtime.InteropServices.CallingConvention.StdCall, _

CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _

Public Shared Function _

SetForegroundWindow(ByVal handle As IntPtr) As Boolean

'

End Function

Public Shared Sub Main()

Dim pInstance As Process =
Win32Helper.GetRunningInstance(Process.GetCurrentProcess().ProcessName)

If Not pInstance Is Nothing Then

Dim handle As IntPtr = pInstance.MainWindowHandle

If Not IntPtr.Zero.Equals(handle) Then

Win32Helper.ShowWindow(handle, 1)

Win32Helper.SetForegroundWindow(handle)

End If

Application.ExitThread()

Else

Dim context As ApplicationContext = New ApplicationContext(New
frmMain)

Application.Run(context)

End If

End Sub

Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Resize

If Me.WindowState = FormWindowState.Minimized Then

Me.mTray.Visible = True

Me.Hide()

End If

End Sub



Thanks for your help.
 
Back
Top