예제 #1
0
        public ContextMenu(IEnumerable <MenuItem> items, bool visible = true)
            : base(null, null, visible)
        {
            BorderSize = 1;
            var menuItems    = items as MenuItem[] ?? items.ToArray();
            var maxX         = (int)menuItems.Max(m => _font.MeasureString(m.Text).X);
            var yPerItem     = ListBox <MenuItem> .ItemHeight;
            var visibleItems = Math.Min(MaxItems, menuItems.Length);
            var rect         = new Rectangle(0, 0, maxX + Margin * 2 + Scroll.Width, visibleItems * yPerItem + Margin * 2);

            SetRect(rect);
            _listBox = new ListBox <MenuItem>(null, null, rect, false, 1);
            _listBox.AddRange(menuItems, item => item.Text);
            _listBox.OnSelect = item =>
            {
                if (item.OnClick != null)
                {
                    Shortcuts.SetToolSelect();
                    Close();
                }
                _listBox.Unselect();
                item.OnClick?.Invoke();
            };
        }