0

I have developed a windows form application and installer for it. I had installed that app on my machine. Now when I restart my PC or Log in on machine the App gets launched and shown on desktop. A sys tray icon is also shown in sys-tray. Now I want to keep app hidden and only sys tray icon should be visible. Means app should not be displayed on screen but sys tray icon should be visible. I have used "CreateProcessAsCurrentUser" method in which I have set the value of "STARTF_USESHOWWINDOW" to different values. But still its not working. Also I am not getting which method of Application gets called on System startup. Is it Main() function from Program.cs file. Please tell me the solution and also the Function which gets called.

[STAThread]
    Main() function code: `static void Main()
    {
        Mutex mutex = new Mutex(false, "Application Name");
        try
        {
            if (mutex.WaitOne(0, false))
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
                Application.Run(new MainForm());
            }
            else
            {
                IntPtr pf = NativeMethods.FindWindow(null, "Application Name");
                NativeMethods.ShowWindow(pf, 0);
                NativeMethods.SetForegroundWindow(pf);
            }
        }

I have set the value of flag as below.

[Flags]
public enum CreateProcessFlags : uint
{
     STARTF_USESHOWWINDOW = 0x00000000,
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
C.M.W.
  • 133
  • 1
  • 8
  • 18
  • why not just hide the form? i.e.... var mainForm = new MainForm(); mainForm.Hide(); Application.Run(mainForm); ?? obviously depends what you are doing when you are initialising MainForm though. – Ahmed ilyas Nov 29 '13 at 11:28
  • @Ahmedilyas: I tried that by setting Windowstate to minimized after and before application.Run (new mainForm) method. But its didnt worked – C.M.W. Nov 29 '13 at 11:37
  • @HansPassant: can you please explain me in details what actually I have to do? I havent understood the explanation given at Pinvoke.com – C.M.W. Dec 04 '13 at 06:10

2 Answers2

0

try this.... put this in your form:

 protected override void SetVisibleCore(bool value)
        {
            base.SetVisibleCore(false);
        }

this will always now make the form invisible.

you will need some logic though to determine if it should be shown or not from other parts of your app. for example, set a global bool value and modify the code above to use that.

alternatively, you can use this:

protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            this.Visible = false;
        }

but you will see a bit of a flash when you run the app straight away. you then, again, need to control when to make it visible so check the global bool value for the visible property so you can eventually display the form

Ahmed ilyas
  • 5,722
  • 8
  • 44
  • 72
0

have you try this?

private void Form1_Load(object sender, EventArgs e)
    {


        this.WindowState = FormWindowState.Minimized;
        this.Hide();
        this.ShowInTaskbar = true;

    }

then use notifyicon

Angelo
  • 335
  • 4
  • 10