views:

238

answers:

2

I made a smart device application in c# and I want to run it at startup (I know how to do that) but the problem is that I can't make my application to run minimized (hidden) on the first time.

I've tried this.Hide() and this.Visible = false and also used ShowWindow API with SW_HIDE(6) but non of them worked.

It seems it's impossible to use these methods on Form_Load() or in InitializeComponent() to start the application minimized or hidden. anyone can help me with this ?

+2  A: 

The CF automatically calls Show on the Form passed to Application.Run. There is no avoiding that without avoiding the call to Application.Run.

The SDF has an Application2.Run that takes a parameter to tell it to not show the form.

You could do the same by creating your own message pump (though it's not a straightforward thing to do).

ctacke
A: 

Well, I managed to solve this problem by using threads.

here is the solution :

consider that we have a Windows Form called frmMain.

I modified Program.cs in this way and now the application starts and hides the frmMain as soon as it's shown:

using System;
using System.Windows.Forms;
using System.Threading;

namespace HideApplicationOnStart
{
    delegate void hideForm();

    class Program
    {
        frmMain frmMain;

        static void Main(string[] args)
        {
            Program prog = new Program();

            new Thread(prog.showForm).Start(); //call Application.Run
            new Thread(prog.hideForm).Start(); //hide the form
        }

        void showForm()
        {
            frmMain = new frmMain();
            Application.Run(frmMain);
        }

        void hideForm()
        {
            wait();
            hideForm hideForm = new hideForm(frmMain.Hide);
            frmMain.Invoke(hideForm);
        }

        void wait()
        {
            while (frmMain == null) Thread.Sleep(1);
        }
    }
}

UPDATE:

another solution to this problem is calling this.Hide() on Paint Event. See Here (Thanks KMan)

x86shadow