Also:
this.Visible = false;
Here's some really fun code for "smart" show/hide of a window when you
never want it to appear in the task bar... this way it will always show
when you hit show, even if it's just hidden behind something - this is
important when you are not in the task bar because you can't just alt
tab or click on the program's task bar item. Let me know if you need me
to explain what it's doing.
// 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;
ShowWindow( this.Handle, 0 );
}
else if ( myVisibleState == WIND_HIDDEN ||
myVisibleState == WIND_COVERED ||
myVisibleState == WIND_PARTIALLY_COVERED )
{
// SW_RESTORE = 9
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)]
internal struct RECT
{
public Int32 left;
public Int32 top;
public Int32 right;
public Int32 bottom;
}
Oliver said:
Hey Mark,
thanks for the fast reply. I did that and he minimized it and dont show in
taskbar. But he minimize to a little frame above the taskbar. Uhm you know
like when you minimize a open project within a program. It doesnt really
disappear, it just moves into a minimum of a form.
When i normalize it again and press minimize over the buttons afterwards he
really minimize if then so it becomes invisible.
Did i made something wrong ?
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
this.trayBarIcon.Visible = true;
Thanks,
oliver