Bring the VB.Net window on top of all windows.

I have a window that needs to stay on top of the Power Point slides. Therefore, it should be on top of all windows. I did it easily using VB 6 using Lib "user32" but it seems to be incompatible with VB.net.

Me.TopMost = True

      

This does not work as it only works 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 an error! Any help is greatly appreciated! Thanks in advance,

Hello

Manjula

+2


source to share


1 answer


If you want a window in your application to always appear on top of another application's window, the feature is BringWindowToTop

definitely not what you want. First, as you noticed, you need to call the function multiple times using a timer. This should be your first clue that this is the wrong API. Another problem is that it only brings your window to the beginning of the Z order for its process, and not all other processes running on the system. As the documentation explains ,

Calling this function is similar to calling a function SetWindowPos

to change the position of a window in Z order. BringWindowToTop

Does not make the window a top-level window.

This last sentence should indicate that there is a better way. Windows has built-in support for top-level windows (that is, those that should always appear on top of other windows): these are called top-most windows. This is exactly what you want. The topmost windows are always displayed above the non-topmost windows.

Raymond Chen tries to explain some of the confusion on his blog . Note that is HWND_TOP

equivalent in this case BringWindowToTop

. You want instead HWND_TOPMOST

.

The easiest way to make the top of the window is to specify a flag WS_EX_TOPMOST

when creating the window. The NET Framework hides most of the window creation work behind the scenes, but you can tweak the options as needed by overriding a property on CreateParams

your form class.



Here is some sample code to make the form always top-most:

Protected Overrides ReadOnly Property CreateParams() As CreateParams
    Get
        Const WS_EX_TOPMOST As Integer = &H00000008

        Dim cp As CreateParams = MyBase.CreateParams
        cp.ExStyle = cp.ExStyle Or WS_EX_TOPMOST
        Return cp
    End Get
End Property

      

This won't work if you want to switch the top-most state of the window at runtime. To do this, you need to P / Invoke SetWindowPos

. P / Invoke is similar to what you did in VB6 with an operator Declare

, but the semantics have changed slightly for the .NET world, so you cannot use your old VB6 statements Declare

in VB.NET.

This is exactly what the code might look like for VB.NET:

<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
End Function

Private Const SWP_NOSIZE As Integer = &H1
Private Const SWP_NOMOVE As Integer = &H2

Private Shared ReadOnly HWND_TOPMOST As New IntPtr(-1)
Private Shared ReadOnly HWND_NOTOPMOST As New IntPtr(-2)

Public Function MakeTopMost()
    SetWindowPos(Me.Handle(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
End Function

Public Function MakeNormal()
    SetWindowPos(Me.Handle(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
End Function

      

+12


source







All Articles