Windows 8開發入門(十六) Windows 8的右鍵菜單

來源:互聯網
上載者:User

在Windows 8中的控制項中有TextBox等輸入控制項的ContextMenuOpening事件和Button等非輸入控制項的 RightTapped事件。

本文中將講述者兩個事件的用法。這兩個事件的

PopupMenu是右鍵快顯功能表 的具體類。

首先我們看具體菜單類的執行個體化和擷取項目代碼

/// <summary>        /// 設定右鍵點擊點擊具體處理細節        /// </summary>        /// <param name="sender"></param>        public async void SetRightClick(object sender)        {            //增加菜單            PopupMenu menu = new PopupMenu();            menu.Commands.Add(new UICommand("複製0", null, 0));            menu.Commands.Add(new UICommand("剪下1", null, 1));            menu.Commands.Add(new UICommand("粘貼2", null, 2));            menu.Commands.Add(new UICommandSeparator());            menu.Commands.Add(new UICommand("Full Screen", null, 3));            menu.Commands.Add(new UICommand("Snap Screen", null, 4));            //擷取選擇的功能表項目            var cmd = await menu.ShowForSelectionAsync(GetRectPosition((FrameworkElement)sender));            if (cmd != null)            {                switch ((int)cmd.Id)                {                    case 0:                        tbText.Text = "選擇了(" + cmd.Label + ") ,其ID為" + cmd.Id;                        break;                    case 1:                        tbText.Text = "選擇了(" + cmd.Label + "),其ID為" + cmd.Id;                        break;                    case 2:                        tbText.Text = "選擇了(" + cmd.Label + ") ,其ID為" + cmd.Id;                        break;                    case 3:                        tbText.Text = "選擇了(" + cmd.Label + ") ,其ID為" + cmd.Id;                        break;                    case 4:                        tbText.Text = "選擇了(" + cmd.Label + ") ,其ID為" + cmd.Id;                        break;                }            }            else            {                tbText.Text = "操作功能表";            }        }        //擷取菜單位置        Rect GetRectPosition(FrameworkElement element)        {            GeneralTransform btnform = element.TransformToVisual(null);            Point point = btnform.TransformPoint(new Point());            return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.