从任务列表和ALT + TAB命令隐藏VB.Net应用程序?

时间:2015-02-14 17:43:35

标签: .net vb.net alt-tab

我正在创建一个我想在后台运行的小型检查器应用程序,它只有一个简单的计时器,用于检查某个进程是否正在运行,但是我想从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

如果有帮助,可以找到原始Microsoft文章here

3 个答案:

答案 0 :(得分:2)

摆脱旧代码......

您在VB.Net中需要做的就是将表单的FormBorderStyle设置为FixedToolWindow,并将ShowInTaskBar设置为False:

  

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

答案 1 :(得分:0)

在表单加载事件中尝试此操作:

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-tab菜单中隐藏。有关详细信息,请参阅:http://www.pinvoke.net/default.aspx/Enums/WindowStylesEx.html

其他常量可以在同一个网站上找到,遗憾的是我无法发布更多链接。希望这回答了你的问题(如果它还没有得到回答)!

答案 2 :(得分:0)

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

2。每种形式都需要调用班级,如下所示

Dim x_cl_HideTaskView As _cl_HideTaskView = New _cl_HideTaskView(Me)

¹用于创建模块/类的代码(如步骤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