将表单保持在任务栏顶部

问题描述 投票:0回答:2

我使用的是 Windows 11,并且希望在任务栏上方保留一个表单,即使单击任务栏也是如此。这里有很多空闲空间,想用这个空间来编写程序。我在 Visual Studio 中使用 vb 代码。

我每秒都尝试过 me.focus() 的timer1方法,但这不起作用。每次我点击程序以外的任务栏图标时,它都会隐藏在任务栏后面。

我的 TopMost 属性已经是 True 了。

这是之前说过的,但不幸的是它似乎不起作用。

创建一个定时器,然后添加以下代码:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles 
Timer1.Tick
Me.Focus()
End Sub
vb.net visual-studio taskbar
2个回答
0
投票

我的解决方案是以下代码:

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

0
投票

我只在 Windows 10 上测试过这一点。Windows 11 上的任务栏可能有不同的类名。

如果这不起作用,请使用 Spy++ 查找正确的名称。

Imports System.Runtime.InteropServices
Public Class Form1
    <DllImport("user32.dll", SetLastError:=True, CharSet:=Runtime.InteropServices.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)
        Me.TopMost = True
    End Sub
End Class
© www.soinside.com 2019 - 2024. All rights reserved.