Thanks Combo
Sorry - i should have started a new thread. In case similar minded individuals stumble across this thread while looking for this solution, i shall post what i have used...
PutFocus and SetActiveWindow were good to "select" the form, but as soon as the user clicks elsewhere the form is deactivated - and therefore, hidden behind other applications. This was proving difficult as the code i am using is run from an "invisible" window.
I wanted either System Modal (which isn't possible in Win2000+) or to be able to attach my form to other applications. The solution i have used isn't perfect - but does the job well enough (and flexible enough) for me. It sets the form as the Foreground Window and then sets it permanently as the Top Level window to disable other applications stealing focus.
I've broken this down to make it easier for beginners.
Here are the API calls to place outside of the procedure but in the same Window as the 'MakeFormActive' procedure below
Private Declare Function GetForegroundWindow Lib "user32" _
() As Integer
Private Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" _
(ByVal hwnd As Integer, _
ByVal lpString As String, _
ByVal nMaxCount As Integer) _
As Integer
Private Declare Function SetWindowPos Lib "user32" _
(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
Private Declare Function SetForegroundWindow Lib "user32" ( _
ByVal hwnd As Long _
) As Long
Private Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Integer, _
ByVal uCmd As Integer) _
As Integer
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const HWND_TOPMOST = -1
'Here is the procedure to make your form active and "almost" system modal
Sub MakeFormActive()
Dim hWndApp As Long
Dim appCaption As String
Dim lngAppCap As Long
'First get the activewindow
hWndApp = GetForegroundWindow
'Loop through all active processes by starting with _
the active window and then going to the next
Do While hWndApp <> 0
'Set a blank string to extract the window caption _
into
appCaption = Space(255)
'Fill this blank string with the current window _
caption
lngAppCap = GetWindowText(hWndApp, appCaption, 255)
'Check there is a valid window caption to test
If Len(Trim(appCaption)) <> 0 And Asc(Trim(appCaption)) <> 0 Then
'Check if this is the window you're searching for
If Left(appCaption, 17) = "My Window Caption" Then
'Activate your form as "almost" system modal
SetWindowPos hWndApp, HWND_TOPMOST, myForm.Top, myForm.Left, 10, 10, SWP_NOMOVE + SWP_NOSIZE
'Set form as the foreground window
SetForegroundWindow hWndApp
End IF
End If
hWndApp = GetWindow(hWndApp, 2)
Loop
End Sub
'Add the below call to the Initialise or Activate section of your form
Sub Form_Activate()
MakeFormActive
End Sub
I hope this helps someone!
sugarflux