陈程的技术博客

  • 关于作者
全栈软件工程师
一个专注于技术研究创新的程序员
  1. 首页
  2. .NET
  3. 正文

在winform窗体上创建一个可以在主窗体上拖动拉伸的panel控件

2018年4月8日 567点热度 0人点赞 0条评论
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ScreenShowManage
{
    /// <summary>
    /// Screen 可以在主窗体上拖动拉伸的panel控件
    /// </summary>
    public partial class Screen : UserControl
    {
        //定义委托
        public delegate void ScreenMouseUpHandle();
        //定义事件 
        public event ScreenMouseUpHandle ScreenMouseUpEvent;

        Point pt;
        public Screen()
        {
            InitializeComponent();
        }

        private void Screen_MouseDown(object sender, MouseEventArgs e)
        {
            pt = Cursor.Position;
            MouseState = true;
        }

        /// <summary>
        /// 鼠标状态是否变化成可拉伸的样式
        /// </summary>
        public bool isChange = false;
        /// <summary>
        /// 鼠标方向
        /// </summary>
        public DictType Dict = DictType.NONE;
        /// <summary>
        /// 鼠标状态是否按下
        /// </summary>
        public bool MouseState = false;
        /// <summary>
        /// 鼠标移动时候
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Screen_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                SetChangeSize();
                if (e.Button == MouseButtons.Left)
                {
                    if (isChange)
                    {
                        switch (Dict)
                        {
                            case DictType.NONE:
                                break;
                            case DictType.LEFT:
                                int px = Cursor.Position.X - pt.X;
                                this.Location = new Point(this.Location.X + px, this.Location.Y);
                                this.Width += -px;
                                pt = Cursor.Position;
                                break;
                            case DictType.RIGHT:
                                this.Width = this.PointToClient(MousePosition).X;
                                break;
                            case DictType.TOP:
                                int py = Cursor.Position.Y - pt.Y;
                                this.Location = new Point(this.Location.X, this.Location.Y + py);
                                this.Height += -py;
                                pt = Cursor.Position;
                                break;
                            case DictType.BOTTOM:
                                this.Height = this.PointToClient(MousePosition).Y;
                                break;
                            case DictType.LEFT_TOP:
                                int ltpx = Cursor.Position.X - pt.X;
                                this.Location = new Point(this.Location.X + ltpx, this.Location.Y);
                                this.Width += -ltpx;
                                int ltpy = Cursor.Position.Y - pt.Y;
                                this.Location = new Point(this.Location.X, this.Location.Y + ltpy);
                                this.Height += -ltpy;
                                pt = Cursor.Position;
                                break;
                            case DictType.LEFT_BOTTOM:
                                int lbpx = Cursor.Position.X - pt.X;
                                this.Location = new Point(this.Location.X + lbpx, this.Location.Y);
                                this.Width += -lbpx;
                                this.Height = this.PointToClient(MousePosition).Y;
                                pt = Cursor.Position;
                                break;
                            case DictType.RIGHT_TOP:
                                this.Width = this.PointToClient(MousePosition).X;
                                int rtpy = Cursor.Position.Y - pt.Y;
                                this.Location = new Point(this.Location.X, this.Location.Y + rtpy);
                                this.Height += -rtpy;
                                pt = Cursor.Position;
                                break;
                            case DictType.RIGHT_BOTTOM:
                                Point ptt = this.PointToClient(MousePosition);
                                this.Height = ptt.Y;
                                this.Width = ptt.X;
                                break;
                        }

                    }
                    else
                    {
                        int px = Cursor.Position.X - pt.X;
                        int py = Cursor.Position.Y - pt.Y;
                        this.Location = new Point(this.Location.X + px, this.Location.Y + py);
                        pt = Cursor.Position;
                    }
                }
                label1.Text = "(X:" + this.Location.X + "Y:" + this.Location.Y + ")";
                label2.Text = "(W:" + this.Width + "H:" + this.Height + ")";
            }
            catch (Exception ex)
            {
                LoggingHelper.Error(ex.StackTrace);
            }
        }

        /// <summary>
        /// 鼠标放置到边界处出现拉伸样式
        /// </summary>
        private void SetChangeSize()
        {
            try
            {
                int mlength = 2;
                Point position = this.PointToClient(MousePosition);//鼠标相对于窗体的坐标
                                                                   //鼠标位置
                int cursorX = position.X;
                int cursorY = position.Y;
                //矩形位置
                int width = this.Width;
                int hegith = this.Height;


                if (Math.Abs(cursorX - 0) <= mlength && Math.Abs(cursorY - 0) <= mlength)
                {
                    //左上
                    if (!MouseState)
                    {
                        //this.Cursor = Cursors.SizeNWSE;
                        Dict = DictType.LEFT_TOP;
                    }
                    else
                        isChange = true;
                }
                else if (Math.Abs(cursorX - 0) <= mlength && Math.Abs(cursorY - hegith) <= mlength)
                {
                    //左下
                    if (!MouseState)
                    {
                        //this.Cursor = Cursors.SizeNESW;
                        Dict = DictType.LEFT_BOTTOM;
                    }
                    else
                        isChange = true;
                }
                else if (Math.Abs(cursorX - width) <= mlength && Math.Abs(cursorY - 0) <= mlength)
                {
                    //右上
                    if (!MouseState)
                    {
                        //this.Cursor = Cursors.SizeNESW;
                        Dict = DictType.RIGHT_TOP;
                    }
                    else
                        isChange = true;
                }
                else if (Math.Abs(cursorX - width) <= mlength && Math.Abs(cursorY - hegith) <= mlength)
                {
                    //右下
                    if (!MouseState)
                    {
                        //this.Cursor = Cursors.SizeNWSE;
                        Dict = DictType.RIGHT_BOTTOM;
                    }
                    else
                        isChange = true;
                }
                else if (Math.Abs(cursorX - 0) <= mlength)
                {
                    //左
                    if (!MouseState)
                    {
                        //this.Cursor = Cursors.SizeWE;
                        Dict = DictType.LEFT;
                    }
                    else
                        isChange = true;
                }
                else if (Math.Abs(cursorY - 0) <= mlength)
                {
                    //上
                    if (!MouseState)
                    {
                       // this.Cursor = Cursors.SizeNS;
                        Dict = DictType.TOP;
                    }
                    else
                        isChange = true;
                }
                else if (Math.Abs(cursorX - width) <= mlength)
                {
                    //右
                    if (!MouseState)
                    {
                        //this.Cursor = Cursors.SizeWE;
                        Dict = DictType.RIGHT;
                    }
                    else
                        isChange = true;
                }
                else if (Math.Abs(cursorY - hegith) <= mlength)
                {
                    //下
                    if (!MouseState)
                    {
                       // this.Cursor = Cursors.SizeNS;
                        Dict = DictType.BOTTOM;
                    }
                    else
                        isChange = true;
                }
                else
                {
                    if (!MouseState)
                    {
                        this.Cursor = Cursors.Default;
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingHelper.Error(ex.StackTrace);
            }
        }

        private void Screen_MouseUp(object sender, MouseEventArgs e)
        {
            isChange = false;
            MouseState = false;
            Dict = DictType.NONE;
            ScreenMouseUpEvent?.Invoke();
        }

        /// <summary>
        /// 鼠标相对控件边界的方向
        /// </summary>
        public enum DictType
        {
            LEFT_TOP, LEFT_BOTTOM, RIGHT_TOP, RIGHT_BOTTOM, TOP, LEFT, RIGHT, BOTTOM, NONE
        }

        private void Screen_Load(object sender, EventArgs e)
        {
            //label1.Text = "(X:" + this.Location.X + "Y:" + this.Location.Y + ")";
            //label2.Text = "(W:" + this.Width + "H:" + this.Height + ")";
            label1.Visible = false;
            label2.Visible = false;
        }
    }
}

 

标签: winform
最后更新:2021年4月2日

博主

全栈工程师,侧重项目技术解决方案规划和开发

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

分类
  • .NET (65)
  • docker (3)
  • linux (12)
  • python (20)
  • web (14)
  • 小程序 (4)
  • 数据库 (2)
  • 未分类 (4)
  • 杂七杂八 (10)
标签聚合
C# nginx linux centos python js DevExpress winform
最新 热点 随机
最新 热点 随机
.NET开发手册标准参考 招募兼职前端开发 Centos安装dotnet6环境 VS上切换分支,vs编译运行出现bug,A fatal error was encountered彻底解决方案 用C#封装一个线程安全的缓存器,达到目标定时定量更新入库 C#通过特性的方式去校验指定数据是否为空
前端VUE调用接口下载execl出现乱码打不开的问题 在winform窗体上创建一个可以在主窗体上拖动拉伸的panel控件 机器学习目标检测之印章检测和分类 用C#写基于用户的协同过滤算法,推荐网站上你可能感兴趣的人 使用C#+Jumony开发网络爬虫并对数据做相关分析 微信小程序 音频播放功能createInnerAudioContext

COPYRIGHT © 2021 陈程的技术博客. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS