How to hide form on startup

A

amil [c#newbie]

Hi all,

I am using the Notify Icon sample that came with .Net Docs and was able
to run it. However, I want to start the form as minimized. I have the
ShowInTaskbar = false and in the this is my code in the form load:

private void Form1_Load(object sender, System.EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
this.Visible = false;
}

This will still display a minimized window title bar near the Windows Start
button.
It does not totally hide the form.

Also, can I remove just the "X" or close button of the form and retain the
minimize/maximize buttons?

TIA.
 
A

amil [c#newbie]

Hi Ignacio,

Thanks for the feedback. The form still shows as a minimized titlebar
just above the Microsoft START button. I changed the code as follows:

1. Added to namespace definitions

using System.Runtime.InteropServices;

2. Declared P/Invoke in my Form1 class

[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;

3. I copied this code in the Form1_Load method

this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
this.Visible = false;
this.ShowInTaskbar = false;
int windowStyle = GetWindowLong(Handle, GWL_EXSTYLE);
SetWindowLong(Handle, GWL_EXSTYLE, windowStyle | WS_EX_TOOLWINDOW);


I noticed that the Min/Max buttons are gone as well.
 
A

amil [c#newbie]

Hi Nicholas,

Thank you for the feedback.

I think this proves that I am indeed a newbie. If I change the WindowState
= Minimized
in the form properties during design time, it actually hides the form when I
run the
code. No other code was necessary.




Nicholas Paldino said:
amil,

I don't think that you can remove the "X" button, but I do think that
you can disable it. Check out knowledge base article Q184686, titled
"HOWTO: Disable the Close Option on Control Menu of a VB Form", located at
(watch for line wrap):

http://support.microsoft.com/default.aspx?scid=kb;en-us;184686

While this applies for VB6, it will show you which API calls you have to
make in order to disable the box.

As for hiding the form, you should probably override the CreateParams
property so that it returns a CreateParams instance that has the appropriate
window styles set so that your form is initially minimized.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


amil said:
Hi all,

I am using the Notify Icon sample that came with .Net Docs and was able
to run it. However, I want to start the form as minimized. I have the
ShowInTaskbar = false and in the this is my code in the form load:

private void Form1_Load(object sender, System.EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
this.Visible = false;
}

This will still display a minimized window title bar near the Windows Start
button.
It does not totally hide the form.

Also, can I remove just the "X" or close button of the form and retain the
minimize/maximize buttons?

TIA.
 

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