locked
How to make the windows form invisible for first time RRS feed

  • Question

  • Hi guys i am doing an C# application in which if userclick the shortcut of the application the main form for first time it shud be invisible for first time and when user clicks the shortcut next time the form should appear....... i am finding a tough time time in solving this as i am new to programming is there any way we can make the form invisible for first time when user click the shortcut and afterwards it should appear as normal for second or third time............

    Thanks and Regards,
    Monday, June 4, 2007 12:30 PM

Answers

  • Gotcha. When I have written a tray program I just hide the form calling this.Hide() in the form load event. Also, set the property ShowInTaskbar to false.
    Monday, June 4, 2007 1:05 PM
  • The tricky part here is that the second time you start the program, it needs to *not* run but communicate with the first instance of the program and tell it to make its window visible.  You can do this with a technique shown in this MSDN magazine article. It uses the Visual Basic application framework, all the nasty details of having programs talk to each other are taken care of.

    Start by adding a reference to Microsoft.VisualBasic.dll with Project + Add Reference.  Then make your Program.cs source code file look like this:

    using System;
    using System.Windows.Forms;
    using Microsoft.VisualBasic.ApplicationServices;

    namespace WindowsApplication1 {
      static class Program {
        public static bool mShowEnabled;
        [STAThread]
        static void Main() {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          // NOTE: allow form to show when we're debugging the code!
          if (System.Diagnostics.Debugger.IsAttached) mShowEnabled = true;
          SingleInstanceProgram.Run(new Form1());
        }
      }
      public class SingleInstanceProgram : WindowsFormsApplicationBase {
        private SingleInstanceProgram() {
          base.IsSingleInstance = true;
        }
        public static void Run(Form f) {
          SingleInstanceProgram app = new SingleInstanceProgram();
          app.MainForm = f;
          app.StartupNextInstance += new StartupNextInstanceEventHandler(app.StartupHandler);
          app.Run(Environment.GetCommandLineArgs());
        }
        private void StartupHandler(object sender, EventArgs e) {
          // Second instance of program was started, allow main form to be visible
          Program.mShowEnabled = true;
          this.MainForm.Visible = true;
        }
      }
    }

    Open the source code for your main form and add this method:

        protected override void SetVisibleCore(bool value) {
          if (!Program.mShowEnabled) value = false;
          base.SetVisibleCore(value);
        }

    Note that I added code to allow the form to be displayed when you are debugging it.  Debugging would otherwise be pretty difficult.
    Monday, June 4, 2007 1:30 PM

All replies

  • I'm not sure why you would want to do that, but you could use the forms visible property or opaque setting. In the form load event call this.Hide(), and it should make your form invisible. Depending on what you are doing I have had some trouble with that technique. Another I have used is to set the form property Opacity to 0. That will also make the form invisible.
    Monday, June 4, 2007 12:36 PM
  • i am writing a sheduler which triggers/starts main process  at specified time...... if sheduler is activated by  user i am reading from system registry and starting a back ground process at system startup which will be doing polling every few seconds and compare the time and at this time i should only place notification icon in system tray.... for this reason i have to hide the mainform


    Monday, June 4, 2007 12:49 PM
  • Gotcha. When I have written a tray program I just hide the form calling this.Hide() in the form load event. Also, set the property ShowInTaskbar to false.
    Monday, June 4, 2007 1:05 PM
  • The tricky part here is that the second time you start the program, it needs to *not* run but communicate with the first instance of the program and tell it to make its window visible.  You can do this with a technique shown in this MSDN magazine article. It uses the Visual Basic application framework, all the nasty details of having programs talk to each other are taken care of.

    Start by adding a reference to Microsoft.VisualBasic.dll with Project + Add Reference.  Then make your Program.cs source code file look like this:

    using System;
    using System.Windows.Forms;
    using Microsoft.VisualBasic.ApplicationServices;

    namespace WindowsApplication1 {
      static class Program {
        public static bool mShowEnabled;
        [STAThread]
        static void Main() {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          // NOTE: allow form to show when we're debugging the code!
          if (System.Diagnostics.Debugger.IsAttached) mShowEnabled = true;
          SingleInstanceProgram.Run(new Form1());
        }
      }
      public class SingleInstanceProgram : WindowsFormsApplicationBase {
        private SingleInstanceProgram() {
          base.IsSingleInstance = true;
        }
        public static void Run(Form f) {
          SingleInstanceProgram app = new SingleInstanceProgram();
          app.MainForm = f;
          app.StartupNextInstance += new StartupNextInstanceEventHandler(app.StartupHandler);
          app.Run(Environment.GetCommandLineArgs());
        }
        private void StartupHandler(object sender, EventArgs e) {
          // Second instance of program was started, allow main form to be visible
          Program.mShowEnabled = true;
          this.MainForm.Visible = true;
        }
      }
    }

    Open the source code for your main form and add this method:

        protected override void SetVisibleCore(bool value) {
          if (!Program.mShowEnabled) value = false;
          base.SetVisibleCore(value);
        }

    Note that I added code to allow the form to be displayed when you are debugging it.  Debugging would otherwise be pretty difficult.
    Monday, June 4, 2007 1:30 PM
  •  

    Hi,

     

    I think you want the application to run like a service (exp: antivirus programs, messangers)

     

    For that first you have make your application must be a single instance application. Only one application runs in the system.

     

    to find out whether the instance is already running use the following code:

    Code Snippet

     

    [DllImport("user32.dll")]
     private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

     

     [DllImport("user32.dll")]
      private static extern int SetForegroundWindow(IntPtr hWnd);

     

     [DllImport("user32.dll")]
      private static extern int IsIconic(IntPtr hWnd);


     

    public static bool IsAlreadyRunning()
      {
       string strLoc = Assembly.GetExecutingAssembly().Location;
       FileSystemInfo fileInfo = new FileInfo(strLoc);
       string sExeName = fileInfo.Name;
       bool bCreatedNew;

      Mutex mutex = new Mutex(true, "Global\\"+sExeName, out bCreatedNew);
       if (bCreatedNew)
        mutex.ReleaseMutex();

       return !bCreatedNew;
      }

     

     

    If the instance is already running then you have to switch to the running instance . use the following code snippet

    Code Snippet

     

    private static IntPtr GetCurrentInstanceWindowHandle()
      {   
       IntPtr hWnd = IntPtr.Zero;
       Process process = Process.GetCurrentProcess();
       Process[] processes = Process.GetProcessesByName(process.ProcessName);
       foreach(Process _process in processes)
       {
        // Get the first instance that is not this instance, has the
        // same process name and was started from the same file name
        // and location. Also check that the process has a valid
        // window handle in this session to filter out other user's
        // processes.
        if (_process.Id != process.Id &&
         _process.MainModule.FileName == process.MainModule.FileName &&
         _process.MainWindowHandle != IntPtr.Zero)   
        {
         hWnd = _process.MainWindowHandle;
         break;
        }
       }
       return hWnd;
      }

     

    public static void SwitchToCurrentInstance()
      {   
       IntPtr hWnd = GetCurrentInstanceWindowHandle();
       if (hWnd != IntPtr.Zero)   
       {   
        // Restore window if minimised. Do not restore if already in
        // normal or maximised window state, since we don't want to
        // change the current state of the window.
        if (IsIconic(hWnd) != 0)
        {
         ShowWindow(hWnd, SW_RESTORE);
        }

        // Set foreground window.
        SetForegroundWindow(hWnd);
       }
      }

     

     

     

    After this create you window and start the loop in you Main function. use the code snippet:

     

    Code Snippet

     

    [STAThread]
      static void Main(string[] args)

    {

    if(SingleApplication.IsAlreadyRunning())
       {
        SingleApplication.SwitchToCurrentInstance();
        return;
       }

    Form1 frm = new Form1();

    Application.Run();

    }

    }

     

    hope this will solve your problem

     

    All the best

     

     

    Monday, June 4, 2007 1:32 PM
  • Guyzzzzzzzzzzzz lot of help there i think this will solve my problem i will check this out in office tommorow thanks for all your support guys.....
    Hats off Smile



    Monday, June 4, 2007 1:45 PM
  • I have taken care of single instance issue before only i used the same code as Mr krishna provided befor my point is how to make the form invisible at system startup

    I iam implementing a sheduler for my application which automatically runs at specified time set by user....  i am writing a entry in system registry if user sets/checks the sheduling for his tasks........  my problem is if user selects the sheduling i should be able to put only the notification icon in the system tray and start the shedulerThread and i dont want to run the main form... and i shuld be able to show the mainform if user clicks the shortcut otherwise i have to place the notification icon in system tray and wait until the sheduler triggers the application..........

    I am able to do all the things like sheduing everything the only problem is i have to make the mainform invisible for first time when it is called at startup automatically by registry and make visible the main form only if user clicks the shortcut.....

    How to Hide mainform at first time i am using this.Hide() this.visible=false but nothing seems to help

    Monday, June 4, 2007 1:57 PM
  • Yes, hiding the form in the Load event doesn't work.  It will work in the Shown event but that's too late.  You'll need to override SetVisibleCore() as I showed you in my previous post.
    Monday, June 4, 2007 2:10 PM
  • Hi is there any way to unhide at startup if user does not selects the sheduling option................. tat is i have to start the applcation normally and allow main form to be visible at first time i get a bool value which indicates whether sheduling is set or not........   i tried it myself but i could not do it please can u say how to do it???

    Wednesday, June 6, 2007 4:45 AM