Click to See Complete Forum and Search --> : On top of things


NeOmega
October 22nd, 2001, 08:52 PM
Is there any code to make a form always appear on top of other programs running (ex. Program A is on top of Internet Explorer even after you click Explorer).

Cakkie
October 23rd, 2001, 01:54 AM
You can use the SetWindowPos API to do this, paste this in a module. Then you can call it from all over the place.

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
'constants for SetWindowPos API
private Const SWP_NOMOVE = 2
private Const SWP_NOSIZE = 1
private Const HWND_TOPMOST = -1
private Const HWND_NOTOPMOST = -2

public Sub SetOnTop(byval hwnd as Long, byval bSetOnTop as Boolean)

Dim lR as Long
If bSetOnTop then
lR = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or _SWP_NOSIZE)
else
lR = SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or _SWP_NOSIZE)
End If

End Sub

' Somewhere else, like in the form load event
private Sub Form_Load()

SetOnTop me.Hwnd, true

End Sub




Tom Cannaerts
slisse@planetinternet.be

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook