Keep form on top of taskbar

73 views Asked by At

I am using Windows 11 and am wanting to keep a form above the task bar, even when the task bar is clicked. There is a lot of empty space here and would like to use this space for a program. I am using vb code in visual studio.

I have tried the timer1 method to me.focus() every second, but this does not work. Every time I click on a taskbar icon other than the program, it hides behind the taskbar.

My TopMost property is True already.

This is what has been said before but unfortunately it seems to not work.

Create a timer, then add the following code:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles 
Timer1.Tick
Me.Focus()
End Sub
2

There are 2 answers

0
Vinton On

The solution for me was the following code:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles 
Timer1.Tick
Me.BringToFront()
End Sub
1
smoorke On

I've only tested this on windows 10. The taskbar on windows 11 may have a different classname.

Use Spy++ to find the right name if this doesn't work.

Imports System.Runtime.InteropServices
Public Class Form1
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Public Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr : End Function
    <DllImport("user32.dll")>
    Public Shared Function SetWindowLong(ByVal hwnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As UInteger) As Integer : End Function
    Public Const GWL_HWNDPARENT As Integer = -8

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Find the taskbar window, note: needs additional checks to see if we got the right window
        Dim taskbarhandle As IntPtr = FindWindow("Shell_TrayWnd", "")

        'This sets it to be always above taskbar. note: TopMost is also needed.
        SetWindowLong(Me.Handle, GWL_HWNDPARENT, taskbarhandle)
    End Sub
End Class

Note that the SetWindowLong documentation states that you should use SetParent, however this does not work and makes your window disappear.