Link to home
Start Free TrialLog in
Avatar of isnoend2001
isnoend2001Flag for United States of America

asked on

Turn off Top Most for form

I have a  form that pings my website every 30 minutes
Usually i am doing something else so the form is not visible
On a failure I call SetWindowPos to put the form on top of other programs. The problem is
the form cannot be hidden without unloading it once it is put on top.
How can i overcome this?
the code I am using:
Private Const SWP_NOMOVE = 2
Private Const SWP_NOSIZE = 1
Private Const Flags = SWP_NOMOVE Or SWP_NOSIZE
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
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

Public Sub SetWindowOnTop(ByVal p_hWnd As Long, Optional ByVal p_bOnTop As Boolean = True)
    If p_bOnTop Then
        Call SetWindowPos(p_hWnd, HWND_TOPMOST, 0, 0, 0, 0, Flags)
    Else
        Call SetWindowPos(p_hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, Flags)
    End If
End Sub

Calling it like this:
Call SetWindowOnTop(Me.hWnd, True)

Open in new window

Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Where is p_bOnTop set? I ask because it looks like if it were False then topmost would be turned off.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of isnoend2001

ASKER

MartinLiss missed setting that flag, that makes me wonder how it got stuck on top
I did miss that, sorry, but try this then.

Call SetWindowOnTop(Me.hWnd, False)
does the HWND_NOTOPMOST turn off top_most ?
I am going to put the top most on a timer  with a sound
After so many seconds turn off top most
It does turn off top most, but it only sends the form to behind all the other topmost windows. It will still be in front of non-topmost windows.
I'm actually not sure (even from the attached's description) so maybe you should try Graham's suggestion.
MartinLiss i had this but, the form was stuck on top, had to unload the form
If Counter >= seconds Then
    Timer1.Enabled = False
    sndPlaySound ByVal 0, 0 'stop sound
   Call SetWindowOnTop(Me.hWnd, False)
    End If

maybe the HWND_NOTOPMOST will work. I read somewhere that the topmost will not work correctly from the ide
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I wonder if widowstate = vbMinimized
would override topmost
I do not see a HWND_BOTTOM constant
Thanks guys
You can substitute the number 1 or add this declaration.

Private Const HWND_BOTTOM = 1
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013