CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2011
    Posts
    2

    Force some other or a chosen application window to bottom

    Hello all, I used to script alot, but it has been years now..anyways, what I need to know, is the proper way in VBS or VB 6, to set or force a chosen applications' window to be BOTTOM all the time, esentially stay behind all other windows...all apps open in front of it. And it is not my app I need to do this with, that's why I need to know how to force someone else windows program to be bottom...I am using a shell replacement written by another company, and I need to force it to always be bottom..hope that makes sense. I believe I can get the program shells real window name using the I beleve spyxx.exe function in VB six...If I could rap an executable that I coud just run on login, with code that does this, that would be awesome...any help is greatly appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Force some other or a chosen application window to bottom

    Not really, other than to check constantly
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Mar 2011
    Posts
    2

    Re: Force some other or a chosen application window to bottom

    so it is a tall order to force an application to always be the furthest back? Ack....So no way at all?

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Force some other or a chosen application window to bottom

    you could always use the SetWindowPos API function, in a Timer event.

  5. #5
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Force some other or a chosen application window to bottom

    Ya, SetWindowPos has that optional constant.

    You can respond to changes in an event hook.

    In a module:
    Code:
    Const OB_REORDER As Long = 32772  '-- hwnd + ID + idChild is parent of zordering children
    Const WINEVENT_SKIPOWNPROCESS As Long = 2
    Const SYS_FOREGROUND As Long = 3 'active window changing.
    Const EVENT_MIN As Long = 1
    Const EVENT_MAX As Long = 2147483647
    Private Declare Function apiSetWinEventHook Lib "user32" Alias "SetWinEventHook" (ByVal eventMin As Long, ByVal eventMax As Long, ByVal hmodWinEventProc As Long, ByVal pfnWinEventProc As Long, ByVal idProcess As Long, ByVal idThread As Long, ByVal dwFlags As Long) As Long
    Private Declare Function apiUnhookWinEvent Lib "user32" Alias "UnhookWinEvent" (ByVal lHandle As Long) As Long
    Private eHook As Long
    
    Public Sub HookEvents()
        If eHook <> 0 Then Exit Sub
        eHook = apiSetWinEventHook(SYS_FOREGROUND, SYS_FOREGROUND, 0, AddressOf WinEventFunc, 0, 0, WINEVENT_SKIPOWNPROCESS)
    End Sub
    Public Sub UnHookEvents()
        If eHook <> 0 Then
           Dim r As Long
           r = apiUnhookWinEvent(eHook)
           If r <> 0 Then eHook = 0
        End If
    End Sub
    
    Private Function WinEventFunc(ByVal eHandle As Long, ByVal LEvent As Long, ByVal hwnd As Long, ByVal objId As Long, ByVal childId As Long, ByVal eThreadId As Long, ByVal eTime As Long) As Long
        On Error Resume Next
    
        If LEvent = SYS_FOREGROUND Then
          'SetWindowPos(
           Beep
        End If
    
        WinEventFunc = 0
    End Function

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Force some other or a chosen application window to bottom

    Quote Originally Posted by staverts View Post
    so it is a tall order to force an application to always be the furthest back? Ack....So no way at all?
    Well, uses may not like you hooking into their system. Be careful running those, as well.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Force some other or a chosen application window to bottom

    This is the SetWindowPos function and related constants: (It should wotk completely without hooking).
    Code:
    Public Declare Function SetWindowPos Lib "user32" ( _
           ByVal hWnd As Long, _
           ByVal hWia As Long, _
           ByVal X As Long, ByVal Y As Long, _
           ByVal cx As Long, ByVal cy As Long, _
           ByVal wFlags As Long _
      ) As Long
    
    Public Const SWP_NoSize = 1&
    Public Const SWP_NoMove = 2&
    Public Const SWP_NoZOrder = 4&
    Public Const SWP_NoOwnerZOrder = &H200
    Public Const SWP_FrameChanged = &H20
    
    Public Const HWND_Top = 0&
    Public Const HWND_Bottom = 1&
    Public Const HWND_TopMost = -1&
    Public Const HWND_NoTopMost = -2&
    You need a call like
    Code:
      SetWindowPos AppshWnd, HWND_Bottom, 0, 0, 0, 0, SWP_NoSize + SWP_NoMove
    You need the apps hWnd to do this.
    If you havent that one there are more complicated methods to obtain it, like enumerating windows and matching the text in the title bar, or by knowing the ProcessID.

  8. #8
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Force some other or a chosen application window to bottom

    Well the OP mentioned "all the time", in which case you would need the timer poll, or event/message hook to do properly.

  9. #9
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Force some other or a chosen application window to bottom

    I see. You want to capture any message which would send the app back to foreground.

    Hm... to avoid hooking, another method would be to call setwindowpos again every second or so, controlled by a timer. (?)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured