?
Solved

Always on top?

Posted on 2004-09-20
2
Medium Priority
?
1,033 Views
Last Modified: 2006-11-17
Is there a way to set a console program to be "always on top" of other open windows?
0
Comment
Question by:fattumsdad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 12105264
Get the console's window handle like in http://support.microsoft.com/default.aspx?scid=kb;en-us;124103 ("How To Obtain a Console Window Handle")

   HWND GetConsoleHwnd(void)
   {
       #define MY_BUFSIZE 1024 // Buffer size for console window titles.
       HWND hwndFound;         // This is what is returned to the caller.
       char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
                                           // WindowTitle.
       char pszOldWindowTitle[MY_BUFSIZE]; // Contains original
                                           // WindowTitle.

       // Fetch current window title.

       GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);

       // Format a "unique" NewWindowTitle.

       wsprintf(pszNewWindowTitle,"%d/%d",
                   GetTickCount(),
                   GetCurrentProcessId());

       // Change current window title.

       SetConsoleTitle(pszNewWindowTitle);

       // Ensure window title has been updated.

       Sleep(40);

       // Look for NewWindowTitle.

       hwndFound=FindWindow(NULL, pszNewWindowTitle);

       // Restore original window title.

       SetConsoleTitle(pszOldWindowTitle);

       return(hwndFound);
   }


and modify the window style

HWND hWnd = GetConsoleHwnd();
LONG lStyle = GetWindowLong ( hWnd, GWL_EXSTYLE);

lStyle |= WS_EX_TOPMOST; // add "WS_EX_TOPMOST"

SetWindowLong ( hWnd, GWL_EXSTYLE, lStyle);

That style bit is

"Specifies that a window created with this style should be placed above all non-topmost windows and should stay above them, even when the window is deactivated."
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12105283
You may try that:

Find the window handle with:

   HWND hwnd = FindWindow("Command");

and make it topmost by

   RECT rect;
   GetWindowRect(hwnd, &rect);
   SetWindowPos(hwnd, HWND_TOPMOST,
                         rect.left, rect.right, rect.width(), rect.height(),
                         SWP_SHOWWINDOW);


Regards, Alex
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

770 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