Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can I start my application as hidden?

Posted on 2007-11-20
5
Medium Priority
?
1,070 Views
Last Modified: 2013-12-17
Hi,

How may I start my Application with no visible Form. I just want to add A NotifyIcon in the systray. When the user DoubleClick my NotifyIcon, I will execute some code.

Thanks.
0
Comment
Question by:EEssam
5 Comments
 
LVL 2

Assisted Solution

by:talspaugh27
talspaugh27 earned 200 total points
ID: 20322034
Found this on another solution, if this doesnt help let me know and I will shoot off the link to the other solution to ya

static void Main()
  {
    System.ComponentModel.Container components;
    components = new System.ComponentModel.Container();
    using(components)
    {
      System.Windows.Forms.ContextMenu contextMenu;
      contextMenu = new System.Windows.Forms.ContextMenu();

      contextMenu.MenuItems.Add("&Close", new EventHandler(CloseMenuItem_Click));

      System.Windows.Forms.NotifyIcon notifyIcon;
      notifyIcon = new System.Windows.Forms.NotifyIcon(components);
      notifyIcon.Icon = System.Drawing.SystemIcons.Application;
      notifyIcon.ContextMenu = contextMenu;
      notifyIcon.Visible = true;

      Application.Run();
    }
  }

  static void CloseMenuItem_Click(object sender, EventArgs e)
  {
    System.Windows.Forms.Application.Exit();
  }
And then in your application, you can just create new forms whenever you want, based on whatever events are triggered.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20322052
Traditionally, in your Main method, you use to run a Form to start your application. Use an application context. Have a look to this skeleton:


    static class Program
    {
        public static AppContext Context;
 
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
 
            Context = new AppContext();
            Application.Run(Context);
        }
    }
 
    // Derive your own ApplicationContext class
    public class AppContext : ApplicationContext
    {
        private NotifyIcon MyNotifyIcon;
        // define other fields for your application
 
        public AppContext()
        {
            InitializeComponents();
 
            this.ThreadExit += new EventHandler(AppContext_ThreadExit);
        }
 
        #region Exit events
        void AppContext_ThreadExit(object sender, EventArgs e)
        {
            // some termination tasks
            NotifyIcon.Dispose();
        }
        #endregion
 
        #region Notify icon events
        private void MenuAbout_Click(object sender, EventArgs e)
        {
            AboutForm form = new AboutForm();
            form.ShowDialog();
        }
        private void MenuExit_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }
 
        #region Private methods
        private void InitializeComponents()
        {
            // Creates Notify Icon context menu
            ContextMenuStrip contextMenu = new ContextMenuStrip();
            ToolStripMenuItem MenuAbout = new ToolStripMenuItem("About...");
            ToolStripSeparator Separator1 = new ToolStripSeparator();
            ToolStripMenuItem MenuExit = new ToolStripMenuItem("Exit");
 
            contextMenu.ShowCheckMargin = true;
            contextMenu.ShowImageMargin = false;
            contextMenu.Size = new System.Drawing.Size(185, 126);
            contextMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                MenuAbout,
                Separator1,
                MenuExit
            });
 
            MenuAbout.Click += new System.EventHandler(MenuAbout_Click);
            MenuExit.Click += new System.EventHandler(MenuExit_Click);
 
            NotifyIcon = new NotifyIcon();
 
            NotifyIcon.ContextMenuStrip = contextMenu;
            NotifyIcon.Icon = new Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream("YourApp.Resources.note.ico"));
            NotifyIcon.Visible = true;
        }
        #endregion
    }

Open in new window

0
 

Author Comment

by:EEssam
ID: 20322385
Thanks guys but this is a lot of difficult work. I'm sure there are easier ways. I do not want to completely  get rid of the main form. I have several timers on it, contextMenu and notifyIcon... all visually designed and set. I just don't want to visually show the main form.
0
 
LVL 22

Expert Comment

by:cookre
ID: 20324763
form.Hide()
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 1800 total points
ID: 20325151
Easiest way is to set the WindowState to FormWindowState.Minimized at design time.
Then implement the Load event and put this.Hide() inside.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question