資源簡介
WPF TextBox如果行數(shù)過多,比如1、2千行,這時候按下ctrl-a進行全選(卡頓很慢),再點擊右鍵,這時候右鍵菜單要么打開遲緩,要么不停閃爍,點擊菜單項后,不會執(zhí)行相應的菜單命令。
根據(jù)這個情況,重建一個菜單,雖然ctrl-a進行全選還很慢,但菜單顯示及執(zhí)行功能不受影響了。組件要添加到根部的canvas里面。
在vs2017編譯后不出現(xiàn)異常,vs2019有幾率出現(xiàn)粘貼板錯誤,自行百度修正。
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; // 行號右側(cè)間隔 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); } }
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Configuration;
using?System.Data;
using?System.Linq;
using?System.Threading.Tasks;
using?System.Windows;
namespace?Test_1
{
????///?
????///?App.xaml?的交互邏輯
????///?
????public?partial?class?App?:?Application
????{
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????..A..H.?????73728??2020-05-12?09:26??Test_1\.vs\Test_1\v15\.suo
?????文件??????????0??2020-05-08?09:35??Test_1\.vs\Test_1\v15\Server\sqlite3\db.lock
?????文件?????708608??2020-05-12?09:15??Test_1\.vs\Test_1\v15\Server\sqlite3\storage.ide
?????文件??????32768??2020-05-12?08:12??Test_1\.vs\Test_1\v15\Server\sqlite3\storage.ide-shm
?????文件????4140632??2020-05-12?09:25??Test_1\.vs\Test_1\v15\Server\sqlite3\storage.ide-wal
????..A..H.?????76800??2020-05-11?22:08??Test_1\.vs\Test_1\v16\.suo
?????文件????????189??2020-05-08?09:35??Test_1\Test_1\App.config
?????文件????????366??2020-05-08?09:35??Test_1\Test_1\App.xaml
?????文件????????333??2020-05-08?09:35??Test_1\Test_1\App.xaml.cs
?????文件??????89600??2020-05-12?09:26??Test_1\Test_1\bin\Debug\Test_1.exe
?????文件????????189??2020-05-08?09:35??Test_1\Test_1\bin\Debug\Test_1.exe.config
?????文件??????54784??2020-05-12?09:26??Test_1\Test_1\bin\Debug\Test_1.pdb
?????文件??????14667??2020-05-09?09:00??Test_1\Test_1\Images\brush.png
?????文件??????14598??2020-05-09?09:00??Test_1\Test_1\Images\copy.png
?????文件??????14673??2020-05-09?09:26??Test_1\Test_1\Images\cut.png
?????文件????????288??2020-04-28?14:06??Test_1\Test_1\Images\del_c.png
?????文件????????271??2020-04-28?14:06??Test_1\Test_1\Images\del_page.png
?????文件??????14677??2020-05-09?09:01??Test_1\Test_1\Images\past.png
?????文件????????272??2016-01-08?10:25??Test_1\Test_1\Images\s_all.png
?????文件????????571??2020-05-12?08:58??Test_1\Test_1\MainWindow.xaml
?????文件????????751??2020-05-12?09:25??Test_1\Test_1\MainWindow.xaml.cs
?????文件???????2321??2020-05-12?09:26??Test_1\Test_1\obj\Debug\App.g.cs
?????文件???????2321??2020-05-12?09:26??Test_1\Test_1\obj\Debug\App.g.i.cs
?????文件????????672??2020-05-12?08:12??Test_1\Test_1\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????7180??2020-05-11?15:42??Test_1\Test_1\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件???????3669??2020-05-11?15:41??Test_1\Test_1\obj\Debug\EditMenu.g.i.cs
?????文件???????1119??2020-05-12?09:26??Test_1\Test_1\obj\Debug\MainWindow.baml
?????文件???????3931??2020-05-12?09:26??Test_1\Test_1\obj\Debug\MainWindow.g.cs
?????文件???????3931??2020-05-12?09:26??Test_1\Test_1\obj\Debug\MainWindow.g.i.cs
?????文件??????????0??2020-05-08?09:35??Test_1\Test_1\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
............此處省略58個文件信息
評論
共有 條評論