在教学和演示中,我们通常需要对重点进行批注,下载安装第三方工具批注显得很麻烦。本篇使用WPF开发了一个批注工具,工具小巧,功能丰富,非常使用日常免费使用,或者进行再次开发。
自制批注工具具有以下功能特点:
- 支持图画,擦除,撤销,恢复,改变画板颜色功能
- 提供颜色选择和线条大小设置功能
- 方便演示教学
示例代码如下:
Stack<StrokeCollection> undoStack = new Stack<StrokeCollection>();//撤销
Stack<StrokeCollection> recoverStack = new Stack<StrokeCollection>();//恢复撤销
public MainWindow()
{
InitializeComponent();
try
{
EraserCanvas.MouseDown += EraserCanvas_MouseDown;
EraserCanvas.MouseMove += EraserCanvas_MouseMove;
EraserCanvas.MouseUp += EraserCanvas_MouseUp;
Global.Instance.CurrentCanvas.PreviewMouseDown += WriteBorad_PreviewMouseDown;
InkCanvasRootPanel.Children.Clear();
InkCanvasRootPanel.Children.Add(Global.Instance.CurrentCanvas);
}
catch (Exception)
{
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//设置色板和清空橡皮位置
Window window = Window.GetWindow(rdoPen);
Point point = rdoPen.TransformToAncestor(window).Transform(new Point(0, 0));
translateColor.X = translateColor.X - 100;
translateEraser.X = translateEraser.X-30;
if (rdoPenWidthMin.Content.ToString() == Global.Instance.ColorHelper.selectedPenWidth.Trim())
rdoPenWidthMin.IsChecked = true;
else if (rdoPenWidthMedium.Content.ToString() == Global.Instance.ColorHelper.selectedPenWidth.Trim())
rdoPenWidthMedium.IsChecked = true;
else if (rdoPenWidthMax.Content.ToString() == Global.Instance.ColorHelper.selectedPenWidth.Trim())
rdoPenWidthMax.IsChecked = true;
//初始化色板
foreach (string color in Global.Instance.ColorHelper.colorList)
{
RadioButton btntemp = new RadioButton();
btntemp.SetResourceReference(StyleProperty, "ColorSelectStyle");
btntemp.Width = 30;
btntemp.Height = 30;
btntemp.Margin = new Thickness(2, 2, 2, 0);
btntemp.Background = new SolidColorBrush((System.Windows.Media.Color)ColorConverter.ConvertFromString(color));
if (btntemp.Background.ToString() == Global.Instance.ColorHelper.selectedColor)
{
btntemp.IsChecked = true;
Global.Instance.CurrentCanvas.DefaultDrawingAttributes.Color =(System.Windows.Media.Color)ColorConverter.ConvertFromString((string)Global.Instance.ColorHelper.selectedColor);
}
uniformColor.Children.Add(btntemp);
}
}
#region InkCanvas操作
bool transformColorFlag = false;
bool transformEraserClearFlag = false;
private void RadioButton_Click(object sender, RoutedEventArgs e)
{
//橡皮模式下选择其他工具关闭橡皮擦功能
EraserCanvas.Visibility = Visibility.Collapsed;
_eraserisDown = false;
//如果色板打开下选择其他工具,隐藏色板工具
if (!(bool)rdoPen.IsChecked)
{
transformColorFlag = false;
translateColor.Y = 300;
}
if (!(bool)rdoEraser.IsChecked)
{
transformEraserClearFlag = false;
translateEraser.Y = 100;
}
if ((sender as RadioButton).Content.ToString() == "画笔")
{
//Topmost = false;
this.Background = (Brush)new BrushConverter().ConvertFromString("#01000000");
if (transformColorFlag)
{
translateColor.Y = -10;
}
else
{
translateColor.Y = 300;
}
transformColorFlag = !transformColorFlag;
Global.Instance.CurrentCanvas.EditingMode = InkCanvasEditingMode.Ink;
}
else if ((sender as RadioButton).Content.ToString() == "橡皮")
{
this.Background = (Brush)new BrushConverter().ConvertFromString("#01000000");
if (transformEraserClearFlag)
{
translateEraser.Y = -10;
}
else
{
translateEraser.Y = 100;
}
transformEraserClearFlag = !transformEraserClearFlag;
Global.Instance.CurrentCanvas.EditingMode = InkCanvasEditingMode.None;
EraserCanvas.Visibility = Visibility.Visible;
TranslateTransform.X = -1000;
TranslateTransform.Y = -1000;
}
else if ((sender as RadioButton).Content.ToString() == "选择")
{
//Topmost = true;
this.Background = System.Windows.Media.Brushes.Transparent;
Global.Instance.CurrentCanvas.EditingMode = InkCanvasEditingMode.None;
// Global.Instance.CurrentCanvas.EditingMode = InkCanvasEditingMode.Select;
}
else if ((sender as RadioButton).Content.ToString() == "撤销")
{
if (undoStack.Count > 0)
{
recoverStack.Push(Global.Instance.CurrentCanvas.Strokes.Clone());
Global.Instance.CurrentCanvas.Strokes = undoStack.Pop();
}
}
else if ((sender as RadioButton).Content.ToString() == "恢复")
{
if (recoverStack.Count > 0)
{
undoStack.Push( Global.Instance.CurrentCanvas.Strokes.Clone());
Global.Instance.CurrentCanvas.Strokes = recoverStack.Pop();
}
}
}
#endregion
实例链接:https://download.csdn.net/download/lvxingzhe3/89505204