PDA

Click to See Complete Forum and Search --> : How to setfocus on existing window


pradeepkrao
Oct 13th, 2001, 02:49 AM
Hi,

I have an application runnig. i want only one instance of it on my system. So i check for the previous instance and then if exist i get the handle and then set that window as fore ground window .. But it is not working fine.. Can any body help me out..

i am sending the code..

Sub Main()

Dim handle As Long
Dim lng As Long
Dim SaveTitle As String

If (App.PrevInstance) Then
SaveTitle = App.Title

handle = FindWindow(0&, "My Application")


If handle = 0 Then
handle = FindWindow(0&, SaveTitle)

End If


lng = BringWindowToTop(handle)

lng = SetForegroundWindow(handle)


If lng = 0 Then

MsgBox "Previous instance not found ", vbInformation

End If


'Kill the Duplicate Instance of the Application in any case.
End

End If
frmMyapplication.Show
End Sub




Reply ASAP

Pradeep

da_silvy
Oct 13th, 2001, 04:04 AM
1) Put all code inside tags
2) Do you not have a form?


BringWindowToTop Me.hwnd

??

pradeepkrao
Oct 13th, 2001, 04:36 AM
Its not working...

if you see the code i have sent you would find
BringWindowToTop(handle) which i have already used.. Bur i dont know why its not working....

pradeep

beachbum
Oct 13th, 2001, 05:10 AM
Hi
Copy the code on this link
http://www.vbforums.com/showthread.php?s=&threadid=103964&highlight=app.previnstance
Regards
Stuart

Megatron
Oct 13th, 2001, 11:24 AM
Change your FindWindow line to match the following. Adding the classname will help narrow your search

'Use This line when you are running a compiled EXE
strClass = ThunderRT6Form

'Use this line if you are running within the IDE
strClass = ThunderForm

handle = FindWindow(strClass, "My Application")

pradeepkrao
Oct 14th, 2001, 01:48 AM
beachbum

The link which you have mentioned is exactly what i wanted..

Thank you

Pradeep

da_silvy
Oct 14th, 2001, 02:31 AM
Originally posted by Megatron
Change your FindWindow line to match the following. Adding the classname will help narrow your search

'Use This line when you are running a compiled EXE
strClass = ThunderRT6Form

'Use this line if you are running within the IDE
strClass = ThunderForm

handle = FindWindow(strClass, "My Application")


Is he even using a form?

pradeepkrao
Oct 14th, 2001, 02:51 AM
Thank you guys,


Every thing is workin fine... Thank you da_silvy... your code also works fine..

Thank you
Pradeep

da_silvy
Oct 14th, 2001, 05:03 AM
no probs man