Exemple #1
0
        public YamuiMenu(Point location, List <YamuiMenuItem> content, string htmlTitle = null, int minSize = 150)
        {
            if (content == null || content.Count == 0)
            {
                content = new List <YamuiMenuItem> {
                    new YamuiMenuItem {
                        ItemName = "Empty", IsDisabled = true
                    }
                }
            }
            ;

            // init menu form
            SetStyle(
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.ResizeRedraw |
                ControlStyles.UserPaint |
                ControlStyles.AllPaintingInWmPaint, true);
            ControlBox      = false;
            FormBorderStyle = FormBorderStyle.None;
            MaximizeBox     = false;
            MinimizeBox     = false;
            ShowIcon        = false;
            ShowInTaskbar   = false;
            SizeGripStyle   = SizeGripStyle.Hide;
            StartPosition   = FormStartPosition.Manual;

            var useImageIcon = content.Exists(item => item.ItemImage != null);
            var noChildren   = !content.Exists(item => item.Children != null);
            var maxWidth     = content.Select(item => TextRenderer.MeasureText(item.SubText, FontManager.GetFont(FontFunction.Small)).Width).Concat(new[] { 0 }).Max();

            maxWidth += maxWidth == 0 ? 0 : 15;
            maxWidth += content.Select(item => TextRenderer.MeasureText(item.ItemName, FontManager.GetStandardFont()).Width).Concat(new[] { 0 }).Max();
            maxWidth += (useImageIcon ? 35 : 8) + 12 + (noChildren ? 0 : 12);
            maxWidth  = Math.Max(minSize, maxWidth);

            int yPos = BorderWidth;

            // title
            HtmlLabel title = null;

            if (htmlTitle != null)
            {
                title = new HtmlLabel {
                    AutoSizeHeightOnly = true,
                    BackColor          = Color.Transparent,
                    Width                = maxWidth - BorderWidth * 2,
                    Text                 = htmlTitle,
                    Location             = new Point(BorderWidth, BorderWidth),
                    IsSelectionEnabled   = false,
                    IsContextMenuEnabled = false,
                    Enabled              = false
                };
                yPos += title.Height;
            }

            // insert buttons
            int  index = 0;
            bool lastButtonWasDisabled = true;

            Controls.Clear();
            foreach (var item in content)
            {
                if (item.IsSeparator)
                {
                    _yPosOfSeparators.Add(yPos);
                    yPos += SeparatorLineHeight;
                }
                else
                {
                    var button = new YamuiMenuButton {
                        Text           = item.ItemName,
                        NoChildren     = item.Children == null || !item.Children.Any(),
                        Location       = new Point(BorderWidth, yPos),
                        Size           = new Size(maxWidth - BorderWidth * 2, LineHeight),
                        NoIconImage    = !useImageIcon,
                        BackGrndImage  = item.ItemImage,
                        SubText        = item.SubText,
                        Tag            = index,
                        SubTextOpacity = SubTextOpacity,
                        Enabled        = !item.IsDisabled
                    };
                    button.Click          += ButtonOnButtonPressed;
                    button.PreviewKeyDown += OnPreviewKeyDown;
                    Controls.Add(button);
                    _content.Add(item);
                    yPos += LineHeight;

                    // allows to select the correct button at start up
                    if (item.IsSelectedByDefault || lastButtonWasDisabled)
                    {
                        _selectedIndex = index;
                    }
                    if (lastButtonWasDisabled)
                    {
                        lastButtonWasDisabled = item.IsDisabled;
                    }
                    index++;
                }
            }

            // add title if needed
            if (title != null)
            {
                Controls.Add(title);
            }

            // Size the form
            Size        = new Size(maxWidth, yPos + BorderWidth);
            MinimumSize = Size;
            MaximumSize = Size;

            // menu position
            var screen = Screen.FromPoint(location);

            if (location.X > screen.WorkingArea.X + screen.WorkingArea.Width / 2)
            {
                location.X = location.X - Width;
                _reverseX  = true;
            }
            if (location.Y > screen.WorkingArea.Y + screen.WorkingArea.Height / 2)
            {
                location.Y = location.Y - Height;
                _reverseY  = true;
            }
            Location = location;

            // events
            Deactivate += OnDeactivate;
            Activated  += OnActivated;
            Closing    += OnClosing;

            // keydown
            KeyPreview      = true;
            PreviewKeyDown += OnPreviewKeyDown;

            // set focused item
            ActiveControl = Controls[_selectedIndex];

            // register to the opened menu list
            if (ListOfOpenededMenuHandle == null)
            {
                ListOfOpenededMenuHandle = new List <IntPtr>();
            }
            ListOfOpenededMenuHandle.Add(Handle);
        }
Exemple #2
0
        public YamuiWaterfallMenu(Point location, List <YamuiMenuItem> content, string htmlTitle = null, int minSize = 150)
        {
            if (content == null || content.Count == 0)
            {
                content = new List <YamuiMenuItem> {
                    new YamuiMenuItem {
                        DisplayText = "Empty", IsDisabled = true
                    }
                }
            }
            ;

            // init menu form
            ShowInTaskbar = false;
            StartPosition = FormStartPosition.Manual;

            var useImageIcon = content.Exists(item => item.ItemImage != null);
            var noChildren   = !content.Exists(item => item.Children != null);
            var maxWidth     = content.Select(item => TextRenderer.MeasureText(item.SubText, FontManager.GetFont(FontFunction.Small)).Width).Concat(new[] { 0 }).Max();

            maxWidth += maxWidth == 0 ? 0 : 15;
            maxWidth += content.Select(item => TextRenderer.MeasureText(item.DisplayText, FontManager.GetStandardFont()).Width).Concat(new[] { 0 }).Max();
            maxWidth += (useImageIcon ? 35 : 8) + 12 + (noChildren ? 0 : 12);
            maxWidth  = Math.Max(minSize, maxWidth);

            int yPos = BorderWidth;

            // title
            HtmlLabel title = null;

            if (htmlTitle != null)
            {
                title = new HtmlLabel {
                    AutoSizeHeightOnly = true,
                    BackColor          = Color.Transparent,
                    Width                = maxWidth - BorderWidth * 2,
                    Text                 = htmlTitle,
                    Location             = new Point(BorderWidth, BorderWidth),
                    IsSelectionEnabled   = false,
                    IsContextMenuEnabled = false,
                    Enabled              = false
                };
                yPos += title.Height;
            }

            // insert buttons
            int  index = 0;
            bool lastButtonWasDisabled = true;

            Controls.Clear();
            foreach (var item in content)
            {
                if (item.IsSeparator)
                {
                    _yPosOfSeparators.Add(yPos);
                    yPos += SeparatorLineHeight;
                }
                else
                {
                    var button = new YamuiMenuButton {
                        Text           = item.DisplayText,
                        NoChildren     = item.Children == null || !item.Children.Any(),
                        Location       = new Point(BorderWidth, yPos),
                        Size           = new Size(maxWidth - BorderWidth * 2, LineHeight),
                        NoIconImage    = !useImageIcon,
                        BackGrndImage  = item.ItemImage,
                        SubText        = item.SubText,
                        Tag            = index,
                        SubTextOpacity = SubTextOpacity,
                        Enabled        = !item.IsDisabled
                    };
                    button.Click   += ButtonOnPressed;
                    button.KeyDown += ButtonOnKeyDown;
                    Controls.Add(button);
                    _content.Add(item);
                    yPos += LineHeight;

                    // allows to select the correct button at start up
                    if (item.IsSelectedByDefault || lastButtonWasDisabled)
                    {
                        _selectedIndex = index;
                    }
                    if (lastButtonWasDisabled)
                    {
                        lastButtonWasDisabled = item.IsDisabled;
                    }
                    index++;
                }
            }

            // add title if needed
            if (title != null)
            {
                Controls.Add(title);
            }

            // Size the form
            Size        = new Size(maxWidth, yPos + BorderWidth);
            MinimumSize = Size;
            MaximumSize = Size;
            Resizable   = false;

            // menu position
            Location = GetBestMenuPosition(location);

            // set focused item
            ActiveControl = Controls[_selectedIndex];

            // register to the opened menu list
            if (ListOfOpenededMenuHandle == null)
            {
                ListOfOpenededMenuHandle = new List <IntPtr>();
            }
            ListOfOpenededMenuHandle.Add(Handle);

            // So that the OnKeyDown event of this form is executed before the HandleKeyDown event of the control focused
            KeyPreview = true;
        }