Results 1 to 10 of 10

Thread: Launch app and make top most window?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Launch app and make top most window?

    I have a .NET WinForms application that has a single form that is set as TopMost so that it runs above all windows on the desktop.

    This form has a single button on it to launch some other application, such as:

    Code:
    Dim app as Process
    app = Process.Start("notepad")
    How can I then make the app TopMost above my application which is already set to TopMost? In other words, notepad needs to be set as TopMost to bring it above my app. I assume this is a Win32 call of some sort. Thanks!

    Visual Studio 2010

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Launch app and make top most window?

    I'm sure the MSDN documentation for the TopMost property states that only other programs that are also marked as 'topmost' (like Task Manager for example) can appear on top of your form if you use the TopMost property. So you might find you have to make your other program TopMost as well (via Windows API) and then bring it to the front (again via Windows API).
    For making a window TopMost you can use the SetWindowPos API http://msdn.microsoft.com/en-us/libr...45(VS.85).aspx
    and I think that same API might also let you bring it to the front, but I've never tried using it for that.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: Launch app and make top most window?

    It is a third party app so I have no control over how that program is displayed. Hopefully I can figure out how to bring it to front, because so far setting the other program to top most with Win32 doesn't work. I probably have to do both somehow...

    Visual Studio 2010

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Launch app and make top most window?

    Post the code you are currently using
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: Launch app and make top most window?

    Here is the calling code:

    Code:
            Dim app As Process
            Try
                app = Process.Start("notepad")
                ptr = app.MainWindowHandle
                SetTopmostWindow(app.MainWindowHandle, True)
                SetForegroundWindow(app.MainWindowHandle)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    Here are the api declarations:

    Code:
        Private Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal _
            hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, _
            ByVal y As Long, ByVal cx As Long, ByVal cy As Long, _
            ByVal wFlags As Long) As Long
    
        PublicDeclare Function SetForegroundWindow Lib "user32" (ByVal hwnd As IntPtr) As Long
    
        Public Sub SetTopmostWindow(ByVal hWnd As Long, Optional ByVal topmost As Boolean = True)
            Const HWND_NOTOPMOST = -2
            Const HWND_TOPMOST = -1
            Const SWP_NOMOVE = &H2
            Const SWP_NOSIZE = &H1
            SetWindowPos(hWnd, IIf(topmost, HWND_TOPMOST, HWND_NOTOPMOST), 0, 0, 0, 0, _
                SWP_NOMOVE + SWP_NOSIZE)
        End Sub
    A key thing to remember is that the calling app window has been set as TopMost = True. I think that is the problem. It is conflicting with the ability to set Notepad on top of this window. But I need to do this because we are trying to lock the desktop until the user does something with the other app (notepad in this example), before closing our window.

    Visual Studio 2010

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Launch app and make top most window?

    Well you could test that theory by just setting the TopMost property for your app to False before you show Notepad couldnt you? Also, just making something TopMost will not stop the user from doing other things on the computer or 'lock' it.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: Launch app and make top most window?

    It seems that if my application form is set as TopMost, it is impossible to bring another window above it. If I turn off TopMost, it works. There should be a way to do this...

    Visual Studio 2010

  8. #8
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: Launch app and make top most window?

    Here are the updated .Net declarations.

    Code:
    <DllImport("user32.dll", SetLastError:=True)> _
    Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInt32) As Boolean
    End Function
    
    ReadOnly HWND_BOTTOM As New IntPtr(1)
    ReadOnly HWND_NOTOPMOST As New IntPtr(-2)
    ReadOnly HWND_TOP As New IntPtr(0)
    ReadOnly HWND_TOPMOST As New IntPtr(-1)
    Code:
    <DllImport("user32.dll")> _
    Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    Prefix has no suffix, but suffix has a prefix.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: Launch app and make top most window?

    Sorry, that doesn't work either. No error, just doesn't bring Notepad on top of my window and doesn't make Notepad Topmost...

    Visual Studio 2010

  10. #10
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: Launch app and make top most window?

    Take a look through here.
    Prefix has no suffix, but suffix has a prefix.

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