资源简介
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);
}
}
代码片段和文件信息
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个文件信息
相关资源
- wpf实现展开收缩效果(ShrinkSpread)
- C#之WPF重绘动态正弦余弦曲线
- WPF 自定义标题的winform窗体实现源码
- c# WPF串口模拟自动生成数据
- WPF 模拟区域选择
- WPF实现视差效果
- wpf gridsplit 布局
- 盛大点卷充值
- WpfBinding demo
- wpf textbox Placeholder Demo
- DevExpressUniversalTrial16.2.3
- WPF自动序号(观察者模式)
- 开火车小游戏源码(基于wpf开发的小
- WPF画布实现旋转等待效果
- C# wpf_动态图片加载datagrid
- wpf任务管理器源码
- wpf自定义进度条
- WPF中将矢量转换为XAML
- Printing wpf的打印功能实力
- qqWPF 一款应用微软WPF技术编写的仿q
- wpf-treeview 自定义漂亮的wpf树控件
- WpfApplication1 一个很好的示例
- MyFriends_WPF_WIN C#VS2008 运用微软最新技
- FreeSCADA2 完整的C#开发的组态软件.OP
- wpf_DragDrop WPF 鼠标拖拽功能
- wpf_animation WPF 三个系列经典动画示例
- CustomSlider 自定义时间轴
- VideoPlay 使用WPF编写的炫目视频播放器
- Wpf-Switch-Pic WPF图片切换效果 canvas
- WPF001

评论
共有 条评论