C#/WPF 自制白板工具

随着电子屏幕技术的发展,普通的黑板已不再适用现在的教学和演示环境,电子白板应运而生。本篇使用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

实例链接:https://download.csdn.net/download/lvxingzhe3/89505111

相关推荐
何曾参静谧1 分钟前
「QT」文件类 之 QIODevice 输入输出设备类
开发语言·qt
爱吃生蚝的于勒1 小时前
C语言内存函数
c语言·开发语言·数据结构·c++·学习·算法
小白学大数据3 小时前
Python爬虫开发中的分析与方案制定
开发语言·c++·爬虫·python
冰芒猓4 小时前
SpringMVC数据校验、数据格式化处理、国际化设置
开发语言·maven
失落的香蕉4 小时前
C语言串讲-2之指针和结构体
java·c语言·开发语言
红中马喽4 小时前
JS学习日记(webAPI—DOM)
开发语言·前端·javascript·笔记·vscode·学习
冷眼Σ(-᷅_-᷄๑)4 小时前
Path.Combine容易被忽略的细节
c#·.net
杜杜的man4 小时前
【go从零单排】Closing Channels通道关闭、Range over Channels
开发语言·后端·golang
java小吕布5 小时前
Java中Properties的使用详解
java·开发语言·后端
versatile_zpc5 小时前
C++初阶:类和对象(上)
开发语言·c++