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; } } }
文章评论