Solved

Always on top?

Posted on 2004-09-20
2
964 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
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now