none
How do I keep my app form beneath other commercial apps, eg Media Player RRS feed

  • Question

  • I know how to keep a form on top, but I want to keep my form behind other apps.

    What happens sometimes, I shell another app but my app stays on top & hide the app I have shell'd

    The apps I am using shell for are not my apps, I just want them to be on top of my app, how do I keep my app in the lowest order of microsoft loading other apps

    there is no ontop code in my app, just a basic app, just for info, this is ontop code I use in other apps, maybe some flag can be reversed

    Private Const HWND_TOPMOST As Short = -&H1S
        Private Const HWND_NOTOPMOST As Short = -&H2S
        Private Const SWP_NOSIZE As Short = &H1S
        Private Const SWP_NOMOVE As Short = &H2S
        '------------------
        Private Declare Sub SetWindowPos Lib "user32" (ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer)
        '------------------
        Private bOnTopState As Boolean
    
        Public Property AlwaysOnTop() As Boolean
    
            Get
                AlwaysOnTop = bOnTopState
            End Get
            Set(ByVal Value As Boolean)
                Dim lFlag As Integer
                On Error Resume Next
                If Value = True Then
                    lFlag = HWND_TOPMOST
                Else
                    lFlag = HWND_NOTOPMOST
                End If
                bOnTopState = Value
                Call SetWindowPos(Me.Handle.ToInt32, lFlag, 0, 0, 0, 0, (SWP_NOSIZE Or SWP_NOMOVE))
            End Set
    
    
    Me.AlwaysOnTop = True
        End Property



    • Edited by x38class Thursday, May 23, 2019 6:41 AM
    Thursday, May 23, 2019 5:29 AM

Answers

  • Hi,

    You are looking for this effect,  external app is in your Form?

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32")>
        Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    
        End Function
        <DllImport("user32")>
        Private Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    
        End Function
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Timer1.Enabled = True
            Timer1.Interval = 1000
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Dim hDeskTop As IntPtr = FindWindow(Nothing, "TestForm")
            SetParent(hDeskTop, Me.Handle)
        End Sub
    End Class

    Best Regards,

    Alex



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by x38class Monday, May 27, 2019 3:24 AM
    Friday, May 24, 2019 2:15 AM

All replies

  • Hi,

    You are looking for this effect,  external app is in your Form?

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32")>
        Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    
        End Function
        <DllImport("user32")>
        Private Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    
        End Function
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Timer1.Enabled = True
            Timer1.Interval = 1000
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Dim hDeskTop As IntPtr = FindWindow(Nothing, "TestForm")
            SetParent(hDeskTop, Me.Handle)
        End Sub
    End Class

    Best Regards,

    Alex



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by x38class Monday, May 27, 2019 3:24 AM
    Friday, May 24, 2019 2:15 AM
  • Thanks for your reply, not quite what I expected, have amended my code accordingly & seemed to work for me this 1 time, will wait for 2 days to see what happens.

    As I said, my app is using shell to launch an external app & when launched my app was sitting on top of the app I had launched, l need my app to sit behind any app I launch with shell,

    Friday, May 24, 2019 3:49 AM
  • Thanks for your contribution, seems to work fine so far
    Monday, May 27, 2019 3:25 AM
  • I use my app every day & trying to get my partner to learn (not easy!), so far my form has always been at bottom, but today, Shelling "THunderbird" my form stayed on top & thunderbird was below.

    I have found the following:

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
                    ByVal lpClassName As String, _
                    ByVal lpWindowName As String) As Long
                    
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
                    ByVal hWnd1 As Long, _
                    ByVal hWnd2 As Long, _
                    ByVal lpsz1 As String, _
                    ByVal lpsz2 As String) As Long
                    
    Private Declare Function SetParent Lib "user32" ( _
                    ByVal hWndChild As Long, _
                    ByVal hWndNewParent As Long) As Long
     
    Private Sub Form_Load()
        Dim ProgMan&, shellDllDefView&, sysListView&
        
        ProgMan = FindWindow("progman", vbNullString)
        shellDllDefView = FindWindowEx(ProgMan&, 0&, "shelldll_defview", vbNullString)
        sysListView = FindWindowEx(shellDllDefView&, 0&, "syslistview32", vbNullString)
        
        SetParent Me.hwnd, sysListView
    End Sub

    When including the code I get an error in the last line of code.

    The error is highlighted at "Me.hwnd" can anybody advise what the correct syntax is please

    Sunday, June 2, 2019 1:56 AM
  • Alex has kindly amended the error to read

    SetParent(me.Handle,syslistview)

    Friday, June 7, 2019 5:42 AM