Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Make Window Stay On Top

Make Window Stay On Top

Scheduled Pinned Locked Moved C / C++ / MFC
c++hardwarehelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KingTermite
    wrote on last edited by
    #1

    I'm not too saavy in Visual C++ programming as most of my C/CPP has been in embedded platforms. I'm working on a host application which is a VC++ window. Essentially, for this one particular window I want it to stay on top all the time. I have added some code that was given to me by someone who has done it in another application before, but it does not seem to be working in mine. Can anyone help? Here is what I have:

    				CWnd appWnd;    // the application CWnd
    				CWnd *pAppWnd;  // a pointer to CWnd
    				pAppWnd = & appWnd;
    				pAppWnd = AfxGetMainWnd();
    
    				if (NULL != pAppWnd)
    				{
    					HWND appHandle; // window handle
    					appHandle = pAppWnd->GetSafeHwnd();
    
    					if (NULL != appHandle)
    					{
    						targetObj.m_pAppWnd = pAppWnd;						
    					}
    				}
    
    		// Cause window to stay on top
    		targetObj.m_bWindowStayOnTop = TRUE;
    

    targetObj is another class which is essentially being called by this (no "gui" elements in that class) and in that class, inside of a loop that he is in, I have this part:

    						// Used to keep parent window (test module) on top
    						if (TRUE == this->m_bWindowStayOnTop)
    						{
    							BOOL bReturn = FALSE; 
    
    							if (NULL != this->m_pAppWnd)
    							{
    								bReturn = this->m_pAppWnd->SetForegroundWindow();
    							}
    						}
    

    It seems that the return from SetForegroundWindow() is 0 which means it is failing. But I don't know why! Any ideas?


    There are only 10 types of people in this world....those that understand binary, and those that do not.

    C 1 Reply Last reply
    0
    • K KingTermite

      I'm not too saavy in Visual C++ programming as most of my C/CPP has been in embedded platforms. I'm working on a host application which is a VC++ window. Essentially, for this one particular window I want it to stay on top all the time. I have added some code that was given to me by someone who has done it in another application before, but it does not seem to be working in mine. Can anyone help? Here is what I have:

      				CWnd appWnd;    // the application CWnd
      				CWnd *pAppWnd;  // a pointer to CWnd
      				pAppWnd = & appWnd;
      				pAppWnd = AfxGetMainWnd();
      
      				if (NULL != pAppWnd)
      				{
      					HWND appHandle; // window handle
      					appHandle = pAppWnd->GetSafeHwnd();
      
      					if (NULL != appHandle)
      					{
      						targetObj.m_pAppWnd = pAppWnd;						
      					}
      				}
      
      		// Cause window to stay on top
      		targetObj.m_bWindowStayOnTop = TRUE;
      

      targetObj is another class which is essentially being called by this (no "gui" elements in that class) and in that class, inside of a loop that he is in, I have this part:

      						// Used to keep parent window (test module) on top
      						if (TRUE == this->m_bWindowStayOnTop)
      						{
      							BOOL bReturn = FALSE; 
      
      							if (NULL != this->m_pAppWnd)
      							{
      								bReturn = this->m_pAppWnd->SetForegroundWindow();
      							}
      						}
      

      It seems that the return from SetForegroundWindow() is 0 which means it is failing. But I don't know why! Any ideas?


      There are only 10 types of people in this world....those that understand binary, and those that do not.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      ::SetWindowPos(HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); If that works, I am a superman, seeing as I've done nothing like this for years. It's damn close though. setwindowpos takes as it's first parameter a value that allows you to make a window move to the top or bottom, or make it topmost (i.e. top and stay there ). You pass whatever you like for the position and size, and use flags to make them irrelevant. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

      K 1 Reply Last reply
      0
      • C Christian Graus

        ::SetWindowPos(HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); If that works, I am a superman, seeing as I've done nothing like this for years. It's damn close though. setwindowpos takes as it's first parameter a value that allows you to make a window move to the top or bottom, or make it topmost (i.e. top and stay there ). You pass whatever you like for the position and size, and use flags to make them irrelevant. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

        K Offline
        K Offline
        KingTermite
        wrote on last edited by
        #3

        You are my superman! ;) It wasn't exactly right, but (hang on..moving window now in my way)....it was damn close! :) Appreciated!


        There are only 10 types of people in this world....those that understand binary, and those that do not.

        C 1 Reply Last reply
        0
        • K KingTermite

          You are my superman! ;) It wasn't exactly right, but (hang on..moving window now in my way)....it was damn close! :) Appreciated!


          There are only 10 types of people in this world....those that understand binary, and those that do not.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          KingTermite wrote: hang on..moving window now in my way).... LOL - brilliant. And you're welcome :-) Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups