Beispiel #1
0
        public void Destroy()
        {
            var menuHandle = GetSystemMenu(WindowHandle, false);

            foreach (var item in _menuItems.Items.Where(x => x.Show))
            {
                var id = MenuItemId.GetId(item.Name);
                if (id > 0)
                {
                    DeleteMenu(menuHandle, id, Constants.MF_BYCOMMAND);
                }
            }

            DeleteMenu(menuHandle, MenuItemId.SC_SEPARATOR_BOTTOM, Constants.MF_BYCOMMAND);

            var numberItems = GetMenuItemCount(menuHandle);

            if (numberItems == DEFAULT_SYSTEM_MENU_NUMBER_ITEMS)
            {
                GetSystemMenu(WindowHandle, true);
            }
        }
Beispiel #2
0
        public bool Create()
        {
            var menuHandle = GetSystemMenu(WindowHandle, false);

            if (menuHandle == IntPtr.Zero)
            {
                return(false);
            }

            foreach (var item in _menuItems.Items)
            {
                if (item.Type == MenuItemType.Item && item.Show)
                {
                    var id    = MenuItemId.GetId(item.Name);
                    var title = GetTransparencyTitle(id);
                    title = GetTitle(item.Name, title, true);
                    if (!IsMenuItem(menuHandle, id))
                    {
                        InsertMenu(menuHandle, MenuItemId.SC_CLOSE, Constants.MF_BYCOMMAND, id, title);
                    }
                }

                if (item.Type == MenuItemType.Separator && item.Show)
                {
                    InsertMenu(menuHandle, MenuItemId.SC_CLOSE, Constants.MF_BYCOMMAND | Constants.MF_SEPARATOR, MenuItemId.SC_SEPARATOR, null);
                }

                if (item.Type == MenuItemType.Group && item.Show)
                {
                    var subMenuHandle = CreateMenu();

                    if (item.Name.ToLower() == "size")
                    {
                        for (int i = 0; i < _menuItems.WindowSizeItems.Count; i++)
                        {
                            var subItemId = MenuItemId.SC_SIZE_DEFINED + i;
                            _menuItems.WindowSizeItems[i].Id = subItemId;
                            if (!IsMenuItem(subMenuHandle, subItemId))
                            {
                                AppendMenu(subMenuHandle, Constants.MF_BYCOMMAND, subItemId, GetTitle(_menuItems.WindowSizeItems[i]));
                            }
                        }
                    }

                    if (item.Name.ToLower() == "move_to")
                    {
                        foreach (var moveToMenuItem in MoveToMenuItems)
                        {
                            AppendMenu(subMenuHandle, Constants.MF_BYCOMMAND, MenuItemId.SC_MOVE_TO + moveToMenuItem.Key, GetTitle("monitor") + moveToMenuItem.Key);
                        }
                    }

                    if (item.Name.ToLower() == "start_program")
                    {
                        for (int i = 0; i < _menuItems.StartProgramItems.Count; i++)
                        {
                            AppendMenu(subMenuHandle, Constants.MF_BYCOMMAND, MenuItemId.SC_START_PROGRAM + i, _menuItems.StartProgramItems[i].Title);
                        }
                    }

                    foreach (var subItem in item.Items)
                    {
                        if (subItem.Type == MenuItemType.Item && subItem.Show)
                        {
                            var subItemId = MenuItemId.GetId(subItem.Name);
                            var title     = GetTransparencyTitle(subItemId);
                            title = GetTitle(subItem.Name, title, true);
                            if (!IsMenuItem(subMenuHandle, subItemId))
                            {
                                InsertMenu(subMenuHandle, -1, Constants.MF_BYCOMMAND, subItemId, title);
                            }
                        }

                        if (subItem.Type == MenuItemType.Separator && subItem.Show)
                        {
                            InsertMenu(subMenuHandle, -1, Constants.MF_BYCOMMAND | Constants.MF_SEPARATOR, MenuItemId.SC_SEPARATOR, null);
                        }
                    }

                    var id = MenuItemId.GetId(item.Name);
                    if (!IsMenuItem(menuHandle, id))
                    {
                        InsertSubMenu(menuHandle, subMenuHandle, MenuItemId.SC_CLOSE, Constants.MF_BYCOMMAND | Constants.MF_POPUP, (uint)id, GetTitle(item.Name, null, true));
                    }
                    subMenuHandle = IntPtr.Zero;
                }
            }

            if (!IsMenuItem(menuHandle, MenuItemId.SC_SEPARATOR_BOTTOM))
            {
                InsertMenu(menuHandle, MenuItemId.SC_CLOSE, Constants.MF_BYCOMMAND | Constants.MF_SEPARATOR, MenuItemId.SC_SEPARATOR_BOTTOM, null);
            }

            return(true);
        }
        public void Create()
        {
            var windowMenuHandle = NativeMethods.GetSystemMenu(WindowHandle, false);
            int index            = NativeMethods.GetMenuItemCount(windowMenuHandle);

            _wasOriginalBefore = index > 0 && NativeMethods.GetMenuItemID(windowMenuHandle, index - 1) == MenuItemId.SC_CLOSE;

            _numberItems++;
            NativeMethods.InsertMenu(windowMenuHandle, index, NativeConstants.MF_BYPOSITION | NativeConstants.MF_SEPARATOR, IntPtr.Zero, "");


            IntPtr subMenuHandle;

            AddMenuItem(MenuItemId.SC_INFORMATION);
            AddMenuItem(MenuItemId.SC_ROLLUP);
            AddMenuItem(MenuItemId.SC_AERO_GLASS);
            AddMenuItem(MenuItemId.SC_TOPMOST);
            AddMenuItem(MenuItemId.SC_SEND_TO_BOTTOM);
            AddMenuItem(MenuItemId.SC_SAVE_SCREEN_SHOT);
            AddMenuItem(MenuItemId.SC_OPEN_FILE_IN_EXPLORER);
            AddMenuItem(MenuItemId.SC_DRAG_BY_MOUSE);

            StartCreatingSubMenu();
            AddSubMenuItem(MenuItemId.SC_SIZE_640_480, "640x480");
            AddSubMenuItem(MenuItemId.SC_SIZE_720_480, "720x480");
            AddSubMenuItem(MenuItemId.SC_SIZE_720_576, "720x576");
            AddSubMenuItem(MenuItemId.SC_SIZE_720_576, "720x576");
            AddSubMenuItem(MenuItemId.SC_SIZE_800_600, "800x600");
            AddSubMenuItem(MenuItemId.SC_SIZE_1024_768, "1024x768");
            AddSubMenuItem(MenuItemId.SC_SIZE_1152_864, "1152x846");
            AddSubMenuItem(MenuItemId.SC_SIZE_1280_768, "1280x768");
            AddSubMenuItem(MenuItemId.SC_SIZE_1280_800, "1280x800");
            AddSubMenuItem(MenuItemId.SC_SIZE_1280_960, "1280x960");
            AddSubMenuItem(MenuItemId.SC_SIZE_1280_1024, "1280x1024");
            AddSubMenuItem(MenuItemId.SC_SIZE_1440_900, "1440x900");
            AddSubMenuItem(MenuItemId.SC_SIZE_1600_900, "1600x900");
            AddSubMenuItem(MenuItemId.SC_SIZE_1680_1050, "1680x1050");
            if (_menuItems.WindowSizeItems.Any())
            {
                AddSubMenuSeparator();
                for (int i = 0; i < _menuItems.WindowSizeItems.Count; i++)
                {
                    var menuItemId = MenuItemId.SC_SIZE_DEFINED + i;
                    _menuItems.WindowSizeItems[i].Id = menuItemId;
                    NativeMethods.InsertMenu(subMenuHandle, -1, NativeConstants.MF_BYPOSITION, menuItemId, _menuItems.WindowSizeItems[i].Title);
                }
            }
            AddSubMenuSeparator();
            AddSubMenuItem(MenuItemId.SC_SIZE_DEFAULT);
            AddSubMenuSeparator();
            AddSubMenuItem(MenuItemId.SC_SIZE_CUSTOM);
            FinishSubMenu("size");

            StartCreatingSubMenu();
            foreach (var item in MoveToMenuItems)
            {
                NativeMethods.InsertMenu(subMenuHandle, -1, NativeConstants.MF_BYPOSITION, MenuItemId.SC_MOVE_TO + item.Key, GetTitle("monitor") + item.Key);
            }
            FinishSubMenu("move_to");

            StartCreatingSubMenu();
            AddSubMenuItem(MenuItemId.SC_ALIGN_TOP_LEFT);
            AddSubMenuItem(MenuItemId.SC_ALIGN_TOP_CENTER);
            AddSubMenuItem(MenuItemId.SC_ALIGN_TOP_RIGHT);
            AddSubMenuItem(MenuItemId.SC_ALIGN_MIDDLE_LEFT);
            AddSubMenuItem(MenuItemId.SC_ALIGN_MIDDLE_CENTER);
            AddSubMenuItem(MenuItemId.SC_ALIGN_MIDDLE_RIGHT);
            AddSubMenuItem(MenuItemId.SC_ALIGN_BOTTOM_LEFT);
            AddSubMenuItem(MenuItemId.SC_ALIGN_BOTTOM_CENTER);
            AddSubMenuItem(MenuItemId.SC_ALIGN_BOTTOM_RIGHT);
            AddSubMenuSeparator();
            AddSubMenuItem(MenuItemId.SC_ALIGN_DEFAULT);
            AddSubMenuSeparator();
            AddSubMenuItem(MenuItemId.SC_ALIGN_CUSTOM);
            FinishSubMenu("alignment");

            StartCreatingSubMenu();
            AddSubMenuItem(MenuItemId.SC_TRANS_00, "0%" + GetTitle("trans_opaque", null, false));
            AddSubMenuItem(MenuItemId.SC_TRANS_10, "10%");
            AddSubMenuItem(MenuItemId.SC_TRANS_20, "20%");
            AddSubMenuItem(MenuItemId.SC_TRANS_30, "30%");
            AddSubMenuItem(MenuItemId.SC_TRANS_40, "40%");
            AddSubMenuItem(MenuItemId.SC_TRANS_50, "50%");
            AddSubMenuItem(MenuItemId.SC_TRANS_60, "60%");
            AddSubMenuItem(MenuItemId.SC_TRANS_70, "70%");
            AddSubMenuItem(MenuItemId.SC_TRANS_80, "80%");
            AddSubMenuItem(MenuItemId.SC_TRANS_90, "90%");
            AddSubMenuItem(MenuItemId.SC_TRANS_100, "100%" + GetTitle("trans_invisible", null, false));
            AddSubMenuSeparator();
            AddSubMenuItem(MenuItemId.SC_TRANS_DEFAULT);
            AddSubMenuSeparator();
            AddSubMenuItem(MenuItemId.SC_TRANS_CUSTOM);
            FinishSubMenu("transparency");

            StartCreatingSubMenu();
            AddSubMenuItem(MenuItemId.SC_PRIORITY_REAL_TIME);
            AddSubMenuItem(MenuItemId.SC_PRIORITY_HIGH);
            AddSubMenuItem(MenuItemId.SC_PRIORITY_ABOVE_NORMAL);
            AddSubMenuItem(MenuItemId.SC_PRIORITY_NORMAL);
            AddSubMenuItem(MenuItemId.SC_PRIORITY_BELOW_NORMAL);
            AddSubMenuItem(MenuItemId.SC_PRIORITY_IDLE);
            FinishSubMenu("priority");

            StartCreatingSubMenu();
            AddSubMenuItem(MenuItemId.SC_COPY_TEXT_TO_CLIPBOARD);
            AddSubMenuItem(MenuItemId.SC_CLEAR_CLIPBOARD);
            FinishSubMenu("clipboard");

            StartCreatingSubMenu();
            AddSubMenuItem(MenuItemId.SC_MINIMIZE_TO_SYSTEMTRAY);
            AddSubMenuItem(MenuItemId.SC_MINIMIZE_ALWAYS_TO_SYSTEMTRAY);
            AddSubMenuItem(MenuItemId.SC_SUSPEND_TO_SYSTEMTRAY);
            FinishSubMenu("system_tray");

            StartCreatingSubMenu();
            AddSubMenuItem(MenuItemId.SC_MINIMIZE_OTHER_WINDOWS);
            AddSubMenuItem(MenuItemId.SC_CLOSE_OTHER_WINDOWS);
            FinishSubMenu("other_windows");

            StartCreatingSubMenu();
            for (int i = 0; i < _menuItems.StartProgramItems.Count; i++)
            {
                NativeMethods.InsertMenu(subMenuHandle, -1, NativeConstants.MF_BYPOSITION, MenuItemId.SC_START_PROGRAM + i, _menuItems.StartProgramItems[i].Title);
            }
            FinishSubMenu("start_program");


            void StartCreatingSubMenu()
            {
                subMenuHandle = NativeMethods.CreateMenu();
            }

            void AddMenuItem(int id, string title = null, bool showHotkey = true)
            {
                string itemName = MenuItemId.GetName(id);

                if (_menuItems.Items.Any(x => x.Name == itemName && x.Show))
                {
                    _numberItems++;
                    NativeMethods.InsertMenu(windowMenuHandle, ++index, NativeConstants.MF_BYPOSITION, id, GetTitle(itemName, title, showHotkey));
                }
            }

            void AddSubMenuSeparator()
            {
                NativeMethods.InsertMenu(subMenuHandle, -1, NativeConstants.MF_BYPOSITION | NativeConstants.MF_SEPARATOR, 0, "");
            }

            void AddSubMenuItem(int id, string title = null, bool showHotkey = true)
            {
                string itemName = MenuItemId.GetName(id);

                if (_menuItems.Items.Any(x => x.Name == itemName && x.Show))
                {
                    NativeMethods.InsertMenu(subMenuHandle, -1, NativeConstants.MF_BYPOSITION, id, GetTitle(itemName, title, showHotkey));
                }
            }

            void FinishSubMenu(string itemName, string title = null, bool showHotkey = true)
            {
                if (_menuItems.Items.Any(x => x.Name == itemName && x.Show))
                {
                    _numberItems++;
                    NativeMethods.InsertMenu(windowMenuHandle, ++index, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, subMenuHandle,
                                             GetTitle(itemName, title, showHotkey));
                }
                _subMenuHandles.Add(subMenuHandle);
                subMenuHandle = IntPtr.Zero;
            }
        }