实例介绍
WPF TextBox如果行数过多,比如1、2千行,这时候按下ctrl-a进行全选(卡顿很慢),再点击右键,这时候右键菜单要么打开迟缓,要么不停闪烁,点击菜单项后,不会执行相应的菜单命令。
根据这个情况,重建一个菜单,虽然ctrl-a进行全选还很慢,但菜单显示及执行功能不受影响了。组件要添加到根部的canvas里面。
在vs2017编译后不出现异常,vs2019有几率出现粘贴板错误,自行百度修正。
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
public class TextBoxHelper
{
public Color BackgroundColor = Colors.White; // 背景色
public Color TextColor = Colors.Black; // 文字颜色
public Color LinecodeBackground = Colors.Gainsboro; // 显示行号背景色
public Color LinecodeColor = Colors.SteelBlue; // 显示行号文字色
public FontFamily fontFamily = new FontFamily("Microsoft YaHei"); // 字体
public double Fontsize = 12; // 字体大小
public double Line_Height = 20; // 行高度
public double Linecode_Width = 35; // 行号宽度
public double Linecode_Interval = 5; // 行号右侧间隔
public TextBox Editor; // TextBox组件
public EditMenu editMenu = new EditMenu();
private ScrollTemplate scrollTemplate_TextBox;
private Canvas Editor_Box;
private Canvas Linenumber;
private double per_height = 0;
private Rect control_rect;
/// <summary>
/// 带行号的TextBox
/// </summary>
/// <param name="obj">父容器</param>
/// <param name="x">位置 x</param>
/// <param name="y">位置 y</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
public void LinenumberEditor(Canvas obj, double x, double y, double width, double height)
{
editMenu = new EditMenu()
{
Visibility = Visibility.Hidden
};
Panel.SetZIndex(editMenu,999);
obj.Children.Add(editMenu);
per_height = height;
Editor_Box = new Canvas()
{
Width = width Linecode_Width,
Height = height,
Background = new SolidColorBrush(BackgroundColor),
Clip = new RectangleGeometry(new Rect(0, 0, width, height))
};
Editor_Box.PreviewMouseDown = Editor_PreviewMouseDown;
Canvas.SetLeft(Editor_Box, x);
Canvas.SetTop(Editor_Box, y);
Editor = new TextBox()
{
BorderThickness = new Thickness(0),
ContextMenu = null,
AcceptsReturn = true,
FontFamily = fontFamily,
FontSize = Fontsize,
AcceptsTab = true,
Foreground = new SolidColorBrush(TextColor),
Background =Brushes.Transparent,
};
Editor.PreviewKeyDown = Editor_PreviewKeyDown;
Editor.TextChanged = Editor_TextChanged;
Editor.SelectionChanged = Editor_SelectionChanged;
Editor.LostFocus = Editor_LostFocus;
TextBlock.SetLineHeight(Editor, Line_Height);
Canvas.SetLeft(Editor, 30);
WrapPanel text_panel = new WrapPanel()
{
Orientation = Orientation.Vertical,
};
text_panel.Children.Add(Editor);
scrollTemplate_TextBox = new ScrollTemplate();
scrollTemplate_TextBox.ScrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
scrollTemplate_TextBox.ScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
scrollTemplate_TextBox.ScrollViewer.Background = Brushes.WhiteSmoke;
scrollTemplate_TextBox.InitializeScrollViewer(Editor_Box, text_panel, Linecode_Width, 0, width- Linecode_Width, height, ScrollViewer_Type.Effect);
scrollTemplate_TextBox.ScrollViewer.ScrollChanged = (s, e) => {
GeneralTransform generalTransform = Editor.TransformToAncestor(Editor_Box);
Point point = generalTransform.Transform(new Point(0, 0));
Canvas.SetTop(Linenumber, point.Y);
};
scrollTemplate_TextBox.ScrollViewer.PreviewMouseDown = (s, e) => {
obj.Dispatcher.BeginInvoke(DispatcherPriority.Background,
(Action)(() => { Keyboard.Focus(Editor); }));
};
Linenumber = new Canvas()
{
Width = Linecode_Width,
Height = height,
Background = new SolidColorBrush(LinecodeBackground)
};
Editor_Box.Children.Add(Linenumber);
TextBlock Linecode = new TextBlock()
{
Height = Line_Height,
Width = Linecode_Width - Linecode_Interval,
TextAlignment = TextAlignment.Right,
Text = "1",
FontFamily = fontFamily,
FontSize = Fontsize,
Foreground = new SolidColorBrush(LinecodeColor),
Background = Brushes.Transparent
};
Linenumber.Children.Add(Linecode);
Editor_Box.MouseDown = (s, e) => { Editor.Focus(); };
obj.Children.Add(Editor_Box);
(obj as FrameworkElement).PreviewMouseDown = TextBoxHelper_PreviewMouseDown;
editMenu.IsVisibleChanged = EditMenu_IsVisibleChanged;
control_rect = new Rect(Canvas.GetLeft(Editor_Box), Canvas.GetTop(Editor_Box), Editor_Box.Width - Linecode_Width, Editor_Box.Height);
}
private void Editor_LostFocus(object sender, RoutedEventArgs e)
{
Set_TextBoxDefrag();
}
private void TextBoxHelper_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement pL = (FrameworkElement)sender;
Point point = Mouse.GetPosition(pL);
if (!control_rect.Contains(point))
{
if (editMenu.ShowMenu)
{
editMenu.Visibility = Visibility.Hidden;
editMenu.ShowMenu = false;
}
Editor.Select(Editor.SelectionStart, 0);
}
}
private void EditMenu_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (editMenu.IsVisible)
{
editMenu.Option = -1;
Set_Menu();
}
else
{
if (editMenu.Option==0) // cut
{
Clipboard.Clear();
Clipboard.SetDataObject(Editor.SelectedText);
Editor.SelectedText = "";
}
else
if (editMenu.Option == 1) // copy
{
Clipboard.Clear();
Clipboard.SetDataObject(Editor.SelectedText);
}
else
if (editMenu.Option == 2) // past
{
if (Editor.SelectionLength > 0)
{
Editor.SelectedText = Clipboard.GetText();
}
else
{
Editor.Select(Editor.SelectionStart, 0);
Editor.SelectedText = Clipboard.GetText();
}
}
else
if (editMenu.Option == 3) // select all
{
Editor.SelectAll();
}
else
if (editMenu.Option == 4) // clear
{
Editor.SelectedText = "";
}
}
}
private void Editor_SelectionChanged(object sender, RoutedEventArgs e)
{
Set_Menu();
}
private void Editor_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
if (editMenu.ShowMenu)
{
editMenu.Visibility = Visibility.Hidden;
editMenu.ShowMenu = false;
}
Editor.Select(Editor.SelectionStart, 0);
}
else
if (e.RightButton == MouseButtonState.Pressed)
{
Set_Menu();
Point point = Mouse.GetPosition(Editor_Box);
double x = point.X 210 > Editor_Box.Width ? (point.X - 150) : point.X 25;
double y = point.Y 120 > Editor_Box.Height ? (point.Y - 110) : point.Y 20;
if (!editMenu.ShowMenu)
{
Canvas.SetLeft(editMenu,x);
Canvas.SetTop(editMenu,y);
editMenu.Visibility = Visibility.Visible;
editMenu.ShowMenu = true;
}
else
{
Canvas.SetLeft(editMenu, x);
Canvas.SetTop(editMenu, y);
}
}
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
MenuItem pL = (MenuItem)sender;
if ((int)pL.Tag == 0)
{
Clipboard.Clear();
Clipboard.SetText(Editor.SelectedText);
Editor.SelectedText = "";
}
else
if ((int)pL.Tag == 1)
{
Clipboard.SetText(Editor.SelectedText);
}
if ((int)pL.Tag == 2)
{
if (Editor.SelectionLength > 0)
{
Editor.SelectedText = Clipboard.GetText();
}
else
{
Editor.Select(Editor.SelectionStart, 0);
Editor.SelectedText = Clipboard.GetText();
}
}
else
if ((int)pL.Tag == 3)
{
Editor.SelectAll();
}
else
if ((int)pL.Tag == 4)
{
Editor.SelectedText = "";
}
}
private void Editor_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (editMenu.ShowMenu)
{
editMenu.Visibility = Visibility.Hidden;
editMenu.ShowMenu = false;
}
if (e.Key==Key.PageDown)
{
e.Handled = true;
scrollTemplate_TextBox.ScrollViewer.ScrollToVerticalOffset(scrollTemplate_TextBox.ScrollViewer.VerticalOffset per_height);
}
else
if (e.Key == Key.PageUp)
{
e.Handled = true;
scrollTemplate_TextBox.ScrollViewer.ScrollToVerticalOffset(scrollTemplate_TextBox.ScrollViewer.VerticalOffset - per_height);
}
}
private void Editor_TextChanged(object sender, TextChangedEventArgs e)
{
if (Linenumber.Children.Count != Editor.LineCount)
{
if (Linenumber.Children.Count < Editor.LineCount)
{
for (int i = Linenumber.Children.Count; i < Editor.LineCount; i )
{
TextBlock Linecode = new TextBlock()
{
Height = Line_Height,
Width = Linecode_Width - Linecode_Interval,
TextAlignment = TextAlignment.Right,
FontFamily = fontFamily,
FontSize = Fontsize,
Foreground = new SolidColorBrush(LinecodeColor),
Background = Brushes.Transparent
};
Canvas.SetTop(Linecode, i * Line_Height);
Linenumber.Children.Add(Linecode);
}
}
else
if (Linenumber.Children.Count > Editor.LineCount)
{
for (int i = Linenumber.Children.Count-1 ; i > Editor.LineCount - 1; i--)
{
Linenumber.Children.RemoveAt(i);
}
}
for (int i = 0; i < Linenumber.Children.Count; i )
{
(Linenumber.Children[i] as TextBlock).Text = "";
}
for (int i = 0; i < Editor.LineCount; i )
{
(Linenumber.Children[i] as TextBlock).Text = (i 1).ToString();
}
Linenumber.Height = Editor.LineCount* Line_Height < Editor_Box.Height? Editor_Box.Height: Editor.LineCount * Line_Height;
}
}
private void Set_Menu()
{
if (string.IsNullOrEmpty(Clipboard.GetText()))
{
editMenu.SetMenuVisable(2, false);
}
else
{
editMenu.SetMenuVisable(2, true);
}
////////////////////////////////////////////////
if (string.IsNullOrEmpty(Editor.Text))
{
editMenu.SetMenuVisable(0, false);
editMenu.SetMenuVisable(1, false);
editMenu.SetMenuVisable(3, false);
editMenu.SetMenuVisable(4, false);
}
else
{
editMenu.SetMenuVisable(3, true);
if (Editor.SelectedText.Length < 1)
{
editMenu.SetMenuVisable(0, false);
editMenu.SetMenuVisable(1, false);
editMenu.SetMenuVisable(4, false);
}
else
{
editMenu.SetMenuVisable(0, true);
editMenu.SetMenuVisable(1, true);
editMenu.SetMenuVisable(4, true);
}
}
}
public void Set_TextBoxDefrag()
{
if (editMenu.ShowMenu)
{
editMenu.Visibility = Visibility.Hidden;
editMenu.ShowMenu = false;
}
Editor.Select(Editor.SelectionStart, 0);
}
}
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论