user4567049
user4567049

Reputation:

Hiding a VB.Net App from the Task List and ALT+TAB Order?

I'm creating a small checker app that I want to run in the background, it just has a simple timer that checks that a certain process is running, however I want to hide this from the Alt-Tab switcher and the Task List is possible too. I came across some code from Microsoft but it's from 2003 and no longer works in the latest version of VB.Net, I'm getting an error with:

OwnerhWnd = GetWindow(Me.hWnd, GW_OWNER)

I've looked into it online and followed some of what people have said but to no avail. Many have suggested to others using Me.Handle but I couldn't get this to work either, just keep getting the same error:

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

Here's the code provided:

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

The original Microsoft article can be found here if that will help.

Upvotes: 0

Views: 3575

Answers (3)

Pinto Anto
Pinto Anto

Reputation: 1

1. Create a Module or Class ¹ using the below code.

2. Call the Class in each form which need to be hidden as shown below

Dim x_cl_HideTaskView As _cl_HideTaskView = New _cl_HideTaskView(Me)

¹ Code for creating Module/Class ( as mentioned in Step 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

Upvotes: 0

Dystopic
Dystopic

Reputation: 19

Try this out in your form Load event:

Call SetWindowLong(Me.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW)

You will need to import the following namespace:

Imports System.Runtime.InteropServices

As well as add this user32 function:

<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

Also, you will need the declare the constants for WS_EX_TOOLWINDOW and GWL_EXSTYLE somewhere:

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

Now your form will be hidden from both the task bar and the alt-tab menu. Read more about this at: http://www.pinvoke.net/default.aspx/Enums/WindowStylesEx.html

Other constants can be found on the same site, sadly I cannot post any more links. Hopefully this answered your question (if it hasn't been answered already)!

Upvotes: 0

Idle_Mind
Idle_Mind

Reputation: 39152

Get rid of that old code...

All you need to do in VB.Net is set the FormBorderStyle of the Form to FixedToolWindow, and set ShowInTaskBar to False:

FixedToolWindow - A tool window border that is not resizable. A tool window does not appear in the taskbar or in the window that appears when the user presses ALT+TAB. Although forms that specify FixedToolWindow typically are not shown in the taskbar, you must also ensure that the ShowInTaskbar property is set to false, since its default value is true.

Upvotes: 3

Related Questions