How to hide a window in start in c# desktop application?
Asked Answered
M

6

1

I am trying to make a desktop application that will be hidden but will display only after a time interval. I am trying to set Visible =false at window load event but it still displays.

Misfit answered 29/12, 2010 at 17:49 Comment(1)
I assume that this is a WinForms application since you are using Visible=False, is this correct?Dippy
D
3

For WinForms applications I have found that the easiest way to control the start-up visibility is to override the SetVisbileCore method.

Here is a simple example, the form will show after 5 seconds

using System;
using System.Windows.Forms;

namespace DelayedShow
{
  public partial class Form1 : Form
  {
    private bool _canShow = false;
    private Timer _timer;

    public Form1()
    {
      InitializeComponent();
      _timer = new Timer();
      _timer.Interval = 5000;
      _timer.Tick += new EventHandler(timer_Tick);
      _timer.Enabled = true;
    }

    void timer_Tick(object sender, EventArgs e)
    {
      _canShow = true;
      Visible = true;
    }

    protected override void SetVisibleCore(bool value)
    {
      if (_canShow)
      {
        base.SetVisibleCore(value);
      }
      else
      {
        base.SetVisibleCore(false);
      }
    }
  }
}
Dippy answered 29/12, 2010 at 18:0 Comment(2)
When are you calling Hide(), all Hide does is Visible=false. SetVsibileCore is the core function that is called when Show/Hide/Visible are used. The framework automatically tries to show the form so by overriding SetVisibleCore you can intercept that automatic call and provide your custom handling.Dippy
Yes u r right. I had to override SetVisibleCore. It does has the side effect as mentioned by @HansMisfit
M
10

The Visible property is a big deal in Winforms, setting it to true is what causes the native Windows window to be created. One side effect of which is that setting it to false in the OnLoad method or Load event doesn't work. There's nothing special about Hide(), it just sets Visible to false and thus doesn't work either.

Overriding SetVisibleCore() is a way to do it. It is however important that you still let the native window get created. You cannot Close() the form otherwise. Make it look like this:

    protected override void SetVisibleCore(bool value) {
        if (!IsHandleCreated && value) {
            value = false;
            CreateHandle();
        }
        base.SetVisibleCore(value);
    }

You can now call Show() or set Visible = true to make the window visible any time you wish. And call Close() even if you never made it visible. This is a good way to implement a NotifyIcon with a popup window that only is shown through a context menu.

Do note that this has a side-effect, the OnLoad() method and Load event won't run until the first time it actually gets visible. You may need to move code.

Mystique answered 29/12, 2010 at 18:18 Comment(0)
D
3

For WinForms applications I have found that the easiest way to control the start-up visibility is to override the SetVisbileCore method.

Here is a simple example, the form will show after 5 seconds

using System;
using System.Windows.Forms;

namespace DelayedShow
{
  public partial class Form1 : Form
  {
    private bool _canShow = false;
    private Timer _timer;

    public Form1()
    {
      InitializeComponent();
      _timer = new Timer();
      _timer.Interval = 5000;
      _timer.Tick += new EventHandler(timer_Tick);
      _timer.Enabled = true;
    }

    void timer_Tick(object sender, EventArgs e)
    {
      _canShow = true;
      Visible = true;
    }

    protected override void SetVisibleCore(bool value)
    {
      if (_canShow)
      {
        base.SetVisibleCore(value);
      }
      else
      {
        base.SetVisibleCore(false);
      }
    }
  }
}
Dippy answered 29/12, 2010 at 18:0 Comment(2)
When are you calling Hide(), all Hide does is Visible=false. SetVsibileCore is the core function that is called when Show/Hide/Visible are used. The framework automatically tries to show the form so by overriding SetVisibleCore you can intercept that automatic call and provide your custom handling.Dippy
Yes u r right. I had to override SetVisibleCore. It does has the side effect as mentioned by @HansMisfit
B
3

you can try this as well..

    public partial class Form1 : Form
{
public delegate void myHidingDelegate();

    public Form1()
    {
        InitializeComponent();
        myHidingDelegate dlg = new myHidingDelegate(mymethodcall);
        dlg.BeginInvoke(null, null);
    }

 public void mymethodcall()
    {
        myHidingDelegate dlg1 = new myHidingDelegate(HideForm);
        this.Invoke(dlg1);
    }

     public void HideForm()
    { this.Hide(); }
}

The only issue with this: Form flickers for a moment and disappears

Bergin answered 3/2, 2011 at 12:12 Comment(0)
A
1

Did you try this.Hide() instead of Visible = false?

Also another option can be to start the application without passing any form object in it.

Application.Run();

Wait for some time (using a Timer), and open your form.

Atheling answered 29/12, 2010 at 18:4 Comment(4)
Thanks. I am useing a timer. And I figured out that it works fine using Hide method. CheersMisfit
@decyclone, calling Hide() is the same as setting Visibile = false. Hide does nothing more and nothing less, so it is probably failing to hide the window on startup in the same way as Visible = false fails.Dippy
@Chris, I agree. But, there are much less details to figure what is going wrong. And what about the second approach I mentioned? I would like to have more details from OP.Atheling
@deyclone Does not work means does not hide the window. Though I haven't tried the second approach it does make sense.Misfit
Y
0

In your Main method, using Application.Run() instead of Application.Run(new Form1()). Then at some later time use new Form1() and form1.Show().

Ygerne answered 29/12, 2010 at 18:3 Comment(0)
W
0

Placing Your C# Application in the System Tray

  1. To get started, open an existing C# Windows form (or create a new one).
  2. Open the Visual Studio Toolbox.
  3. Drag a NotifyIcon control onto the form. The control will named notifyIcon1 by default and placed below the form because it has no visual representation on the form itself.
  4. Set the NotifyIcon control's Text property to the name you want to appear when the user pauses the mouse over the application's icon. For example, this value could be "KillerApp 1.0".
  5. Set the control's Icon property to the icon that you want to appear in the System Tray.

Tip: If you have a BMP file that you want to convert to an icon file, I highly recommend the QTam Bitmap to Icon 3.5 application.

-- Add an event handler for the form's Resize event that will hide the application when it's minimized. That way, it won't appear on the task bar.

private void Form1_Resize(object sender, System.EventArgs e)
  {
     if (FormWindowState.Minimized == WindowState)
        Hide();
  }

-- Add an event handler for the NotifyIcon.DoubleClick event and code it as follows so that the application will be restored when the icon is double-clicked.

private void notifyIcon1_DoubleClick(object sender,
                                       System.EventArgs e)
  {
      Show();
      WindowState = FormWindowState.Normal;
  }
Wyly answered 29/12, 2010 at 18:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.