Show a Hidden Form?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an application. When a user minimizes it it hides itselfs but stays activate in the system tray. Since it is hidden the user may think that they have exited the application and now may attempt to create a new instance by launching the program again. The problem that I am running into is being able to restore the previous application. I am able to end the multiple instance application but since the previous application is hidden I cannot find a way to 'show' previous application
Any suggestions? I need an answer fast.
 
Maybe I am missing the point, however, in Properties you have the option
"showIn Taskbar". The user will be able to maximize the screen from there.
Second, you are worried that the form is still active, so why not close one
form when the other is opened. Then when the user needs it again he'll just
open it again.
Patrick.

*************************
 
The form in the first instance of the application is hidden, and runs in the background. I do not want two instances running, so when a user tries to open a second instance, I want to bring up the hidden form from the first instance, and close the second. I just have no idea how to show that form from the code in the second instance (the application runs the mulitple instance code on startup). The other option that I have is to close the first instance, and bring up the second, but I don't know how to do that either. I am currently researching how to kill a process, hoping that will do the trick.
 
Hi Susan

There are two separated issues in your post, how to detect that a previous
instance is running and how to send a message to another windows to make it
the foremost window.

1- Detect a previous instance:
You can use several techniques, basically ALL of them involve the creation
of a unique object, you can use a Mutex for example, or search by process
name. if you do a search in the archives of this NG you will find several
suggestions as this is an issue that is constantly asked.

2- All you need is the handle of the main windows of the other isntance, you
get it using Process.MainWindowHandle

Here is some code I have used

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



//yyou need this namespace
using System.Runtime.InteropServices;
// P/invoke declarations
public class Form1 : System.Windows.Forms.Form
{
//I search by process name
static public Process FindParallelProcess()
{

string currentprocname = Process.GetCurrentProcess().ProcessName;
foreach( Process process in Process.GetProcessesByName(
currentprocname) )
{
if ( process.Id != Process.GetCurrentProcess().Id )
return process;
}
return null;

}
[DllImport("user32.dll",EntryPoint="SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

//Detection
static void Main()
{

Process previous = Form1.FindParallelProcess();
if ( previous == null )
Application.Run(new Form1());
else
{
//Give the focus to the other instance
SetForegroundWindow( previous.MainWindowHandle);
}
}

Susan said:
I have an application. When a user minimizes it it hides itselfs but
stays activate in the system tray. Since it is hidden the user may think
that they have exited the application and now may attempt to create a new
instance by launching the program again. The problem that I am running into
is being able to restore the previous application. I am able to end the
multiple instance application but since the previous application is hidden I
cannot find a way to 'show' previous application.
 
Thanks. But the SetForegroundWindow will not work, because the form is hidden. I can make it work all day long, if the UI is visible, but after it is hidden, SetForegroundWindow doesn't work. That is why I need to know first how to make that form visible again, before I call the SetForegroundWindow. I am so confused with this right now. Do you know anything about Killing a process? If I can't make the first instance visible, I can close it, and let the second instance come up. All the examples I find declare variables as process, and that will not compile for me. I am beginning to think that I am just an idiot, lol.
 
Hi Susan,


How are you hidding the form?

Now that I think about it I have never use SetForegroundWindow from another
application to show a form I had call hidden on.

Now , I do know that if you call Show from the same application it does
show again.

IF that is the case then what you can do is send a message to the hidden
application, the target application get the message and call the Show method
as needed.


Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Susan said:
Thanks. But the SetForegroundWindow will not work, because the form is
hidden. I can make it work all day long, if the UI is visible, but after it
is hidden, SetForegroundWindow doesn't work. That is why I need to know
first how to make that form visible again, before I call the
SetForegroundWindow. I am so confused with this right now. Do you know
anything about Killing a process? If I can't make the first instance
visible, I can close it, and let the second instance come up. All the
examples I find declare variables as process, and that will not compile for
me. I am beginning to think that I am just an idiot, lol.
 
Back
Top