Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error when using SetForegroundWindow

Status
Not open for further replies.

ejgb

Programmer
Oct 24, 2001
41
GB
I have a piece of code that checks to see if a previous instance of the application is running, and if so sets focus to the already opened instance.

I am using the SetForegroundWindow API routine, and it works fine if the already openned instance has been minimized to the taskbar.

However, if the open instance is open behind another window then I get the error message "Invalid Porcedure Call or Argumnet", and it closes down. The code I am using is below.

Sub ActivatePrevInstance()
Dim OldTitle As String
Dim PrevHndl As Long
Dim result As Long

'Save the title of the application.
OldTitle = App.Title

'Rename the title of this application so FindWindow
'will not find this application instance.
App.Title = "unwanted instance"

'Attempt to get window handle using VB6 class name
PrevHndl = FindWindow("ThunderRT6Main", OldTitle)

'Check if found
If PrevHndl = 0 Then
'No previous instance found.
Exit Sub
End If

'Get handle to previous window.
PrevHndl = GetWindow(PrevHndl, GW_HWNDPREV)

'Restore the program.
result = OpenIcon(PrevHndl)


'Activate the application.
result = SetForegroundWindow(PrevHndl)

'End the application.
End

End Sub


ED
 
You might try the BringWindowToTop API after a successful call to FindWindow.

PrevHndl = FindWindow("ThunderRT6Main", OldTitle)

'Check if found
If PrevHndl = 0 Then
BringWindowToTop PrevHndl
End If

End
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Unfortunatly I'm still getting the error when it gets to the SetForegorundWindow call.

It works OK when the existing instance is minimized, but if it is sat behind another window I get the error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top