how to activate an application not a form

H

Hari

Hi,

How do i activate my application? I used GetForegroundWindow(),
CreateProcess() , but nothing works fine. I'm only able to activate
the current form. But not the application as a whole.

To give in detail, my app contains multiple forms, like form1
creates form2, form2 inturn creates form3. Suppose now form3 is the
current active form and my app goes background as i click on 'today'
item from 'Start'. Now if CreateProcess() or GetForegroundWindow() is
called, it displays form3. Its fine until that. But as i close
form3(), this should show up form2. But it does'nt. It just shows
today screen, no form.

What i want exactly is when i activate a form in my app from
settings->memory, it works just as fine as the application was never
put in background. I want to acheive the same.
PLZ suggest.


Thanks,

Hari.
 
G

Guest

Then you need to do what the CPL does, which is likely sending a WM_ACTIVATE
message to the Window. Spy++ would tell you for sure.
 
G

Guest

I ran into the same problem, and while I didn't come up with a good solution,
this is what I found.

When your form is reactivated, its OnActivated method is called; however, at
this point, it is already the foreground window, so calling
GetForegroundWindow won't tell you anything.

When your form closes (i.e., upon return from ShowDialog) your next form is
NOT the foreground window (the 'today' screen is, and it gets displayed). I
don't recall if Show/BringToTop etc. could reactivate the next form, but I
ended up using SetForegroundWindow.

The next problem was that as each of my forms closed, the 'today' screen
showed up for a moment.

To fix this, I added code to have each of my forms keep track of its
"parent" form, and added the following code after each call to ShowDialog:

if (Win32.GetForegroundWindow() != parent.Handle)
{
IParentForm topForm = parent;
while (topForm.ParentForm != null)
topForm = topForm.ParentForm;
Win32.SetForegroundWindow((IntPtr)(((uint)topForm.Handle) | 0x00000001));
}

This makes my "bottom most" form the foreground window and tells it to
reactivate its child forms. It seems to work, as I don't see the 'today'
screen again.

I suspect this approach would cause lots of window-flashing if the system
tries to close the form (and application) in low-memory situations.

If I could figure out how to detect this situation in OnActivated, I'd put
the code there. I think that would solve the problem.
 
H

Hari

Thanks Chris and David

Hi Chris,
Then you need to do what the CPL does, which is likely sending a WM_ACTIVATE
message to the Window. Spy++ would tell you for sure.

I did'nt follow even a bit of your suggestion. May be i should be
aware of something which i'm not. What is CPL? Can you please expand
on this??



Hi David,

Thanks a lot for the suggestion
When your form closes (i.e., upon return from ShowDialog) your next form is
NOT the foreground window (the 'today' screen is, and it gets displayed).

I did'nt use showDialog(), because the child form has to be
minimized or hidden, not closed.
To be more clear,

form1, form2 - minimize Box - False
form3 - minimize Box - true

form1 creates and shows( calls show()) on form2 instance. Form2
creates and shows( calls show()) on Form3 instance.
So when i hit the "Cross" at the top right corner of form3, it
minimizes and its previous form, i.e. form2 is shown.


To fix this, I added code to have each of my forms keep track of its
"parent" form, and added the following code after each call to ShowDialog:
if (Win32.GetForegroundWindow() != parent.Handle)
{
IParentForm topForm = parent;
while (topForm.ParentForm != null)
topForm = topForm.ParentForm;
Win32.SetForegroundWindow((IntPtr)(((uint)topForm.Handle) | 0x00000001));

}

So i don't think i can use this code for my case. Can i? any
suggestions??




Thanks,
Hari
 
G

Guest

CPL = control panel applet. THat's what the "running processes" applet is.

Spy++ is a remote tool.
 
H

Hari

Hi Chris,

Thanks a lot! I'm complete illiterate of 'Control panel applet' and
'Spy++'. Can you please give me links to sample app that uses these
tools that could solve my prob.

- Hari
 
P

Paul G. Tobey [eMVP]

There isn't going to be a "sample app" that shows how to combine the use of
a Control Panel applet and a remote spy tool for anything. Run Remote Spy++
and read the help for information on how it works. Run a few applications
on your device, open the Control Panel and choose the Memory applet. The
Running Applications tab will allow you to activate one of the running
applications. If you point Remote Spy++ at that application's main window,
start capturing the messages, and tell the Memory applet to activate that
application, that will tell you what messages get sent.

Paul T.
 
G

Guest

Hari said:
So i don't think i can use this code for my case. Can i? any
suggestions??

Try attaching a Closed delegate to the form you're about to show...
something like this:

form2.Closed += new EventHandler(OnForm2Closed);

Then put the foreground window code in the delegate. I'm not positive it
will work, but you can give it a try.

David
 

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

Top