Connecting Tech Pros Worldwide Help | Site Map

Bring To Front of a Process/Other application

DraguVaso
Guest
 
Posts: n/a
#1: Nov 20 '05
Hi,

I want my application to bring another application to the Front.

I thought best way to do this was by the Process-model:
Dim c As Process = Process.GetCurrentProcess()
Dim p As Process
For Each p In Process.GetProcessesByName("EXTRA")
If Left(p.MainWindowTitle, 4) = "Appl" Then
'Here I should be able to Bring the Process to the Front!!!
Exit Function
End If
Next p

Does anybody nows how to do this? Or another solution?

Thanks!

Pieter


Bob Powell [MVP]
Guest
 
Posts: n/a
#2: Nov 20 '05

re: Bring To Front of a Process/Other application


Windows Forms Tips and Tricks explains how to maintain a single instance of
an application and bring it to the front when it's activated.

http://www.bobpowell.net/tipstricks.htm


--
Bob Powell [MVP]
Visual C#, System.Drawing

Image transition effects, automatic persistent configuration and
design time mouse operations all in April's issue of Well Formed
http://www.bobpowell.net/wellformed.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://royo.is-a-geek.com/siteFeeder...aspx?FeedId=41





"DraguVaso" <pietercoucke@hotmail.com> wrote in message
news:O3isdJzNEHA.1160@TK2MSFTNGP09.phx.gbl...[color=blue]
> Hi,
>
> I want my application to bring another application to the Front.
>
> I thought best way to do this was by the Process-model:
> Dim c As Process = Process.GetCurrentProcess()
> Dim p As Process
> For Each p In Process.GetProcessesByName("EXTRA")
> If Left(p.MainWindowTitle, 4) = "Appl" Then
> 'Here I should be able to Bring the Process to the[/color]
Front!!![color=blue]
> Exit Function
> End If
> Next p
>
> Does anybody nows how to do this? Or another solution?
>
> Thanks!
>
> Pieter
>
>[/color]


DraguVaso
Guest
 
Posts: n/a
#3: Nov 20 '05

re: Bring To Front of a Process/Other application


Thanks!

I just found it myself like this:

Dim handle As IntPtr = prcSiclid.MainWindowHandle
Dim Win32Help As New Win32Helper
If Not IntPtr.Zero.Equals(handle) Then
Win32Helper.ShowWindow(handle, 1)
Win32Helper.SetForegroundWindow(handle)
End If

Public NotInheritable Class Win32Helper
<System.Runtime.InteropServices.DllImport("user32. dll", _
EntryPoint:="SetForegroundWindow", _
CallingConvention:=Runtime.InteropServices.Calling Convention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Public Shared Function _
SetForegroundWindow(ByVal handle As IntPtr) As Boolean
' Leave function empty
End Function

<System.Runtime.InteropServices.DllImport("user32. dll", _
EntryPoint:="ShowWindow", _
CallingConvention:=Runtime.InteropServices.Calling Convention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Public Shared Function ShowWindow(ByVal handle As IntPtr, _
ByVal nCmd As Int32) As Boolean
' Leave function empty
End Function

End Class ' End Win32Helper


"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:uou2YSzNEHA.620@TK2MSFTNGP10.phx.gbl...[color=blue]
> Windows Forms Tips and Tricks explains how to maintain a single instance[/color]
of[color=blue]
> an application and bring it to the front when it's activated.
>
> http://www.bobpowell.net/tipstricks.htm
>
>
> --
> Bob Powell [MVP]
> Visual C#, System.Drawing
>
> Image transition effects, automatic persistent configuration and
> design time mouse operations all in April's issue of Well Formed
> http://www.bobpowell.net/wellformed.htm
>
> Answer those GDI+ questions with the GDI+ FAQ
> http://www.bobpowell.net/gdiplus_faq.htm
>
> The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
> Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
> Bob's Blog: http://royo.is-a-geek.com/siteFeeder...aspx?FeedId=41
>
>
>
>
>
> "DraguVaso" <pietercoucke@hotmail.com> wrote in message
> news:O3isdJzNEHA.1160@TK2MSFTNGP09.phx.gbl...[color=green]
> > Hi,
> >
> > I want my application to bring another application to the Front.
> >
> > I thought best way to do this was by the Process-model:
> > Dim c As Process = Process.GetCurrentProcess()
> > Dim p As Process
> > For Each p In Process.GetProcessesByName("EXTRA")
> > If Left(p.MainWindowTitle, 4) = "Appl" Then
> > 'Here I should be able to Bring the Process to the[/color]
> Front!!![color=green]
> > Exit Function
> > End If
> > Next p
> >
> > Does anybody nows how to do this? Or another solution?
> >
> > Thanks!
> >
> > Pieter
> >
> >[/color]
>
>[/color]


Cor Ligthert
Guest
 
Posts: n/a
#4: Nov 20 '05

re: Bring To Front of a Process/Other application


Hi Pieter,

I don't know, I did not really check everything you wrote, however did you
look already to the forms TopMost property?

Cor


DraguVaso
Guest
 
Posts: n/a
#5: Nov 20 '05

re: Bring To Front of a Process/Other application


TopMost works in the application itself, but I don't think you can use it to
put another appliation on Top.
Even in the application itself TopMost doesn't always work fine. But the
"SetForegroundWindow" works always :-)

Thansk anyways :-)

"Cor Ligthert" <notfirstname@planet.nl> wrote in message
news:uWwiCazNEHA.1608@TK2MSFTNGP12.phx.gbl...[color=blue]
> Hi Pieter,
>
> I don't know, I did not really check everything you wrote, however did you
> look already to the forms TopMost property?
>
> Cor
>
>[/color]


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#6: Nov 20 '05

re: Bring To Front of a Process/Other application


* "Cor Ligthert" <notfirstname@planet.nl> scripsit:[color=blue]
> I don't know, I did not really check everything you wrote, however did you
> look already to the forms TopMost property?[/color]

This will make the window stay "always on top".

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#7: Nov 20 '05

re: Bring To Front of a Process/Other application


* "DraguVaso" <pietercoucke@hotmail.com> scripsit:[color=blue]
> I want my application to bring another application to the Front.
>
> I thought best way to do this was by the Process-model:
> Dim c As Process = Process.GetCurrentProcess()
> Dim p As Process
> For Each p In Process.GetProcessesByName("EXTRA")
> If Left(p.MainWindowTitle, 4) = "Appl" Then
> 'Here I should be able to Bring the Process to the Front!!!
> Exit Function
> End If
> Next p
>
> Does anybody nows how to do this? Or another solution?[/color]

'AppActivate'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Ken Wagnitz
Guest
 
Posts: n/a
#8: Nov 20 '05

re: Bring To Front of a Process/Other application


I tried Bob's C# code in his "Only one instance of an application"
article.
It didn't work for me in Windows 2000.

I then tried to email you about it Bob, but you are running FrontPage
extensions on your site, which didn't work for me. Presumably my
company firewall doesn't like them. Why you need FrontPage extensions
to handle a simple form is beyond me.

I am still looking for a solution to this problem. All I have found
on the net so far are solutions which don't work reliably, or guesses
from people (even Microsofties) who haven't tried what they are
suggesting.

Don't know if the barrier is .net, or Win2K/XP. I suspect the latter.

Ken.
Ken Wagnitz
Guest
 
Posts: n/a
#9: Nov 20 '05

re: Bring To Front of a Process/Other application


I tried Bob's C# code in his "Only one instance of an application"
article.
It didn't work for me in Windows 2000.

I then tried to email you about it Bob, but you are running FrontPage
extensions on your site, which didn't work for me. Presumably my
company firewall doesn't like them. Why you need FrontPage extensions
to handle a simple form is beyond me.

I am still looking for a solution to this problem. All I have found
on the net so far are solutions which don't work reliably, or guesses
from people (even Microsofties) who haven't tried what they are
suggesting.

Don't know if the barrier is .net, or Win2K/XP. I suspect the latter.

Ken.
DraguVaso
Guest
 
Posts: n/a
#10: Nov 20 '05

re: Bring To Front of a Process/Other application


Hi,

I used this to bring another application to the Front:

Hope this helps,

Pieter

Public Sub BringExtraToFront()
Dim prc As Process
Dim clsProc As New clsProcesses
prc = clsProc.ProcessExtra 'gets me the
process based on the name
Dim handle As IntPtr = prc.MainWindowHandle
Dim Win32Help As New Win32Helper
If Not IntPtr.Zero.Equals(handle) Then
Win32Helper.ShowWindow(handle, 1)
Win32Helper.SetForegroundWindow(handle)
End If
End Sub


Option Explicit On

Public Class clsProcesses

Public Shared Function PrevInstance() As Process
Dim c As Process = Process.GetCurrentProcess()

' Durchlaufen aller Prozesse mit gleichem Namen.
Dim p As Process
For Each p In Process.GetProcessesByName(c.ProcessName)

' Aktuellen Prozess nicht beachten.
If p.Id <> c.Id Then

' Es kann mehrere Prozesse gleichen Namens geben, die von
' unterschiedlichen Programmen stammen.
If p.MainModule.FileName = c.MainModule.FileName Then

' Prozess der ersten gefundenen anderen Instanz
' zurückgeben.
Return p
End If
End If
Next p

' Keine andere Instanz gefunden.
Return Nothing
End Function

Public Shared Function ExtraOpen() As Boolean
' Durchlaufen aller Prozesse mit gleichem Namen.
Dim p As Process
For Each p In Process.GetProcessesByName("EXTRA")
If Left(p.MainWindowTitle, 4) = SessionName Then
Return True
Exit Function
End If
Next p
Return False
End Function

Public Shared Function ProcessExtra() As Process
' Durchlaufen aller Prozesse mit gleichem Namen.
Dim p As Process
For Each p In Process.GetProcessesByName("EXTRA")
If Left(p.MainWindowTitle, 4) = SessionName Then
Return p
Exit Function
End If
Next p
Return Nothing
End Function

Public Sub New()
'error-handler die alle errors voor zn rekening neemt
AddHandler System.Windows.Forms.Application.ThreadException,
AddressOf GlobalErrorHandler
End Sub
End Class

Public NotInheritable Class Win32Helper
<System.Runtime.InteropServices.DllImport("user32. dll", _
EntryPoint:="SetForegroundWindow", _
CallingConvention:=Runtime.InteropServices.Calling Convention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Public Shared Function _
SetForegroundWindow(ByVal handle As IntPtr) As Boolean
' Leave function empty
End Function

<System.Runtime.InteropServices.DllImport("user32. dll", _
EntryPoint:="ShowWindow", _
CallingConvention:=Runtime.InteropServices.Calling Convention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Public Shared Function ShowWindow(ByVal handle As IntPtr, _
ByVal nCmd As Int32) As Boolean
' Leave function empty
End Function

End Class ' End Win32Helper



"Ken Wagnitz" <kwagnitz@bigfoot.com> wrote in message
news:21fcf54.0405202057.53d28570@posting.google.co m...[color=blue]
> I tried Bob's C# code in his "Only one instance of an application"
> article.
> It didn't work for me in Windows 2000.
>
> I then tried to email you about it Bob, but you are running FrontPage
> extensions on your site, which didn't work for me. Presumably my
> company firewall doesn't like them. Why you need FrontPage extensions
> to handle a simple form is beyond me.
>
> I am still looking for a solution to this problem. All I have found
> on the net so far are solutions which don't work reliably, or guesses
> from people (even Microsofties) who haven't tried what they are
> suggesting.
>
> Don't know if the barrier is .net, or Win2K/XP. I suspect the latter.
>
> Ken.[/color]


Closed Thread