Metro style App ContextMenu Summary

本文提供了在UWP应用中实现右键菜单及文本选择功能的代码示例,包括获取元素边界、显示上下文菜单及复制选定文本等实用功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Metro style App ContextMenu Summary。

Fist let us see the effect pictures。

Picture 1.

Picture 2.

 

Get the frameworkElement Rect。

public static Rect GetElementRect(FrameworkElement element)
{
    GeneralTransform buttonTransform = element.TransformToVisual(null);
    Point point = buttonTransform.TransformPoint(new Point());
    return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));
}

 

Next is a image righttap event。

private async void AttachmentImage_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
    var menu = new PopupMenu();
    menu.Commands.Add(new UICommand("Open with", (command) =>
    {
        //ToDo:function
    }));
    menu.Commands.Add(new UICommand("Save attachment", (command) =>
    {
        //ToDo:function
    }));
 
    var chosenCommand = await menu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));
    if (chosenCommand == null)
    {
        // The command is null if no command was invoked.
        //ToDo:function
    }
}

 

 

 2.Another sample

 returns a rect for selected text

// returns a rect for selected text
private Rect GetTextboxSelectionRect(TextBox textbox)
{
    Rect rectFirst, rectLast;
    if (textbox.SelectionStart == textbox.Text.Length)
    {
        rectFirst = textbox.GetRectFromCharacterIndex(textbox.SelectionStart - 1, true);
    }
    else
    {
        rectFirst = textbox.GetRectFromCharacterIndex(textbox.SelectionStart, false);
    }
 
    int lastIndex = textbox.SelectionStart + textbox.SelectionLength;
    if (lastIndex == textbox.Text.Length)
    {
        rectLast = textbox.GetRectFromCharacterIndex(lastIndex - 1, true);
    }
    else
    {
        rectLast = textbox.GetRectFromCharacterIndex(lastIndex, false);
    }
 
    GeneralTransform buttonTransform = textbox.TransformToVisual(null);
    Point point = buttonTransform.TransformPoint(new Point());
 
    return new Rect(point.X + rectFirst.Left,
        point.Y + rectFirst.Top,
        rectLast.Right - rectFirst.Left,
        rectLast.Bottom - rectFirst.Top);
}

 

Next is a TextBox ContextMenuOpening event.

private async void ReadOnlyTextBox_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
    e.Handled = true;
    TextBox textbox = (TextBox)sender;
    if (textbox.SelectionLength > 0)
    {
        var menu = new PopupMenu();
        menu.Commands.Add(new UICommand("Copy", null, 1));
        menu.Commands.Add(new UICommandSeparator());
        menu.Commands.Add(new UICommand("Highlight", null, 2));
        menu.Commands.Add(new UICommand("Look up", null, 3));
 
        Rect rect = GetTextboxSelectionRect(textbox);
        var chosenCommand = await menu.ShowForSelectionAsync(rect);
        if (chosenCommand != null)
        {
            switch ((int)chosenCommand.Id)
            {
                case 1:
                    //Next is copy function
                    String selectedText = ((TextBox)sender).SelectedText;
                    var dataPackage = new DataPackage();
                    dataPackage.SetText(selectedText);
                    Clipboard.SetContent(dataPackage);
                    break;
 
                case 2:
                    //Todo:function
                    break;
 
                case 3:
                    //Todo:function
                    break;
            }
        }
        else
        {
            //Todo:function
        }
    }
    else
    {
        //Todo:function
    }
}

 

The source sample is from msdn.



本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2012/08/01/2618865.html,如需转载请自行联系原作者

代码介绍 MetroForWinForm(win8风格模版) using System; using System.Drawing; using System.Globalization; using System.Windows.Forms; using MetroFramework.Forms; namespace MetroFramework.Demo { public partial class MainForm : MetroForm { public MainForm() { InitializeComponent(); metroStyleManager.Theme = MetroThemeStyle.Default; metroStyleManager.Style = MetroColorStyle.Teal; } private void metroTileSwitch_Click(object sender, EventArgs e) { var m = new Random(); int next = m.Next(0, 13); metroStyleManager.Style = (MetroColorStyle)next; } private void metroTile1_Click(object sender, EventArgs e) { metroStyleManager.Theme = metroStyleManager.Theme == MetroThemeStyle.Light ? MetroThemeStyle.Dark : MetroThemeStyle.Light; } private void metroButton1_Click(object sender, EventArgs e) { MetroTaskWindow.ShowTaskWindow(this, "SubControl in TaskWindow", new TaskWindowControl(), 10); } private void metroButton2_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "Do you like this metro message box?", "Metro Title", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Asterisk); } private void metroButton5_Click(object sender, EventArgs e) { metroContextMenu1.Show(metroButton5, new Point(0, metroButton5.Height)); } private void metroButton6_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `OK` only button", "MetroMessagebox", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void metroButton10_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `OK` and `Cancel` button", "MetroMessagebox", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); } private void metroButton7_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Yes` and `No` button", "MetroMessagebox", MessageBoxButtons.YesNo, MessageBoxIcon.Question); } private void metroButton8_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Yes`, `No` and `Cancel` button", "MetroMessagebox", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); } private void metroButton11_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Retry` and `Cancel` button. With warning style.", "MetroMessagebox", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning); } private void metroButton9_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Abort`, `Retry` and `Ignore` button. With Error style.", "MetroMessagebox", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error); } private void metroButton12_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample `default` MetroMessagebox ", "MetroMessagebox"); } private void metroButton4_Click(object sender, EventArgs e) { var testform = new TestForm1(); testform.ShowDialog(); } private void metroButton4_Click_1(object sender, EventArgs e) { metroTextBox2.Focus(); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值