如何重写PictureBox的OnPaint方法绘制矩形,并实现拖动,鼠标悬停时提示拖拽方向

来源:互联网 发布:马尔可夫链 转化矩阵 编辑:程序博客网 时间:2024/06/06 04:11

使用Graphic绘制可拖动的矩形框(鼠标悬停可以显示拖动提示)
首先做绘图前的准备
新建winform程序,在解决方案上右击添加用户控件,然后同样的方法添加组件并继承于用户控件;
一切准备好后,在Form窗体中将添加的用户控件拖拽到窗体中;

 public partial class mainPictureBox :myPictureBox    {        private Pen myPen;        private Color myColor;        private Rectangle drawRectangle;        public bool ROIStatus;        public mainPictureBox()        {            InitializeComponent();            this.SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.UserPaint | System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer, true);            myColor = Color.ForestGreen;            myPen = new Pen(myColor,2);            drawRectangle = new Rectangle(10, 10, 100, 100);            ROIStatus = false;        }

然后开始重写OnPaint方法绘图

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)        {            base.OnPaint(e);            if(ROIStatus)            {                e.Graphics.DrawRectangle(myPen, drawRectangle);            }            Invalidate();        }

关于托动鼠标悬停事件后续会更新

阅读全文
0 0
原创粉丝点击