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

相关推荐
web1368856587122 分钟前
rust教程 第一章 —— 初识rust
开发语言·后端·rust
songroom23 分钟前
Rust : tokio中select!
开发语言·后端·rust
dubochao_xinxi25 分钟前
QT5 在某些系统出现qt.qpa.xcb: could not connect
开发语言·qt
blueman888827 分钟前
QWidget应用封装为qt插件,供其他qt应用调用
开发语言·qt
qincjun28 分钟前
Qt仿音乐播放器:设置窗口、部件属性
开发语言·qt
编码小哥38 分钟前
C++线程同步和互斥
开发语言·c++
qincjun40 分钟前
Qt仿音乐播放器:动画类
开发语言·qt
L.S.V.1 小时前
Java 溯本求源之基础(三十)——封装,继承与多态
java·开发语言
游客5201 小时前
设计模式-创建型-工厂方法模式
开发语言·python·设计模式·工厂方法模式
m0_748234901 小时前
Hmsc包开展群落数据联合物种分布模型分析通用流程(Pipelines)
开发语言·python