0

我正在创建一个我想在后台运行的小型检查器应用程序,它只有一个简单的计时器来检查某个进程是否正在运行,但是我想从 Alt-Tab 切换器中隐藏它并且任务列表是可能的也。我遇到了一些来自 Microsoft 的代码,但它来自 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 文章。

4

3 回答 3

2

摆脱那个旧代码......

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

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

于 2015-02-14T17:53:12.587 回答
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

其他常量可以在同一站点上找到,遗憾的是我无法发布更多链接。希望这回答了您的问题(如果尚未回答)!

于 2015-05-18T08:18:58.063 回答
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
于 2020-10-08T09:45:02.160 回答