VB.NET hide taskbar icon of an external application using win32 api

1.3k views Asked by At

In my project I need to hide the icon of an external application from taskbar. I am able to fetch appropriate window handle using FindWindow method in user32.dll

Is there any function to hide taskbar icon, something like:

HideTaskbarIcon (hwnd as IntPtr)

I have found the following code on google but can't understand the operators, can someone elaborate the operators used and their equivalent in visual basic:

long style= GetWindowLong(hWnd, GWL_STYLE);
style &= ~(WS_VISIBLE);    // this works - window become invisible 

style |= WS_EX_TOOLWINDOW;   // flags don't work - windows remains in taskbar
style &= ~(WS_EX_APPWINDOW); 

ShowWindow(hWnd, SW_HIDE); // hide the window
SetWindowLong(hWnd, GWL_STYLE, style); // set the style
ShowWindow(hWnd, SW_SHOW); // show the window for the new style to come into effect
ShowWindow(hWnd, SW_HIDE); // hide the window so we can't see it
1

There are 1 answers

2
Remy Lebeau On

Is there any function to hide taskbar icon, something like:

HideTaskbarIcon (hwnd as IntPtr)

No.

I have found the following code on google but can't understand the operators

Window styles are expressed as a bitmask. The code in question is using bitwise AND, NOT, and OR operators to manipulate the individual bits. It uses GetWindowLong() to retrieve the window's existing style bits, removes the WS_VISIBLE and WS_EX_APPWINDOW bits, adds the WS_EX_TOOLWINDOW bit, and then assigns the new bitmask back to the window. Only visible windows with the WS_EX_APPWINDOW bit can appear on the Taskbar.

That being said, the original code is wrong. Extended window styles (ones with _EX_ in their names) cannot be retrieved/assigned using GWL_STYLE, GWL_EXSTYLE must be used instead, and there is no reason to ever manipulate WS_VISIBLE directly as ShowWindow() handles that. The original code should have looked like this instead:

LONG style = GetWindowLong(hWnd, GWL_EXSTYLE);

style |= WS_EX_TOOLWINDOW;
style &= ~WS_EX_APPWINDOW; 

ShowWindow(hWnd, SW_HIDE);
SetWindowLong(hWnd, GWL_EXSTYLE, style);
ShowWindow(hWnd, SW_SHOW);
ShowWindow(hWnd, SW_HIDE);

can someone elaborate the operators used and their equivalent in visual basic

Microsoft has documentation on that topic:

Logical and Bitwise Operators in Visual Basic

Logical/Bitwise Operators (Visual Basic)

For example:

Dim hWnd as IntPtr = ...
Dim style as Integer = GetWindowLong(hWnd, GWL_EXSTYLE)

style = style or WS_EX_TOOLWINDOW
style = style and not WS_EX_APPWINDOW

ShowWindow(hWnd, SW_HIDE)
SetWindowLong(hWnd, GWL_EXSTYLE, style)
ShowWindow(hWnd, SW_SHOW)
ShowWindow(hWnd, SW_HIDE);