Solved

Always on top?

Posted on 2004-09-20
2
1,025 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

717 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