Bring application window to the top

Posted on 2000-01-21
Medium Priority
Last Modified: 2010-04-02
I am using CreateProcess to start applications.  I maintain the process handle and the thread handle returned in the PROCESS_INFORMATION structure.  At various points I check to see if the program is still running (this works fine), if it is I need to bring it to the front (doesn't work).  I have tried the following code:
ShowWindow(hWnd, SW_SHOWNORMAL);
I have tried these two statements seperately, together in different orders and by passing the process handle and then the thread handle.  It will not bring the window to the front.  (Please do not Answer the question, use comments.)  The program is running on a Windows 98 machine.    
Question by:Gryph
  • 3
  • 2
LVL 32

Expert Comment

ID: 2376343
Use BringWindowToTop() instead of SetForegroundWindow()

Expert Comment

ID: 2376344
Why don't you try The "SetWindowPos" ? It can change the size, position, and Z order of a child, pop-up, or top-level window. Child, pop-up, and top-level windows are ordered according to their appearance on the screen. The topmost window receives the highest rank and is the first window in the Z order.

I noticed that you your CreateProcess to a applications, of  course you can get the handle of that application's main window. use it as the parameter for SetWindowPos,  use "HWND_TOPMOST" as the second parameter, It Places the window above all non-topmost windows. The window maintains its topmost position even when it is deactivated.

You also can use "FindWindow" function to get the handle of that window if you know the class name or window name.
good lucky!

Author Comment

ID: 2376463
From the MSDN documentation on SetWindowPos:

"If an application is not in the foreground, and should be in the foreground, it must call the SetForegroundWindow function."

The concern I have is that SetWindowPos may run into the same problem as the other things I've tried.  Can you tell me why this would work or why the other code will not work?  Also, the application that is running this code is not and will never be in the foreground, it is invisible to the user.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

LVL 32

Accepted Solution

jhance earned 680 total points
ID: 2376467
Use BringWindowToTop() instead of SetForegroundWindow()

Author Comment

ID: 2376494
Thanks!!  :)  

Author Comment

ID: 2391513
The mistake I was making was in passing the Thread and Process Handles.  Once I started using the window handles, BringWindowToTop worked.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

588 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