Solved

Setting Focus to another Application

Posted on 2000-02-24
11
343 Views
Last Modified: 2013-11-20
Using VC++ and NT, how can I set input
focus to another running application,
like AppActivcate in VB.  Looking for a
method to switch between applicatons using software.  One application is a commercial package that we are not able to modify.  
0
Comment
Question by:TBursch
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 2555740
What about simply using

HWND hWnd = FindWindow ( NULL, "<Window's title>");

SetForegorundWindow ( hWnd);

(error chacking omitted for brevity)

?
0
 

Author Comment

by:TBursch
ID: 2556136
jkr,
Thanks for your quick respones.
 
FindWinow() looked promising, but ...
both applications change the Window Title, the string that shows up on the title bar, by appending the working file name to the application name.  So, the Window Title changes each time the applications are started.

I was hopping to find a way to use the application name, that seems to stay constant.

Thanks again,
tom
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2556230
Are you the author of both applications?

Are the separate instances of the same paplication or two completely different applications?

If you have source code for both, then you could get them both to write their main window handle to a shared file/memory.  Then they could talk to each other and activate one another etc.

If two instances of the same app, this would be a little easier.  But not necessary.

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:TBursch
ID: 2556344
The applications are completely different.   We are the authors of one of them.   The other is a third party product that we only have the executable for.

Somthing seems to be jamming my responses back to ExpertsExchange, sorry for the delay.

Thanks for the thoughts,
tom
0
 
LVL 4

Expert Comment

by:inpras
ID: 2557107
Hi
****In the lines of jkr ****
in that case U can use
HWND hWnd = FindWindow ("WindowClassName", NULL);

Hope this helps
0
 

Expert Comment

by:coreyfrost
ID: 2557156
If the application's main window doesn't have a unique class name try the following. This will only work on NT, Win95/98 requires toolhelp functions which NT doesn't provide. There are a couple of examples of how to provide this for both platforms on the MSDN. I don't have access to NT so I am just providing a rough outline.

1) Enumerate all top level windows with the function EnumWindows.

2) For each window, call the function GetWindowThreadProcessID to get its process ID.

3) Open the process with a call to the function OpenProcess as follows:
 HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, nProcessID );
(where nProcessID is the value obtained via the call to GetWindowThreadProcessID)

4) Obtain a list of all modules associated with this process with a call to the function EnumProcessModules. The 0 element in the array of module handles filled by this function will be the EXE.

5) Make a call to GetModuleFileNameEx to obtain the name of the application. This function is preferred over GetModuleFileName as it can get this information about a separate process.

6) If the file name is the application you want to set the focus to, make a call to SetForegroundWindow with its handle.


0
 

Expert Comment

by:johnxi
ID: 2557163
u can also use "EnumWindows" to get the handle that u want, then use
"SetForegorundWindow"
hope this help
0
 

Author Comment

by:TBursch
ID: 2566319
EnumWindows()looks promising, and am trying to write some code to work out the details, but am having trouble locating the PSAPI.DLL and the required header file.  I am assuming it is named psapi.h.    Should I be able to find the DLL in the Development Platform release disks?
tom
0
 

Accepted Solution

by:
coreyfrost earned 100 total points
ID: 2567830
My apologies! I didn't notice in my sources PSAPI.DLL isn't included with the system. Here is an excerpt from the MSDN article "HOWTO: Enumerate Applications in Win32" which tells where to go to get this files.

The Windows NT approach to creating a list of processes uses functions from the PSAPI.DLL. The PSAPI.DLL file is distributed with the Platform SDK, which is available at http://www.microsoft.com/msdn/sdk (path: \MSSDK\bin\winnt\psapi.dll). The PSAPI.H and PSAPI.LIB files that you need are also included with the Platform SDK (paths: \Mssdk\samples\sdktools\image\winnt\pfmon\psapi.h and \Mssdk\samples\sdktools\image\winnt\pfmon\psapi.lib).

To use the functions in the PSAPI.DLL, add the PSAPI.LIB file to your project, and include the PSAPI.H file with any modules that call the PSAPI.DLL functions. Remember to distribute the PSAPI.DLL file with any executable that uses it, as it is not currently distributed with the operating system.


0
 

Author Comment

by:TBursch
ID: 2576950
coreyfrost,
Well it was a little struggle, but I found the DLL and .h, and with the help of the article got some code working.  As it turns out what I needed was to get the process.   My code works most of the time ... there is still some uncertanty in the function that I haven't worked out yet, but I am almost there.   Would like to have the programs come up full screen ... are coming up minimized now.  I'm sure there must be a function to do that.
Thanks much for your help, corey frost.
tom
 
0
 

Expert Comment

by:coreyfrost
ID: 2582136
For a windowed application with a resizable frame you can make a call to the Win32 API function ShowWindow. Since you have the handle to main window that you want to set focus to, you could make this call before you change the focus:
::ShowWindow( hWndOfOtherApplication, SW_MAXIMIZE );

If you want your MFC application to initially appear maximized, you'll need to make some changes to your implementation of CWinApp::InitInstance(). The AppWizard will most likely have added these lines near the end of the function:

pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();

Just replace m_nCmdShow to SW_MAXIMIZED. The value of m_nCmdShow is only referenced once during the life of the application so don't bother changing it.

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
function returning CString in a regular win32 DLL 8 190
Expand data scrubbing tool 13 35
Doc'in system (example?) BA 7 115
Problem to App source 6 75
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question