Example #1
0
        internal static void Main(string[] args)
        {
            var joinedArgs = String.Join(" ", args);

            Debug.WriteLine("MultiClip " + Assembly.GetExecutingAssembly().GetName().Version.ToString());

            if (!String.IsNullOrWhiteSpace(joinedArgs))
            {
                Debug.WriteLine("Invoked with command line parameters: " + joinedArgs);
            }

            // If we can not aquire a mutex lock (i.e. one already exists), that means we need
            // to send the command to the existing process.
            if (!EnsureMainInstance())
            {
                Debug.WriteLine("WARNING: Could not aquire a process mutex lock!");

                if (!String.IsNullOrWhiteSpace(joinedArgs))
                {
                    var client = new PipeClient();
                    client.Open();
                    client.SendAndRead(joinedArgs);
                    client.Close();
                }
                else
                {
                    MessageBox.Show("MultiClip is already active in the background.", "You tried to start MultiClip", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                Debug.WriteLine("This process has completed its work. Farewell, cruel world!");

                Environment.Exit(0);
                return;
            }

            Debug.WriteLine("Successfully aquired a mutex lock for MultiClip (" + PROCESS_MUTEX_NAME + "); this is the main process.");

            // Initialize server
            PipeServer.Start();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // Initialize tray
            tray              = new NotifyIcon();
            tray.Icon         = Icon.FromHandle(Resources.iconWhite.GetHicon());
            tray.Text         = "MultiClip";
            tray.Visible      = true;
            tray.DoubleClick += tray_DoubleClick;

            // Set up tray context menu
            tray.ContextMenu = new ContextMenu();
            tray.ContextMenu.MenuItems.Add("Clipboard 1", new EventHandler(OnClipClicked));
            tray.ContextMenu.MenuItems.Add("Clipboard 2", new EventHandler(OnClipClicked));
            tray.ContextMenu.MenuItems.Add("Clipboard 3", new EventHandler(OnClipClicked));
            tray.ContextMenu.MenuItems.Add("Clipboard 4", new EventHandler(OnClipClicked));
            tray.ContextMenu.MenuItems.Add("Clipboard 5", new EventHandler(OnClipClicked));
            tray.ContextMenu.MenuItems.Add("-");
            tray.ContextMenu.MenuItems.Add("Exit", new EventHandler(OnExitClicked));

            CommandProcessor.Process(args);

            if (!Clipper.WasConfigured)
            {
                Clipper.SetClipboard(0);
            }

            Application.ApplicationExit += Application_ApplicationExit;
            Application.Run();
        }