Example #1
0
    private Virtualtray()
    {
        _virtualbox = (IVirtualBox) new VirtualBox.VirtualBox();

        /* the generated VirtualBox.dll COM library doesn't work with the C# event
         * system, so we handle events by registering callbacks
         */
        _virtualbox.RegisterCallback(this);

        _icon = GetIcon("icon/icon-16.ico");
        _idleIcon = GetIcon("icon/icon-gray-16.ico");

        _notifyIcon = new NotifyIcon();
        _notifyIcon.Icon = _idleIcon;
        _notifyIcon.Text = "Virtualtray";
        _notifyIcon.Visible = true;

        _vms = new Hashtable();

        ContextMenu menu = new ContextMenu();
        foreach (IMachine machine in _virtualbox.Machines) {
            MachineMenu mm = new MachineMenu(machine, menu);
            mm.StateChange += new EventHandler(MachineStateChangeEventHandler);
            _vms.Add(machine.Id, mm);
        }
        menu.MenuItems.Add(new MenuItem("-") {Name = "-"});
        menu.MenuItems.Add(new MenuItem("Open VirtualBox...", new EventHandler(
            MenuLaunchVirtualBoxEventHandler)));
        menu.MenuItems.Add(new MenuItem("Exit", new EventHandler(MenuExitEventHandler)));
        _notifyIcon.ContextMenu = menu;

        ToggleIcon();
        _notifyIcon.MouseClick += new MouseEventHandler(IconClickEventHandler);
        Application.ApplicationExit += new EventHandler(ApplicationExitEventHandler);
    }
Example #2
0
        public MachineMenu(IMachine machine, Menu parentMenu)
        {
            _virtualbox = machine.Parent;
            _virtualbox.RegisterCallback(this);
            _session = null;
            _id = machine.Id;
            _name = machine.Name;
            _parentMenu = parentMenu;
            _menu = new MenuItem(machine.Name);

            /* initially create MenuItems for all possible VM actions, then show/hide
             * them as needed
             */
            object[][] submenus = new object [][] {
                new object[] {Submenu.Start, "Start Machine", new EventHandler(MenuStartEventHandler)},
                new object[] {Submenu.StartHeadless, "Start Machine Headless", new EventHandler(MenuStartHeadlessEventHandler)},
                new object[] {Submenu.Save, "Save Machine", new EventHandler(MenuSaveEventHandler)},
                new object[] {Submenu.Stop, "Stop Machine", new EventHandler(MenuStopEventHandler)}
            };

            Array.ForEach<object[]>(submenus, delegate(object[] s) {
                MenuItem m = new MenuItem((string) s[1], (EventHandler) s[2]);
                m.Visible = false;
                _submenu[(int) s[0]] = m;
                _menu.MenuItems.Add(m);
            });

            State = machine.State;

            int last = _parentMenu.MenuItems.IndexOfKey("-");
            last = last < 0 ? _parentMenu.MenuItems.Count : last;
            _parentMenu.MenuItems.Add(last, _menu);
        }