AllAPI Network - The KPD-Team

 
Allapi Network
 API-Guide
 ApiViewer

 API List

 
API Resources
 Tips & Tricks
 VB Tutorials
 Error Lookup
 
Misc Stuff
 VB examples
 VB Tools
 VB Links
 Top Downloads
 
This Site
 Search Engine
 Contact Form
 

Donate to AllAPI.net

Bring a Window to Top
 
by Sreejath


Introduction
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 ()
	Form2.Show
End Sub

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

Analysis
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.

Summary
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.

1 - 2 - 3 - 4 - 5
Back to Tutorials - Main

 

 


Copyright © 1998-2007, The Mentalis.org Team - Privacy statement
Did you find a bug on this page? Tell us!
This site is located at http://allapi.mentalis.org/