开发者

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

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 开发者_JS百科it still displays.


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.


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);
      }
    }
  }
}


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


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.


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


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;
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜