VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



API Programming Series Article #3 Bring a Window to Top

by Sreejath S. Warrier (4 Submissions)
Category: Windows API Call/Explanation
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (5 Votes)

In the third article we see how to bring a window to top using the Win32 API.

Rate API Programming Series Article #3 Bring a Window to Top

font-family:Verdana;color:black'>In the previous article we saw how to declare
and invoke API functions from Visual Basic. In this article we see a small
example how to bring a window to top.

As usual we start with the API declaration.



Create a new VB Standard EXE project.

When you created the project, a form Form1 should have been added to the
project by default. Add another form to the project. Since this is only an
example to illustrate the above API call, we'll not change the properties of
the forms. So now we have two forms named Form1 and Form2 in the project. Add a
command button each to both the forms. Leave their names as Command1 itself.

In the General | Declarations section of both the forms, type in the following
code




Private Declare Function BringWindowToTop Lib "user32" Alias "BringWindowToTop" (ByVal hwnd As Long) As Long

font-family:Verdana;color:black'>



In the Click event procedure of the button on Form1 add the following code:




Private Sub Command1_Click ()

BringWindowToTop Form2.hwnd

End Sub

font-family:Verdana;color:black'>





In the Click event procedure of the button on Form2 add the following code:




Private Sub Command1_Click ()

BringWindowToTop Form1.hwnd

End Sub

font-family:Verdana;color:black'>





Now in the load event of Form1 (which should be the default form of the project
add the following code.




Private Sub Form_Load ()

Form2.Show

End Sub

color:black'>





Now if we press the command button on Form1, Form2 will be brought to top and
vice versa.

Analysis


color:black'>Let us see how this works.

First we declared the API function that we’re going to use, which in this case
is the BringWindowToTop encapsulated in the user32.dll. If you are not familiar
with the mechanics of declaring and invoking API functions, please go through
the Parts 1 and 2 of this series which describe the basics of API programming
in considerable detail.


color:black'> 


color:black'>The API function BringWindowToTop accepts the hwnd (Handle to the
Window, a unique id that all windows have) of the window that is to be brought
on top. It brings the specified window to the top of the Z order. If the window
is a top-level window, it is activated. If the window is a child window, the
top-level parent window associated with the child window is activated.

While adequate for the purpose of explaining the function, the above example is
rather trivial in nature. I.e. it doesn't achieve anything practical. So what
would be a practical application for this? Hmmm, say, you've got a long process
running in a window. Naturally you can expect your users to switch to other
windows during this period. However, once the process is complete you may want
to put this window on top. In such a case this code can be put to use. 


color:black'> 


color:black'>Summary


color:black'> 


color:black'>In this article we saw how to make a Window come on top of all
other windows using the API. If you have any questions or comments, please feel
free to contact me.


Download this snippet    Add to My Saved Code

API Programming Series Article #3 Bring a Window to Top Comments

No comments have been posted about API Programming Series Article #3 Bring a Window to Top. Why not be the first to post a comment about API Programming Series Article #3 Bring a Window to Top.

Post your comment

Subject:
Message:
0/1000 characters