RichEditBox 使用自定义菜单

最后更新于:2022-04-01 20:23:07

老周:当RichEditBox控件的上下文菜单即将弹出时,会引发ContextMenuOpening事件,我们需要处理该事件,并且将e.Handled属性设置为true,这样才能阻止默认上下文菜单的弹出 在RichEditBox控件上声明附加的菜单项 ~~~ ~~~ 处理OnContextMenuOpening ~~~ private void OnContextMenuOpening(object sender , ContextMenuEventArgs e) { //阻止弹出默认的上下文菜单,然后,调用ShowAt方法在指定的坐标处打开菜单 e.Handled = true; MenuFlyout menu = FlyoutBase.GetAttachedFlyout(redit) as MenuFlyout; menu?.ShowAt(redit , new Point(e.CursorLeft , e.CursorTop)); } ~~~ 处理复制粘贴 ~~~ private void OnCopy(object sender , RoutedEventArgs e) { //复制 redit.Document.Selection.Copy(); } private void OnCut(object sender , RoutedEventArgs e) { //剪切 redit.Document.Selection.Cut(); } private void OnPaste(object sender , RoutedEventArgs e) { //粘贴 redit.Document.Selection.Paste(0); //Paste 要在粘贴操作中使用的剪贴板格式。零表示最佳格式 } ~~~ 处理OnFontSize ~~~ /// /// 设置字体 /// /// /// private void OnFontSize(object sender , RoutedEventArgs e) { MenuFlyoutItem item = sender as MenuFlyoutItem; // 获取字号 float size = Convert.ToSingle(item.Tag); redit.Document.Selection.CharacterFormat.Size = size; } ~~~ ~~~ /// /// 加粗 /// /// /// private void OnBold(object sender , RoutedEventArgs e) { //using Windows.UI.Text; ToggleMenuFlyoutItem item = sender as ToggleMenuFlyoutItem; redit.Document.Selection.CharacterFormat.Bold = item.IsChecked ? FormatEffect.On : FormatEffect.Off; } private void OnUnderline(object sender , RoutedEventArgs e) { MenuFlyoutItem item = sender as MenuFlyoutItem; int x = Convert.ToInt32(item.Tag); UnderlineType unlinetp; switch (x) { case -1: // 无 unlinetp = UnderlineType.None; break; case 0: // 单实线 unlinetp = UnderlineType.Single; break; case 1: // 双实线 unlinetp = UnderlineType.Double; break; case 2: // 虚线 unlinetp = UnderlineType.Dash; break; default: unlinetp = UnderlineType.None; break; } redit.Document.Selection.CharacterFormat.Underline = unlinetp; } ~~~ ~~~ private void OnTinct(object sender , RoutedEventArgs e) { MenuFlyoutItem item = sender as MenuFlyoutItem; string tinct = item.Tag as string; Windows.UI.Color color = new Windows.UI.Color(); switch (tinct) { case "黑色": color= Windows.UI.Colors.Black; break; case "蓝色": color = Windows.UI.Colors.Blue; break; case "白色": color = Windows.UI.Colors.White; break; default: break; } redit.Document.Selection.CharacterFormat.BackgroundColor = color; } ~~~ 颜色在Windows.UI.Color 里面代码都是抄老周的 ![这里写图片描述](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-04-08_5707636b8545f.jpg "") 参考:[http://www.cnblogs.com/tcjiaan/p/4937301.html](http://www.cnblogs.com/tcjiaan/p/4937301.html)
';