|
-
May 13th, 2010, 03:32 PM
#1
Thread Starter
Frenzied Member
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!
-
May 13th, 2010, 04:48 PM
#2
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.
-
May 14th, 2010, 12:07 PM
#3
Thread Starter
Frenzied Member
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...
-
May 14th, 2010, 12:21 PM
#4
Re: Launch app and make top most window?
Post the code you are currently using
-
May 14th, 2010, 12:39 PM
#5
Thread Starter
Frenzied Member
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.
-
May 15th, 2010, 08:26 PM
#6
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.
-
May 16th, 2010, 11:44 AM
#7
Thread Starter
Frenzied Member
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...
-
May 16th, 2010, 10:26 PM
#8
Hyperactive Member
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.
-
May 17th, 2010, 09:17 AM
#9
Thread Starter
Frenzied Member
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...
-
May 17th, 2010, 11:31 AM
#10
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|