C# 实现拖拉控件改变位置与大小的方法

2019-12-30 19:13:45于丽

FrameControl


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace DragControl
{
 public partial class FrameControl : UserControl
 {
  #region Constructors
  public FrameControl(Control ctrl)
  {
   baseControl = ctrl;
   AddEvents();
   CreateBounds();
  }
  #endregion
  #region Fields
  const int Band = 6; //调整大小的响应边框
  Size square = new Size(Band, Band);//小矩形大小
  Control baseControl; //基础控件,即被包围的控件
  Rectangle[] smallRects = new Rectangle[8];//边框中的八个小圆圈
  Rectangle[] sideRects = new Rectangle[4];//四条边框,用来做响应区域
  Point[] linePoints = new Point[5];//四条边,用于画虚线
  Graphics g; //画图板
  Rectangle ControlRect; //控件包含边框的区域
  #endregion
  #region Methods
  /// <summary>
  /// 加载事件
  /// </summary>
  private void AddEvents()
  {
   this.Name = "FrameControl" + baseControl.Name;
   this.MouseDown += new MouseEventHandler(FrameControl_MouseDown);
   this.MouseMove += new MouseEventHandler(FrameControl_MouseMove);
   this.MouseUp += new MouseEventHandler(FrameControl_MouseUp);
  }
  #region 创建边框
  /// <summary>
  /// 建立控件可视区域
  /// </summary>
  private void CreateBounds()
  {
   //创建边界
   int X = baseControl.Bounds.X - square.Width - 1;
   int Y = baseControl.Bounds.Y - square.Height - 1;
   int Height = baseControl.Bounds.Height + (square.Height * 2) + 2;
   int Width = baseControl.Bounds.Width + (square.Width * 2) + 2;
   this.Bounds = new Rectangle(X, Y, Width, Height);
   this.BringToFront();
   SetRectangles();
   //设置可视区域
   this.Region = new Region(BuildFrame());
   g = this.CreateGraphics();
  }
  /// <summary>
  /// 设置定义8个小矩形的范围
  /// </summary>
  void SetRectangles()
  {
   //左上
   smallRects[0] = new Rectangle(new Point(0, 0), square);
   //右上
   smallRects[1] = new Rectangle(new Point(this.Width - square.Width - 1, 0), square);
   //左下
   smallRects[2] = new Rectangle(new Point(0, this.Height - square.Height - 1), square);
   //右下
   smallRects[3] = new Rectangle(new Point(this.Width - square.Width - 1, this.Height - square.Height - 1), square);
   //上中
   smallRects[4] = new Rectangle(new Point(this.Width / 2 - 1, 0), square);
   //下中
   smallRects[5] = new Rectangle(new Point(this.Width / 2 - 1, this.Height - square.Height - 1), square);
   //左中
   smallRects[6] = new Rectangle(new Point(0, this.Height / 2 - 1), square);
   //右中
   smallRects[7] = new Rectangle(new Point(square.Width + baseControl.Width + 1, this.Height / 2 - 1), square);
   //四条边线
   //左上
   linePoints[0] = new Point(square.Width / 2, square.Height / 2);
   //右上
   linePoints[1] = new Point(this.Width - square.Width / 2 - 1, square.Height / 2);
   //右下
   linePoints[2] = new Point(this.Width - square.Width / 2 - 1, this.Height - square.Height / 2);
   //左下
   linePoints[3] = new Point(square.Width / 2, this.Height - square.Height / 2 - 1);
   //左上
   linePoints[4] = new Point(square.Width / 2, square.Height / 2);
   //整个包括周围边框的范围
   ControlRect = new Rectangle(new Point(0, 0), this.Bounds.Size);
  }
  /// <summary>
  /// 设置边框控件可视区域
  /// </summary>
  /// <returns></returns>
  private GraphicsPath BuildFrame()
  {
   GraphicsPath path = new GraphicsPath();
   //上边框
   sideRects[0] = new Rectangle(0, 0, this.Width - square.Width - 1, square.Height + 1);
   //左边框
   sideRects[1] = new Rectangle(0, square.Height + 1, square.Width + 1, this.Height - square.Height - 1);
   //下边框
   sideRects[2] = new Rectangle(square.Width + 1, this.Height - square.Height - 1, this.Width - square.Width - 1, square.Height + 1);
   //右边框
   sideRects[3] = new Rectangle(this.Width - square.Width - 1, 0, square.Width + 1, this.Height - square.Height - 1);
   path.AddRectangle(sideRects[0]);
   path.AddRectangle(sideRects[1]);
   path.AddRectangle(sideRects[2]);
   path.AddRectangle(sideRects[3]);
   return path;
  }
  #endregion
  /// <summary>
  /// 绘图
  /// </summary>
  public void Draw()
  {
   this.BringToFront();
   Pen pen = new Pen(Color.Black);
   pen.DashStyle = DashStyle.Dot;//设置为虚线,用虚线画四边,模拟微软效果
   g.DrawLines(pen, linePoints);//绘制四条边线
   g.FillRectangles(Brushes.White, smallRects); //填充8个小矩形的内部
   foreach (Rectangle smallRect in smallRects)
   {
    g.DrawEllipse(Pens.Black, smallRect); //绘制8个小椭圆
   }
   //g.DrawRectangles(Pens.Black, smallRects); //绘制8个小矩形的黑色边线
  }
  #endregion
  #region Events
  /// <summary>
  /// 鼠标按下事件:记录当前鼠标相对窗体的坐标
  /// </summary>
  void FrameControl_MouseDown(object sender, MouseEventArgs e)
  {
  }
  /// <summary>
  /// 鼠标移动事件:让控件跟着鼠标移动
  /// </summary>
  void FrameControl_MouseMove(object sender, MouseEventArgs e)
  {
  }
  /// <summary>
  /// 鼠标弹起事件:让自定义的边框出现
  /// </summary>
  void FrameControl_MouseUp(object sender, MouseEventArgs e)
  {
  }
  #endregion
 }
}