Results 1 to 9 of 9

Thread: How to setfocus on existing window

  1. #1

    Thread Starter
    Fanatic Member pradeepkrao's Avatar
    Join Date
    Sep 2001
    Location
    New Jersey
    Posts
    534

    How to setfocus on existing window

    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

  2. #2
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    1) Put all code inside [vbcode] [/vbcode] tags
    2) Do you not have a form?

    VB Code:
    1. BringWindowToTop Me.hwnd
    ??

  3. #3

    Thread Starter
    Fanatic Member pradeepkrao's Avatar
    Join Date
    Sep 2001
    Location
    New Jersey
    Posts
    534

    its not workin

    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

  4. #4
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    Copy the code on this link
    http://www.vbforums.com/showthread.p...p.previnstance
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  5. #5
    Megatron
    Guest
    Change your FindWindow line to match the following. Adding the classname will help narrow your search
    Code:
    '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")

  6. #6

    Thread Starter
    Fanatic Member pradeepkrao's Avatar
    Join Date
    Sep 2001
    Location
    New Jersey
    Posts
    534

    Thanks beachbum

    beachbum

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

    Thank you

    Pradeep

  7. #7
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Originally posted by Megatron
    Change your FindWindow line to match the following. Adding the classname will help narrow your search
    Code:
    '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?

  8. #8

    Thread Starter
    Fanatic Member pradeepkrao's Avatar
    Join Date
    Sep 2001
    Location
    New Jersey
    Posts
    534

    THANK YOU ALL

    Thank you guys,


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

    Thank you
    Pradeep

  9. #9
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    no probs man

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