API Programming Series Article #3 Bring a Window to Top

Submitted on: 1/24/2015 4:14:00 AM
By: Sreejath S. Warrier (from psc cd)  
Level: Intermediate
User Rating: By 5 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 374
     In the third article we see how to bring a window to top using the Win32 API.


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

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

Private Sub Command1_Click ()
BringWindowToTop Form2.hwnd
End Sub

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

Private Sub Command1_Click ()
BringWindowToTop Form1.hwnd
End Sub

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

Private Sub Form_Load ()
End Sub

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

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.

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.


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.

Other 3 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.