Hide From Alt-Tab List

N

Never Best

I have an application. I want it so when the user minimizs the window it
goes into the system tray, and while in the system tray I don't want it in
the "Alt-Tab" list. (When the program starts it starts in the system tray
aswell; only shows main window if they double click).

Currently to achive this when the user minimizes I have to set
"ShowInTaskbar - false" "ControlBox - false" "Size - 0,0" "Text - null"
"FormBorderStyle - FixedToolWindow". And then reset everything when they
double click the system tray. Is there a simplier solution to this? (The
reasion I have to set "Text - null" and "ControlBox - fasle" is because Size
wont go to 0,0 if they arn't set when FormBorderStyle is a ToolWindow).

Ideally I would like to simply set "WindowState - minimized"
"ShowInTaskbar - false" but when "FormBorderStyle - ToolWindow" is set the
window wont minimize to taskbar (it goes into a box above the start menu
icon), but the program always shows up in the Alt-Tab list unless I set the
FormBorderStyle to ToolBar.

I use VS.NET 2005. I tried "this.Visable = false" or "this.Hide()" but for
some reasion it doesn't work for my main window if I set "FormBorderStyle -
ToolWindow" (this.Visable doesn't seem to work for me even if borderstyle
isn't changed). I do the command right under my "InitializeComponent();" in
the form constructor... This is because its my main window? or should it be
working...


Any help would be appresiated. Thanks

NvrBst
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Never Best said:
I have an application. I want it so when the user minimizs the window it
goes into the system tray, and while in the system tray I don't want it in
the "Alt-Tab" list. (When the program starts it starts in the system tray
aswell; only shows main window if they double click).

Loom in the archives, I posted the code a time ago (I do not have the code
with me now). It's very easy just a couple of simples P/invokes
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Found it in the sent-items :)


[DllImport("user32.dll")]
public static extern int SetWindowLong( IntPtr window, int index, int
value);
[DllImport("user32.dll")]
public static extern int GetWindowLong( IntPtr window, int index);


const int GWL_EXSTYLE = -20;
const int WS_EX_TOOLWINDOW = 0x00000080;
const int WS_EX_APPWINDOW = 0x00040000;

private System.Windows.Forms.NotifyIcon notifyIcon1;


// I use two icons depending of the status of the app
normalIcon = new Icon(this.GetType(),"Normal.ico");
alertIcon = new Icon(this.GetType(),"Alert.ico");
notifyIcon1.Icon = normalIcon;

this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
this.Visible = false;
this.ShowInTaskbar = false;
iconTimer.Start();

//Make it gone frmo the ALT+TAB
int windowStyle = GetWindowLong(Handle, GWL_EXSTYLE);
SetWindowLong(Handle, GWL_EXSTYLE, windowStyle | WS_EX_TOOLWINDOW);
 
N

nvrbst

Ahh thank you. I kinda already knew about the p/invoke method from
http://dotnet247.com/247reference/msgs/45/228585.aspx which is
basically identical to yours but he removes the WS_EX_APPWINDOW tag
while doing the SetWindowLong.

Mainly I was hoping for a managed solution because at
http://pinvoke.net/ under the "GetWindowLong" its like !!Obsolet:
This function is UNSAFE!!

But it seems I can't set the window size to 0,0 (it goes to 2,2
min), and thus now I'm using the p/invoke method and it seems fine
:) Maybe later when a managed solution pop's up I'll be able to
change.

Thanks for your reply.


NvrBst
--
 
B

Benny Raymond

Take a look at this code... also use showintaskbar property of the form
and mark it false when you want it to get off teh task bar - removing an
item from the task bar will remove it from the alt tab list

Here is the code I used to get it to work, it's a little bit bootleg
(with the hasBeenActivated and what not)... but it worked... note: I
didn't add a notification icon, I also didn't do anything on load -
instead i moved everything into activated.:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private bool _hasBeenActivated = false;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent
call
//
this.Activated += new EventHandler(Form1_Activated);
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 271);
this.Name = "Form1";
this.ShowInTaskbar = false;
this.Text = "Form1";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

// the function you should call when you want to show or hide
the window
private void ShowHideWindow ()
{
int myVisibleState = this.GetWindowVisibleState(
this.Handle, 0, 0 );
if ( myVisibleState == WIND_VISIBLE )
{
// SW_HIDE = 0;
this.ShowInTaskbar = false;
ShowWindow( this.Handle, 0 );
}
else if ( myVisibleState == WIND_HIDDEN ||
myVisibleState == WIND_COVERED ||
myVisibleState == WIND_PARTIALLY_COVERED )
{
// SW_RESTORE = 9
this.ShowInTaskbar = true;
ShowWindow( this.Handle, 9 );
SetForegroundWindow( this.Handle );
}
}

// the following is the meat of the entire thing
// it does some visibility checks on a pixel matrix regarding your
// app's window - it then decides if the window is hidden
// or not
public const int WIND_HIDDEN = 0;
public const int WIND_VISIBLE = 1;
public const int WIND_COVERED = 21;
public const int WIND_PARTIALLY_COVERED = 3;
internal int GetWindowVisibleState(IntPtr hWnd, int iStepX, int
iStepY)
{
RECT rc = new RECT();
Point pt = new Point();
int i = 0;
int j = 0;
int width = 0;
int height = 0;
int iCountedDots = 0;
int iNotCoveredDots = 0;
IntPtr hAux = IntPtr.Zero;
if ( iStepX <= 0 ) iStepX = 4;
if ( iStepY <= 0 ) iStepY = 16;

if ( !this.Visible || this.WindowState ==
FormWindowState.Minimized )
return WIND_HIDDEN;
else
{
GetWindowRect( hWnd, ref rc );
width = rc.right - rc.left;
height = rc.bottom - rc.top;

for ( i = rc.top; i<rc.bottom; i+=( height/iStepY ) )
{
pt.Y = i;
for ( j=rc.left; j<rc.right; j+= ( width / iStepX ) )
{
pt.X = j;
hAux = WindowFromPoint( pt );
while ( GetParent( hAux ) != IntPtr.Zero )
hAux = GetParent( hAux );
if ( hAux == hWnd ) // another window!
iNotCoveredDots++;
iCountedDots++;
}
}
if ( iNotCoveredDots == iCountedDots ) // window is
completely visible
return WIND_VISIBLE;
else if ( iNotCoveredDots == 0 ) // they're all covered
return WIND_COVERED;
else // partial
return WIND_PARTIALLY_COVERED;
}
}

[DllImport("user32.dll")]
public static extern IntPtr WindowFromPoint(
System.Drawing.Point lpPoint);

[DllImport("user32.dll", ExactSpelling=true, CharSet=CharSet.Auto)]
public static extern IntPtr GetParent(IntPtr hWnd);

[DllImport("User32.dll", CharSet=CharSet.Auto)]
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT
rect);

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern bool ShowWindow(IntPtr hWnd, short State);

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern bool SetForegroundWindow(IntPtr hWnd);

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public Int32 left;
public Int32 top;
public Int32 right;
public Int32 bottom;
}

private void Form1_Activated(object sender, EventArgs e)
{
if (! _hasBeenActivated )
{
_hasBeenActivated = true;
ShowWindow( this.Handle, 0 );
}
}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top