How can I made a form on top but will disappear when switch to screen of other windows task ?

BarryTang
BarryTang used Ask the Experts™
on
I use the API : SetWindowPos to make a form always on top
as follows :

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 Sub Form_Load()
    SetWindowPos Form1.hwnd, -1, 0, 0, 0, 0, 1
End Sub

However, when I switch the screen to another task ( e.g. from my program to MS Word ), the on top window still on the top which is I don't want to !

Can I have a way to make a form on top but it will disappear when switching task ?


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I could be wrong, but I suggest that you don’t use this function since you are looking forward to make the form go back when switching to another application. The purpose of this api function sets how to move the window, or how the window will behave.
When you use “SetWindowPos Form1.hwnd, -1, 0, 0, 0, 0, 1”  what the first –1 means is to set the behavior of your form to be on TOPMOST, if you would like to turn it back behind other windows you should use instead NOTOPMOST.

Example: Open a new project and click the project window with the right button and add a module. Coppy the following declarations to it.


'************Declarations *************
Declare Function SetWindowPos Lib "user32.dll" (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 Const HWND_NOTOPMOST = -2
    Public Const HWND_TOPMOST = -1
    Public Const HWND_TOP = 0
    Public Const HWND_BOTTOM = 1
    Public Const SWP_FRAMECHANGED = &H20
    Public Const SWP_DRAWFRAME = SWP_FRAMECHANGED
    Public Const SWP_NOSIZE = &H1
'************************************


Now add a command button to the form1, and copy the following code to the
“Private Sub Command1_Click()” event
‘************************************
Dim flags As Long  ' the flags specifying how to move the window
Dim retval As Long  ' return value
Static togle As Boolean
If togle = False Then  'if is topmost
    flags = SWP_NOSIZE Or SWP_DRAWFRAME
    retval = SetWindowPos(Form1.hwnd, HWND_TOPMOST, 0, 0, 1, 1, flags)   ' move the window
Else 'set to normal
    flags = SWP_NOSIZE Or SWP_DRAWFRAME
    retval = SetWindowPos(Form1.hwnd, HWND_BOTTOM, 0, 0, 1, 1, flags)   ' move the window
End If
togle = Not togle 'switch on or off
‘************************************
as you could see SetWindowsPos set how the window will behave. And how the effect could be reversed. Windows could be placed behind other windows too.
But if you wan to make the windows go to the back and still want to use this function, you should make a function to set the windows position like the previous example.
I hope this could help you.

Best of lucks.!
Felipe_51
BarryTang,
You have 17 opened questions as of this date.  
I will be posting in each of these questions.  
Please take care of your opened questions older than 30 days by clicking on your logon name link and looking for UnlockedQ or using the links I am providing below.  
Please finalize these within 72 hours or I will ask administration to look into your account at that point.  If you have questions, please ask and I will provide assistance.  

http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_20187331.html
http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_20133725.html
http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_20088012.html
http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_20083509.html
http://www.experts-exchange.com/Miscellaneous/Games/Q_20382035.html
http://www.experts-exchange.com/Programming/Game_Development/Q_20382294.html
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20534626.html
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20521771.html
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20515842.html
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20513575.html
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20493221.html
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20436510.html
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20399960.html
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20395868.html
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20375410.html
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20373966.html
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20372811.html

Thank you.
Administrative Action - Force Accepted.

SpideyMod
Community Support Moderator @Experts Exchange

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial