Bring Another Application To Front?

O

Octavius Khan

I have an application that runs in the system tray and I use global hotkeys
to perform functions. When certain hotkeys are pressed, my system tray
application may display a message box and sometimes a balloontip, but then
focus is taken away from the application the user is using when this
happens. How can I return focus to the original application?

For example, let's say the user is using Notepad and then presses Ctrl-F9.
This causes the system tray application to paste a block of text into
Notepad and then display a balloontip saying that it did this. Now, focus
is taken away from Notepad and the titlebar is grey.

Thanks for your assistance.
 
S

ShaneO

Octavius said:
I have an application that runs in the system tray and I use global hotkeys
to perform functions. When certain hotkeys are pressed, my system tray
application may display a message box and sometimes a balloontip, but then
focus is taken away from the application the user is using when this
happens. How can I return focus to the original application?

For example, let's say the user is using Notepad and then presses Ctrl-F9.
This causes the system tray application to paste a block of text into
Notepad and then display a balloontip saying that it did this. Now, focus
is taken away from Notepad and the titlebar is grey.

Thanks for your assistance.

There are a number of issues that need to be addressed to achieve your
required result.

One that I can't answer at the moment (without further research) is how
to determine which application "had" the focus prior to activating your
application, however, you've probably already got an answer to that as
your second paragraph mentions that you're able to paste a block of
text. (You must know something about the original app, unless you're
simply using the Clipboard??)

Anyway, to switch back to the original app you first need to obtain its
"MainWindowHandle" (ProcessHandle).

<AirCode - Not Completely Tested>

Public Declare Auto Function ShowWindow Lib "user32.dll" (ByVal hwnd As
IntPtr, ByVal nCmdShow As Int32) As Long
Const SW_SHOWNORMAL As Int32 = 1
Const SW_SHOW As Int32 = 5


Private Function fnGetProcessHandle(ByVal sAppTitle as String) As Long
Dim Proc as Process
For Each Proc in Process.GetProcesses
If Proc.MainWindowTitle.ToUpper.Contains(sAppTitle) then
Return Proc.MainWindowHandle
End If
Next
End Function

ShowWindow(fnGetProcessHandle("NOTEPAD", SW_SHOWNORMAL)


This code will certainly switch you back to your designated app, you
just need to be able to identify what the original application was first.

I hope this helps.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
O

Octavius Khan

Thanks, Shane.

This was very helpful. You have certainly pointed me in the right
direction.

- Octavius
 
O

Octavius Khan

Thanks.

This was very helpful!

- Octavius

ShaneO said:
There are a number of issues that need to be addressed to achieve your
required result.

One that I can't answer at the moment (without further research) is how to
determine which application "had" the focus prior to activating your
application, however, you've probably already got an answer to that as
your second paragraph mentions that you're able to paste a block of text.
(You must know something about the original app, unless you're simply
using the Clipboard??)

Anyway, to switch back to the original app you first need to obtain its
"MainWindowHandle" (ProcessHandle).

<AirCode - Not Completely Tested>

Public Declare Auto Function ShowWindow Lib "user32.dll" (ByVal hwnd As
IntPtr, ByVal nCmdShow As Int32) As Long
Const SW_SHOWNORMAL As Int32 = 1
Const SW_SHOW As Int32 = 5


Private Function fnGetProcessHandle(ByVal sAppTitle as String) As Long
Dim Proc as Process
For Each Proc in Process.GetProcesses
If Proc.MainWindowTitle.ToUpper.Contains(sAppTitle) then
Return Proc.MainWindowHandle
End If
Next
End Function

ShowWindow(fnGetProcessHandle("NOTEPAD", SW_SHOWNORMAL)


This code will certainly switch you back to your designated app, you just
need to be able to identify what the original application was first.

I hope this helps.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
A

Andrew Morton

ShaneO said:
There are a number of issues that need to be addressed to achieve your
required result.

One that I can't answer at the moment (without further research) is
how to determine which application "had" the focus prior to
activating your application, however, you've probably already got an
answer to that as your second paragraph mentions that you're able to
paste a block of text. (You must know something about the original
app, unless you're simply using the Clipboard??)

Wouldn't the previous app. be next down in the z-order of windows, so it
could be found with the GetWindow function in user32.dll?
http://msdn2.microsoft.com/en-us/library/ms633515.aspx

Andrew
 

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