I have a window which should stay on top of Power point slide shows. So it should be on top of all the windows. I did this easily using VB 6 using Lib "user32", but it seems to be difficut with VB.net.
Me.TopMost = True
This does not seem to work as it works only within the program.
Private Declare Function BringWindowToTop Lib "user32" Alias "BringWindowToTop" (ByVal hwnd As Long) As Long
Private Sub frmTmr_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
BringWindowToTop(Me.Handle)
End Sub
This also gives a error! Any help is greatly appreciated! Thanks in advance,
Regards
Manjula
If you want a window in your application to always appear on top of a window of a different application, then the
BringWindowToTop
function is definitely not what you want. For starters, as you've noticed, you have to repeatedly call the function using a timer. That should be your first clue that it's the wrong API. Another problem is that it's only bringing your window to the top of the Z order for its process, not all of the other processes running on the system. As the documentation explains,That last sentence should indicate that there is a better way. Windows has built-in support for top-level windows (i.e., those that should always appear on top of other windows): these are called top-most windows. This is exactly what you want. Top-most windows always appear above non-topmost windows.
Raymond Chen attempts to explain some of the confusion on his blog. Note that in this case,
HWND_TOP
is equivalent toBringWindowToTop
. Instead, you wantHWND_TOPMOST
.The simplest way of making a window top-most is to specify the
WS_EX_TOPMOST
flag when you create the window. The .NET Framework hides most of the window creation work behind the scenes, but you can customize the parameters when you need to by overriding theCreateParams
property of your form class.Here's some sample code to make a form always top-most:
This won't work if you want to toggle the top-most state of the window at run-time. To do that, you're going to have to P/Invoke the
SetWindowPos
function. P/Invoke is similar to what you used to do in VB6 with theDeclare
statement, but the semantics have changed slightly for the .NET world—that's why you can't use your old VB6Declare
statements in VB.NET.Here's what that code might look like for VB.NET: