By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,709 Members | 2,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,709 IT Pros & Developers. It's quick & easy.

Start with hidden Form

P: n/a
Hello,

im new to c# and i got an app with a notifyicon. Now i want to start the app
only with the notifyIcon, so that the Main-Form doesnt show up.

The form itself is used at the entrance point of the app:
Application.Run(new MainFrame());

Now i tried to use this to hide the Form and show up the trayBarIcon with
this in the constructor of the form.
this.Hide();
this.trayBarIcon.Visible = true;

He shows the trayBarIcon but he doesnt hide the form. The very same two
operation works in an event-call of a button-click but not by a call inside
the constructor of the form.
Can anyone tell me what im doing wrong ?
Thanks for the help,
oliver
Nov 19 '05 #1
Share this Question
Share on Google+
19 Replies


P: n/a
Hi Oliver,
set the WindowState property of your main form to Minimized in the form
constructor and set the ShowInTaskBar property to false also.

Hope that helps
Mark R Dawson
http://www.markdawson.org

"Oliver Neumann" wrote:
Hello,

im new to c# and i got an app with a notifyicon. Now i want to start the app
only with the notifyIcon, so that the Main-Form doesnt show up.

The form itself is used at the entrance point of the app:
Application.Run(new MainFrame());

Now i tried to use this to hide the Form and show up the trayBarIcon with
this in the constructor of the form.
this.Hide();
this.trayBarIcon.Visible = true;

He shows the trayBarIcon but he doesnt hide the form. The very same two
operation works in an event-call of a button-click but not by a call inside
the constructor of the form.
Can anyone tell me what im doing wrong ?
Thanks for the help,
oliver

Nov 19 '05 #2

P: n/a
Hey Mark,
set the WindowState property of your main form to Minimized in the form
constructor and set the ShowInTaskBar property to false also.


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
Nov 19 '05 #3

P: n/a
http://msdn.microsoft.com/msdnmag/is...s/default.aspx

Greg

"Oliver Neumann" <th*******@web.de> wrote in message
news:dl*************@news.t-online.com...
Hello,

im new to c# and i got an app with a notifyicon. Now i want to start the
app
only with the notifyIcon, so that the Main-Form doesnt show up.

The form itself is used at the entrance point of the app:
Application.Run(new MainFrame());

Now i tried to use this to hide the Form and show up the trayBarIcon with
this in the constructor of the form.
this.Hide();
this.trayBarIcon.Visible = true;

He shows the trayBarIcon but he doesnt hide the form. The very same two
operation works in an event-call of a button-click but not by a call
inside
the constructor of the form.
Can anyone tell me what im doing wrong ?
Thanks for the help,
oliver

Nov 19 '05 #4

P: n/a
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 Neumann wrote:
Hey Mark,

set the WindowState property of your main form to Minimized in the form
constructor and set the ShowInTaskBar property to false also.

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

Nov 19 '05 #5

P: n/a
Hey benny,

i added this.visible = false but still have this minimized form on the
bottom of my taskbar :/
oliver
"Benny Raymond" <be***@pocketrocks.com> schrieb im Newsbeitrag
news:um**************@TK2MSFTNGP09.phx.gbl...
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 Neumann wrote:
Hey Mark,

set the WindowState property of your main form to Minimized in the formconstructor and set the ShowInTaskBar property to false also.

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

Nov 19 '05 #6

P: n/a
Hey Greg,

"Greg Burns" <bl*******@newsgroups.nospam> schrieb im Newsbeitrag
news:Ou**************@TK2MSFTNGP12.phx.gbl...
http://msdn.microsoft.com/msdnmag/is...s/default.aspx

i guess you mean this point: "Removing that Main Window Dependency"

But i dont see the relevance to my problem ? I dont use threads for the
mainframe...
Can you tell me directly what am i have todo ?
cheers!
oliver
Nov 19 '05 #7

P: n/a
Have you tried the other code to see if it works? I'd suggest you do one
of the following:

1. Get rid of your current logic and try the other code (with the form
set up to never show in the task bar)... then if you want it to show in
the task bar, edit the logic and make it do so.

2. Instead of this.visible=false, use ShowWindow from user32 (it's in
the code, so you can look at it and see how that works too)

Oliver Neumann wrote:
Hey benny,

i added this.visible = false but still have this minimized form on the
bottom of my taskbar :/
oliver
"Benny Raymond" <be***@pocketrocks.com> schrieb im Newsbeitrag
news:um**************@TK2MSFTNGP09.phx.gbl...
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 Neumann wrote:
Hey Mark,

set the WindowState property of your main form to Minimized in the
form
constructor and set the ShowInTaskBar property to false also.
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


Nov 19 '05 #8

P: n/a

"Oliver Neumann" <th*******@web.de> wrote in message
news:dl*************@news.t-online.com...
Hey Greg,

"Greg Burns" <bl*******@newsgroups.nospam> schrieb im Newsbeitrag
news:Ou**************@TK2MSFTNGP12.phx.gbl...
http://msdn.microsoft.com/msdnmag/is...s/default.aspx

i guess you mean this point: "Removing that Main Window Dependency"

But i dont see the relevance to my problem ? I dont use threads for the
mainframe...
Can you tell me directly what am i have todo ?


That article starts off with talking about preventing running the same app
twice (singleton app). May or may not be relevant to what you are doing, but
I would imagine it would be pretty important for a notify icon app...

Like he says: "It's important to do all these small tasks when your app runs
as a Notify icon because..."

What made me think this article would help was the author talking about
starting a Notify icon program without a main window. Seems to be what your
trying to do, no? Never created a Notify icon program myself yet, so I am
afraid I can't tell you what to do directly to make it happen. But it looks
like it is all there in his example.

"Your main application class also creates the Notify icon and runs your main
message loop. Running an application without a main window is almost the
same as running with a main window-there are only two changes you need to
make. You need to change your call from Application.Run(FormName) to
Application.Run() so that you start a message loop without any main form. If
the user never creates the main window, you don't need to create it in an
invisible state; just don't create it at all. The second change is to handle
the SessionEnded event. If a user logs off Windows while your application is
running, Windows will request that your application exit by raising the
SessionEnded event. If you don't respond to this event, Windows will assume
your application just crashed. Windows will tell your users that your
application is not responding, and will request to terminate it. To fix
this, just handle the event and close your application."

Greg
Nov 19 '05 #9

P: n/a
Hey Benny,
1. Get rid of your current logic and try the other code (with the form
set up to never show in the task bar)... then if you want it to show in
the task bar, edit the logic and make it do so.


ok i actually try but i get this error:
Error 1 Inconsistent accessibility: parameter type 'ref
PortMonitor.MainFrame.RECT' is less accessible than method
'PortMonitor.MainFrame.GetWindowRect(System.IntPtr , ref
PortMonitor.MainFrame.RECT)' C:\Visual Studio
Projects\PortMonitor\PortMonitor\MainFrame.cs 1035 35 PortMonitor

Could you tell me what shall change at the struct Rect ? Im not into c++/c#
this much....
cheers,
oliver
Nov 20 '05 #10

P: n/a
When you get these types of errors, the easiest thing to do is to look
at the "public","private", etc that comes before the functions and structs

in this case if you change internal to public on the RECT struct it
should solve the problem

Oliver Neumann wrote:
Hey Benny,

1. Get rid of your current logic and try the other code (with the form
set up to never show in the task bar)... then if you want it to show in
the task bar, edit the logic and make it do so.

ok i actually try but i get this error:
Error 1 Inconsistent accessibility: parameter type 'ref
PortMonitor.MainFrame.RECT' is less accessible than method
'PortMonitor.MainFrame.GetWindowRect(System.IntPtr , ref
PortMonitor.MainFrame.RECT)' C:\Visual Studio
Projects\PortMonitor\PortMonitor\MainFrame.cs 1035 35 PortMonitor

Could you tell me what shall change at the struct Rect ? Im not into c++/c#
this much....
cheers,
oliver

Nov 20 '05 #11

P: n/a
Hey benny,
When you get these types of errors, the easiest thing to do is to look
at the "public","private", etc that comes before the functions and structs

thanks for the help. worked...so my constructor of the MainForm looks
includes this now:

this.ShowInTaskbar = false;
ShowHideWindow();
this.trayBarIcon.Visible = true;
But the form itself still is shown in normal-position/size :/ Did i
forgot something ?

oliver
Nov 21 '05 #12

P: n/a
Since the window isn't visible when the constructor is run,
ShowHideWindow will make it visible... Remember, ShowHideWindow actually
"looks" at the window to see if it's hidden or not.

You should instead run ShowWindow( this.Handle, 0 ); from the
form.Loading event maybe?

Oliver Neumann wrote:
Hey benny,

When you get these types of errors, the easiest thing to do is to look
at the "public","private", etc that comes before the functions and structs


thanks for the help. worked...so my constructor of the MainForm looks
includes this now:

this.ShowInTaskbar = false;
ShowHideWindow();
this.trayBarIcon.Visible = true;
But the form itself still is shown in normal-position/size :/ Did i
forgot something ?

oliver

Nov 21 '05 #13

P: n/a
Hey benny,

Since the window isn't visible when the constructor is run,
ShowHideWindow will make it visible... Remember, ShowHideWindow actually
"looks" at the window to see if it's hidden or not.

You should instead run ShowWindow( this.Handle, 0 ); from the
form.Loading event maybe?

ok i cleared the constructor and added a Load-Event:
private void MainFrame_Load(object sender, EventArgs e)
{
//ShowHideWindow();
ShowWindow(this.Handle, 0);
}
But the form still appears :/ Hmm, does it ever worked for you ? You got
an example-code for me maybe ?

greets,
oliver
Nov 21 '05 #14

P: n/a
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;
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)]
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 );
}
}

}
}

Nov 23 '05 #15

P: n/a
Hmm... Also make sure you are never minimizing the form - it's not
something you want to do anymore. Catch the event that gets fired and
tell the form not to minimize then run showhide...

perhaps that was the problem all along?

Benny Raymond wrote:
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;
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)]
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 );
}
}

}
}

Nov 23 '05 #16

P: n/a
Hey Benny,
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.:

....

GREAT! Thats it...works now for me. thanks alot for the help. much
appreciated ;)
oliver
Nov 23 '05 #17

P: n/a
btw Benny, i got an much easier solution now:

public class Class1 : System.Windows.Forms.ApplicationContext
{
private Form1 f1;

[STAThread]
static void Main()
{
System.Windows.Forms.Application.Run(new Class1());
}

public Class1()
{
this.f1 = new Form1();
this.f1.Visible = false;
this.f1.VisibleChanged += new EventHandler(this.f1_VisibleChanged);
}

private void f1_VisibleChanged(object sender, EventArgs e)
{
this.MainForm = this.f1;
}
}
This works too and saves some lines ;)
thanks anyway,
oliver
Nov 23 '05 #18

P: n/a
The reason for the code in the previous threads was so that if the
window was hidden behind other windows but still not in the "hidden" or
"minimized" state and you clicked on the notify icon to "show" it, the
window would be shown instead of hidden... That all causes confusion to
the end user so the code previous was in place so that the user could
double click the icon and the window would be smart about whether or not
it would show. :)

If you're only hiding the window from the task bar when it's minimized
then the following code works fine... However if you never show the form
in the task bar you'll need to make the notify icon smarter.

actually... I have no clue what the code below is doing... ;) could you
explain it a bit?

Oliver Neumann wrote:
btw Benny, i got an much easier solution now:

public class Class1 : System.Windows.Forms.ApplicationContext
{
private Form1 f1;

[STAThread]
static void Main()
{
System.Windows.Forms.Application.Run(new Class1());
}

public Class1()
{
this.f1 = new Form1();
this.f1.Visible = false;
this.f1.VisibleChanged += new EventHandler(this.f1_VisibleChanged);
}

private void f1_VisibleChanged(object sender, EventArgs e)
{
this.MainForm = this.f1;
}
}
This works too and saves some lines ;)
thanks anyway,
oliver

Nov 23 '05 #19

P: n/a
Hey berry,
actually... I have no clue what the code below is doing... ;) could you
explain it a bit?


while i watched at it i became a bit confused now too haha. Well but it
still works ;)
greets,
oliver
Nov 23 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.