Solved

Always on top?

Posted on 2004-09-20
2
978 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

863 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