WPF TextBox如果行数过多,比如1、2千行,这时候按下ctrl-a进行全选(卡顿很慢),再点击右键,这时候右键菜单要么打开迟缓,要么不停闪烁,点击菜单项后,不会执行相应的菜单命令。根据这个情况,脱离右键菜单,重建一个菜单,虽然ctrl-a进行全选还很慢,但菜单显示及执行功能不受影响了。组件要添加到根部的canvas里面
下载:
链接:https://pan.baidu.com/s/1vja-Bo8C8jfrOjs7C_v0zw
提取码:qubp
在vs2017编译不出现异常,vs2019有几率出现粘贴板错误,自行百度修正。
TextBoxHelper.cs
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);
}
}
EditMenu.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
public partial class EditMenu : UserControl
{
public bool ShowMenu = false;
public int Option = -1;
private bool[] menuVisable = new bool[5] { false, false, false, false, false };
private Image[] image = new Image[5];
private TextBlock[] menublock = new TextBlock[5];
private Border[] border = new Border[5];
public EditMenu()
{
InitializeComponent();
string[] menu_text = new string[] { "剪切", "复制", "粘贴", "全选", "清除" };
string[] menu_key = new string[] { "Ctrl+X", "Ctrl+C", "Ctrl+V", "Ctrl+A", "Delete" };
string[] menu_image = new string[] { "cut.png", "copy.png", "past.png", "s_all.png", "brush.png" };
int step = 0;
for (int i = 0; i < 5; i++)
{
step = i > 2 ? 6 : 0;
border[i] = new Border()
{
Width = 149,
Height = 22,
BorderBrush = Brushes.Transparent,
Background = Brushes.Transparent,
BorderThickness = new Thickness(1),
Opacity = 0.9,
Tag = i
};
border[i].MouseEnter += EditMenu_MouseEnter;
border[i].MouseLeave += EditMenu_MouseLeave;
border[i].MouseUp += EditMenu_MouseUp;
Canvas.SetLeft(border[i], 3);
Canvas.SetTop(border[i], 4 + i * 21 + step);
Menu_Box.Children.Add(border[i]);
Canvas content = new Canvas()
{
Width = 149,
Height = 21,
};
border[i].Child = content;
image[i] = new Image
{
Width = 16,
Height = 16,
Source = new BitmapImage(new Uri("../Images/" + menu_image[i], UriKind.RelativeOrAbsolute))
};
Canvas.SetLeft(image[i], 5);
Canvas.SetTop(image[i], 2);
content.Children.Add(image[i]);
menublock[i] = new TextBlock()
{
Text = menu_text[i] + " " + menu_key[i]
};
Canvas.SetLeft(menublock[i], 40);
Canvas.SetTop(menublock[i], 3);
content.Children.Add(menublock[i]);
}
}
private void EditMenu_MouseUp(object sender, MouseButtonEventArgs e)
{
Border pL = (Border)sender;
Option = -1;
if (menuVisable[(int)pL.Tag])
{
Option = (int)pL.Tag;
}
ShowMenu = false;
this.Visibility = Visibility.Hidden;
}
private void EditMenu_MouseLeave(object sender, MouseEventArgs e)
{
Border pL = (Border)sender;
pL.BorderBrush = Brushes.Transparent;
pL.Background = Brushes.Transparent;
}
private void EditMenu_MouseEnter(object sender, MouseEventArgs e)
{
Border pL = (Border)sender;
if (menuVisable[(int)pL.Tag])
{
pL.BorderBrush = new SolidColorBrush(Color.FromArgb(0xff, 0x26, 0xa0, 0xda));
pL.Background = new SolidColorBrush(Color.FromArgb(0xff, 0xc3, 0xe0, 0xee));
}
}
public void SetMenuVisable(int item, bool visable)
{
menuVisable[item] = visable;
if (menuVisable[item])
{
image[item].Opacity = 1;
menublock[item].Opacity = 1;
}
else
{
image[item].Opacity = 0.5;
menublock[item].Opacity = 0.5;
}
}
}