从任务列表和ALT+TAB顺序中隐藏VB.Net应用程序



我正在创建一个小的检查器应用程序,我想在后台运行它,它只有一个简单的计时器来检查某个进程是否正在运行,但我想对Alt-Tab切换器和任务列表隐藏这一点。我遇到了一些来自微软的代码,但它是2003年的,不再适用于最新版本的VB.Net,我遇到了一个错误:

OwnerhWnd = GetWindow(Me.hWnd, GW_OWNER)

我在网上查了一下,并关注了一些人的说法,但没有用。许多人建议其他人使用Me.Handle,但我也无法使其工作,只是不断收到相同的错误:

A first chance exception of type 'System.DllNotFoundException' occurred in Checkr.exe

这是提供的代码:

Public Class Form1
  Declare Function ShowWindow Lib "User" (ByVal hWnd As Integer,
  ByVal nCmdShow As Integer) As Integer
  Declare Function GetWindow Lib "User" (ByVal hWnd As Integer,
  ByVal wCmd As Integer) As Integer
  Const SW_HIDE = 0
  Const GW_OWNER = 4
Sub Form_Load ()
  Dim OwnerhWnd As Integer
  Dim ret As Integer
  ' Make sure the form is invisible:
  form1.Visible = False
  ' Set interval for timer for 5 seconds, and make sure it is enabled:
  timer1.Interval = 5000
  timer1.Enabled = True
  ' Grab the background or owner window:
  OwnerhWnd = GetWindow(Me.hWnd, GW_OWNER)
  ' Hide from task list:
  ret = ShowWindow(OwnerhWnd, SW_HIDE)
End Sub

Sub Timer1_Timer ()
  Dim ret As Integer
  ' Display a message box:
ret = MsgBox("Visible by Alt+Tab. Cancel to Quit", 1, "Invisible Form")
  ' If cancel clicked, end the program:
  If ret = 2 Then
     timer1.Enabled = False
     Unload Me
     End
  End If
End Sub

如果有帮助的话,可以在这里找到微软的原始文章。

去掉旧代码。。。

在VB.Net中,您只需要将窗体的FormBorderStyle设置为FixedToolWindow,并将ShowInTaskBar设置为False:

FixedToolWindow-不可调整大小的工具窗口边框。一个工具窗口没有出现在任务栏或显示的窗口中当用户按下ALT+TAB时。尽管指定了FixedToolWindow通常不会显示在任务栏中,您还必须确保ShowInTaskbar属性设置为false,因为默认值为true。

在表单加载事件中尝试一下:

Call SetWindowLong(Me.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW)

您需要导入以下命名空间:

Imports System.Runtime.InteropServices

以及添加此user32功能:

<DllImport("user32.dll", _
EntryPoint:="SetWindowLong")> _
Private Shared Function SetWindowLong(ByVal hWnd As IntPtr, _
                                     ByVal nIndex As Integer, _
                                     ByVal dwNewLong As Integer) _
                                 As Integer
End Function

此外,您还需要在以下位置声明WS_EX_TOOLWINDOW和GWL_EXSTYLE的常量:

Dim WS_EX_TOOLWINDOW as Integer = &H80
Dim GWL_EXSTYLE as Integer = -20

现在,您的表单将从任务栏和alt选项卡菜单中隐藏。有关此内容的详细信息,请访问:http://www.pinvoke.net/default.aspx/Enums/WindowStylesEx.html

其他常量可以在同一个网站上找到,遗憾的是我不能再发布任何链接了。希望这能回答你的问题(如果还没有回答的话)!

1。使用下面的代码创建一个模块或类cco

2.调用每个表单中需要隐藏的类,如下所示

Dim x_cl_HideTaskView As _cl_HideTaskView = New _cl_HideTaskView(Me)

cco创建模块/类的代码(如步骤1所述)

Imports System.Runtime.InteropServices
Module _g_ui_Fn_HideTaskView
Public Class _cl_HideTaskView
    Dim WithEvents x_form As Form
    Public Sub New(ByVal _form As Object)
        x_form = _form
    End Sub
    Private Sub x_form_Load(sender As Object, e As EventArgs) Handles x_form.Load
        _sub_hideTaskView()
    End Sub
#Region "Hide this form from Task View Window (ALT + TAB)"
    'Imports System.Runtime.InteropServices
    <DllImport("user32.dll", EntryPoint:="SetWindowLong")>
    Private Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
    End Function
    Private Sub _sub_hideTaskView()
        Dim WS_EX_TOOLWINDOW As Integer = &H80
        Dim GWL_EXSTYLE As Integer = -20
        Call SetWindowLong(x_form.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW)
    End Sub
#End Region
End Class
End Module

最新更新