コード例 #1
0
ファイル: ContextMenu.cs プロジェクト: migueldeicaza/gui.cs
        /// <summary>
        /// Open the <see cref="MenuItems"/> menu items.
        /// </summary>
        public void Show()
        {
            if (menuBar != null)
            {
                Hide();
            }
            container          = Application.Current;
            container.Closing += Container_Closing;
            container.Resized += Container_Resized;
            var frame    = container.Frame;
            var position = Position;

            if (Host != null)
            {
                Host.ViewToScreen(container.Frame.X, container.Frame.Y, out int x, out int y);
                var pos = new Point(x, y);
                pos.Y += Host.Frame.Height - 1;
                if (position != pos)
                {
                    Position = position = pos;
                }
            }
            var rect = Menu.MakeFrame(position.X, position.Y, MenuItems.Children);

            if (rect.Right >= frame.Right)
            {
                if (frame.Right - rect.Width >= 0 || !ForceMinimumPosToZero)
                {
                    position.X = frame.Right - rect.Width;
                }
                else if (ForceMinimumPosToZero)
                {
                    position.X = 0;
                }
            }
            else if (ForceMinimumPosToZero && position.X < 0)
            {
                position.X = 0;
            }
            if (rect.Bottom >= frame.Bottom)
            {
                if (frame.Bottom - rect.Height - 1 >= 0 || !ForceMinimumPosToZero)
                {
                    if (Host == null)
                    {
                        position.Y = frame.Bottom - rect.Height - 1;
                    }
                    else
                    {
                        Host.ViewToScreen(container.Frame.X, container.Frame.Y, out int x, out int y);
                        var pos = new Point(x, y);
                        position.Y = pos.Y - rect.Height - 1;
                    }
                }
                else if (ForceMinimumPosToZero)
                {
                    position.Y = 0;
                }
            }
            else if (ForceMinimumPosToZero && position.Y < 0)
            {
                position.Y = 0;
            }

            menuBar = new MenuBar(new [] { MenuItems })
            {
                X      = position.X,
                Y      = position.Y,
                Width  = 0,
                Height = 0,
                UseSubMenusSingleFrame = UseSubMenusSingleFrame
            };

            menuBar.isContextMenuLoading = true;
            menuBar.MenuAllClosed       += MenuBar_MenuAllClosed;
            IsShow = true;
            menuBar.OpenMenu();
        }