C# GDI画图系列(一)实现画布,主控大脑,和鼠标标识

先来个整体效果
在这里插入图片描述
在这里插入图片描述
点击线条之后
在这里插入图片描述
鼠标放中间蓝色小块上改变位置,放两侧红块上随鼠标改变线的方向和长度

在这里插入图片描述
以及画四边框时的选定和改变样式

饭一口一口吃

先来实现个简单的 ,实现画布和主控大脑以及鼠标标识

先实现画布接口 IMyCanvas,主控大脑接口 IParents

 /// <summary>
    /// 画布接口
    /// </summary>
    public interface IMyCanvas
    {
        // 画布宽
        double width { get; }
        // 画布高
        double height { get; }
        Point HideFouce(Point poi);
        /// <summary>
        /// 设置Presenter
        /// </summary>
        void SetPresenter(IParents controller);

        /// <summary>
        /// 添加子元素
        /// </summary>
        void AddChild(object child);

        /// <summary>
        /// 删除子元素
        /// </summary>
        void RemoveChild(object child);

        /// <summary>
        /// 重绘
        /// </summary>
        void Repaint();
        void Repaint(double x, double y, double width, double height);
    }
/// <summary>
    /// 大脑接口
    /// </summary>
    public interface IParents
    {
        IMyCanvas canvas { get; }
        void OnPaintCanvas(PaintEventArgs e);
        void OnResize(EventArgs e);
        void OnMouseDown(MouseEventArgs e);
        void OnMouseUp(MouseEventArgs e);
        void OnMouseMove(MouseEventArgs e);
        void OnMouseDoubleClick(MouseEventArgs e);
        void OnMouseWheel(MouseEventArgs e);
        void OnKeyDown(KeyEventArgs e);
        void OnKeyUp(KeyEventArgs e);
    }

添加鼠标类 MousePoint,负责大部分后续与鼠标的交互功能

 class MousePoint
    {
        private Bitmap _bitmap = null;
        private Presenter _presenter = null;
        private PointF _point = new PointF();

        internal MousePoint(Presenter presenter)
        {
            _presenter = presenter;
            UpdateBitmap();
        }

        private void UpdateBitmap()//鼠标标识
        {
            _bitmap = new Bitmap(50, 50);
            Graphics graphics = Graphics.FromImage(_bitmap);
            Pen pen = new Pen(Color.Red);
            graphics.DrawLine(pen,
              _bitmap.Width / 2, 0,
              _bitmap.Width / 2, _bitmap.Height);
            graphics.DrawLine(pen,
                0, _bitmap.Height / 2,
                _bitmap.Width, _bitmap.Height / 2);
        }

        internal void OnPaint(Graphics graphics)
        {
            //图像为一个十字架,画图位置为图像左上角的位置,让点击的点成为十字架的中心
            graphics.DrawImage(_bitmap, (float)(_point.X - _bitmap.Width / 2), (float)(_point.Y - _bitmap.Height / 2));
        }
        internal void OnMouseMove(MouseEventArgs e)
        {
            _point = e.Location;
            _presenter.RepaintCanvas();
        }
    }

添加主控大脑类 Presenter(重要)

 class Presenter : IParents
    {
        /// <summary>
        /// 画布
        /// </summary>
        private IMyCanvas _canvas = null;
        /// <summary>
        /// 鼠标标识
        /// </summary>
        private MousePoint _point = null;


        public Presenter(IMyCanvas canvas)
        {
            _canvas = canvas;
            _point = new MousePoint(this);
        }

        public IMyCanvas canvas
        {
            get
            {
                return _canvas;
            }
        }
        /// <summary>
        /// 鼠标在画布上按下时,调用主导的按下方法
        /// </summary>
        /// <param name="e"></param>
        public void OnMouseDown(MouseEventArgs e)
        {

        }
        /// <summary>
        /// 鼠标在画布上挪动时,触发主导的鼠标移动事件
        /// </summary>
        /// <param name="e"></param>
        public void OnMouseMove(MouseEventArgs e)
        {
            _point.OnMouseMove(e);
        }
        public void OnMouseUp(MouseEventArgs e)
        {

        }
        public void OnKeyDown(KeyEventArgs e)
        {

        }

        public void OnKeyUp(KeyEventArgs e)
        {

        }
        public void OnMouseDoubleClick(MouseEventArgs e)
        {
           
        }
        public void OnResize(EventArgs e)
        {          
            RepaintCanvas(true);
        }
        public void OnMouseWheel(MouseEventArgs e)
        {
           
        }

        internal void RepaintCanvas(bool bufferBitmapToRedraw = false)
        {           
            _canvas.Repaint();
        }
        /// <summary>
        /// 主要绘图的方法
        /// </summary>
        /// <param name="e"></param>
        public void OnPaintCanvas(PaintEventArgs e)
        {          
            //画出鼠标点
            _point.OnPaint(e.Graphics);
        }
    }

添加画布自定义控件 CanavsControl

public partial class CanavsControl : UserControl, IMyCanvas
    {
        private IParents _presenter = null;
        // 画布宽
        public double width
        {
            get { return this.ClientRectangle.Width; }
        }
        // 画布高
        public double height
        {
            get { return this.ClientRectangle.Height; }
        }

        public CanavsControl()
        {
            InitializeComponent();
            //防止界面闪烁
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }
        /// <summary>
        /// 设置画布的主导对象
        /// </summary>
        /// <param name="presenter"></param>
        public void SetPresenter(IParents presenter)
        {
            _presenter = presenter;
        }   
        /// <summary>
        /// 在画布中添加控件
        /// </summary>
        /// <param name="child"></param>
        public void AddChild(object child)
        {
            this.Controls.Add((Control)child);
            Control thiscontrol = (Control)child;
            thiscontrol.Focus();
        }
        /// <summary>
        /// 在画布中删除控件
        /// </summary>
        /// <param name="child"></param>
        public void RemoveChild(object child)
        {
            this.Controls.Remove((Control)child);
        }
        public Point HideFouce(Point poi)
        {
            return this.PointToClient(poi);
        }
        /// <summary>
        /// 画布重绘图形
        /// </summary>
        public void Repaint()
        {
            Invalidate();
        }
        public void Repaint(double x, double y, double width, double height)
        {
            Invalidate(new Rectangle((int)x, (int)y, (int)width, (int)height));
        }       
        #region  画布上的鼠标事件,调用主导的事件
        protected override void OnPaint(PaintEventArgs e)
        {
            _presenter.OnPaintCanvas(e);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            if (_presenter != null)
            {
                _presenter.OnResize(e);
            }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            _presenter.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            _presenter.OnMouseUp(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            _presenter.OnMouseMove(e);
        }

        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            _presenter.OnMouseDoubleClick(e);
        }

        protected override void OnMouseWheel(MouseEventArgs e)
        {
            base.OnMouseWheel(e);
            _presenter.OnMouseWheel(e);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            _presenter.OnKeyDown(e);
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            _presenter.OnKeyUp(e);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            Cursor.Hide();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            Cursor.Show();
        }
        #endregion
    }

添加个窗体Form1

public partial class Form1 : Form
    {
        private Presenter _presenter = null;
        private CanavsControl _canvas = null;//画布

        public Form1()
        {
            InitializeComponent();
            _canvas = new CanavsControl();
            _presenter = new Presenter(_canvas);
            _canvas.Dock = DockStyle.Fill;
            _canvas.SetPresenter(_presenter);
            this.Controls.Add(_canvas);
        }
    }

运行
在这里插入图片描述
初步OK。

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
优化路径是指通过对路径进行优化,使其更加平滑,从而提高路径的质量和效率。在 C# 中,可以使用 GDI+实现路径的优化。 首先,需要创建一个 GraphicsPath 对象,该对象用于存储路径信息。然后,通过调用 GraphicsPath 的 AddLine 或 AddCurve 方法,将路径的线段或曲线段添加到 GraphicsPath 中。接下来,可以使用 GraphicsPath 的 Flatten 或 Widen 方法来优化路径。 Flatten 方法将路径中的曲线段转换为线段,从而使路径更加平滑。Widen 方法则可以将路径进行扩展,从而使路径更加宽阔。可以根据实际需求选择使用哪种方法进行路径优化。 以下是一个简单的示例代码,演示了如何使用 GDI+ 实现路径的优化: ``` // 创建 GraphicsPath 对象 GraphicsPath path = new GraphicsPath(); // 添加路径线段 path.AddLine(0, 0, 100, 100); path.AddLine(100, 100, 200, 100); path.AddLine(200, 100, 300, 0); // 优化路径 path.Flatten(); // 在画布上绘制路径 Graphics graphics = this.CreateGraphics(); graphics.DrawPath(Pens.Black, path); ``` 上述示例代码创建了一个 GraphicsPath 对象,添加了三条直线路径,并使用 Flatten 方法对路径进行优化。最后,使用 DrawPath 方法在画布上绘制优化后的路径。 需要注意的是,路径的优化是一个相对较慢的过程,如果需要对大量路径进行优化,可能会导致性能问题。因此,在实际应用中需要谨慎考虑路径优化的使用。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yangzm996

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值