Solved

Turn off Top Most for form

Posted on 2013-12-05
13
259 Views
Last Modified: 2013-12-05
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

0
Comment
Question by:isnoend2001
  • 6
  • 5
  • 2
13 Comments
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39698910
Where is p_bOnTop set? I ask because it looks like if it were False then topmost would be turned off.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 250 total points
ID: 39698922
Have you tried calling it with the constant: HWND_BOTTOM?
0
 

Author Comment

by:isnoend2001
ID: 39698956
MartinLiss missed setting that flag, that makes me wonder how it got stuck on top
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39698983
I did miss that, sorry, but try this then.

Call SetWindowOnTop(Me.hWnd, False)
0
 

Author Comment

by:isnoend2001
ID: 39698994
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
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39699009
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.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 45

Expert Comment

by:Martin Liss
ID: 39699011
I'm actually not sure (even from the attached's description) so maybe you should try Graham's suggestion.
0
 

Author Comment

by:isnoend2001
ID: 39699017
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
0
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
ID: 39699027
Try (as Graham suggested)

Call SetWindowPos(Me.hWnd, HWND_BOTTOM, 0, 0, 0, 0, Flags)
0
 

Author Comment

by:isnoend2001
ID: 39699056
I wonder if widowstate = vbMinimized
would override topmost
I do not see a HWND_BOTTOM constant
0
 

Author Closing Comment

by:isnoend2001
ID: 39699070
Thanks guys
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39699077
You can substitute the number 1 or add this declaration.

Private Const HWND_BOTTOM = 1
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39699081
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now