随着电子屏幕技术的发展,普通的黑板已不再适用现在的教学和演示环境,电子白板应运而生。本篇使用WPF开发了一个电子白板工具,功能丰富,非常使用日常免费使用,或者进行再次开发。
示例代码如下:
cs
Stack<StrokeCollection> undoStack = new Stack<StrokeCollection>();//撤销
Stack<StrokeCollection> recoverStack = new Stack<StrokeCollection>();//恢复撤销
public MainWindow()
{
InitializeComponent();
this.DataContext = Global.Instance.WhiteBoardPageModel;
try
{
EraserCanvas.MouseDown += EraserCanvas_MouseDown;
EraserCanvas.MouseMove += EraserCanvas_MouseMove;
EraserCanvas.MouseUp += EraserCanvas_MouseUp;
Global.Instance.InkCanvasList[ Global.Instance.WhiteBoardPageModel.PageIndex-1].PreviewMouseDown += WriteBorad_PreviewMouseDown;
InkCanvasRootPanel.Children.Clear();
InkCanvasRootPanel.Children.Add(Global.Instance.InkCanvasList[Global.Instance.WhiteBoardPageModel.PageIndex - 1]);
}
catch (Exception)
{
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//top = this.Top;
//left = this.Left;
//width = this.Width;
//heigh = this.Height;
//启动最大化白板
this.Top = this.Left = 0;
this.Width = SystemParameters.WorkArea.Width;
this.Height = SystemParameters.WorkArea.Height;
//设置色板和清空橡皮位置
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.InkCanvasList[ Global.Instance.WhiteBoardPageModel.PageIndex-1].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() == "画笔")
{
if (transformColorFlag)
{
translateColor.Y = -10;
}
else
{
translateColor.Y = 300;
}
transformColorFlag = !transformColorFlag;
Global.Instance.InkCanvasList[Global.Instance.WhiteBoardPageModel.PageIndex - 1].EditingMode = InkCanvasEditingMode.Ink;
}
else if ((sender as RadioButton).Content.ToString() == "橡皮")
{
if (transformEraserClearFlag)
{
translateEraser.Y = -10;
}
else
{
translateEraser.Y = 100;
}
transformEraserClearFlag = !transformEraserClearFlag;
Global.Instance.InkCanvasList[Global.Instance.WhiteBoardPageModel.PageIndex - 1].EditingMode = InkCanvasEditingMode.None;
EraserCanvas.Visibility = Visibility.Visible;
TranslateTransform.X = -1000;
TranslateTransform.Y = -1000;
//Global.Instance.InkCanvasList[ Global.Instance.WhiteBoardPageModel.PageIndex-1].EditingMode = InkCanvasEditingMode.EraseByPoint;
//Global.Instance.InkCanvasList[ Global.Instance.WhiteBoardPageModel.PageIndex-1].EditingMode = InkCanvasEditingMode.EraseByStroke;
}
else if ((sender as RadioButton).Content.ToString() == "选择")
{
//PenFullScreen();
Global.Instance.InkCanvasList[Global.Instance.WhiteBoardPageModel.PageIndex - 1].EditingMode = InkCanvasEditingMode.None;
//Global.Instance.InkCanvasList[ Global.Instance.WhiteBoardPageModel.PageIndex-1].EditingMode = InkCanvasEditingMode.Select;
}
else if ((sender as RadioButton).Content.ToString() == "撤销")
{
if (undoStack.Count > 0)
{
recoverStack.Push(Global.Instance.InkCanvasList[Global.Instance.WhiteBoardPageModel.PageIndex - 1].Strokes.Clone());
Global.Instance.InkCanvasList[Global.Instance.WhiteBoardPageModel.PageIndex - 1].Strokes = undoStack.Pop();
}
}
else if ((sender as RadioButton).Content.ToString() == "恢复")
{
if (recoverStack.Count > 0)
{
undoStack.Push(Global.Instance.InkCanvasList[Global.Instance.WhiteBoardPageModel.PageIndex - 1].Strokes.Clone());
Global.Instance.InkCanvasList[Global.Instance.WhiteBoardPageModel.PageIndex - 1].Strokes = recoverStack.Pop();
}
}
}
#endregion