0
点赞
收藏
分享

微信扫一扫

c#自定义可拖动变形控件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace util
{
public class 控件移动变形类 : UserControl //控件类
{
Rectangle 传递控件;//传递控件相对于本控件的范围
Rectangle 本控件;//本控件相对于自己的范围
Rectangle[] 调节点边框 = new Rectangle[8];//8个点相对于本控件的范围
Rectangle[] 调节框边框 = new Rectangle[4];//4个调节框相对于本控件的范围,封闭曲线
Size 调节点大小 = new Size(6, 6);
Graphics g;
Control 当前控件;
//保存鼠标单击的位置,以备释放鼠标时计算距离
Point prevLeftClick;
bool 已经绘出 = false;

enum 调节点列表
{
HDS_NONE = 0,
HDS_TOP = 1,
HDS_RIGHT = 2,
HDS_BOTTOM = 3,
HDS_LEFT = 4,
HDS_TOPLEFT = 5,
HDS_TOPRIGHT = 6,
HDS_BOTTOMLEFT = 7,
HDS_BOTTOMRIGHT = 8
}
private 调节点列表 CurrHitPlace;
public 控件移动变形类(Control theControl)
{
InitializeComponent();
当前控件 = theControl;
Create();
}
private void InitializeComponent()
{
this.BackColor = System.Drawing.Color.Wheat;
this.Name = "TestMoveAndResizeControl";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.RectTracker_Paint);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.RectTracker_MouseMove);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.RectTracker_MouseUp);
}
private void Create()
{
//创建边界
int X = 当前控件.Bounds.X - 调节点大小.Width;
int Y = 当前控件.Bounds.Y - 调节点大小.Height;
int Height = 当前控件.Bounds.Height + (调节点大小.Height * 2);
int Width = 当前控件.Bounds.Width + (调节点大小.Width * 2);
this.Bounds = new Rectangle(X, Y, Width + 1, Height + 1);//本控件相对于窗体的范围
this.BringToFront();
Rect = 当前控件.Bounds;
//设置可视区域
this.Region = new Region(创建包含区域());
g = this.CreateGraphics();//创建画笔
控件重画();//控件显示
}
public Rectangle Rect
{
get { return 传递控件; }
set
{
int X = 调节点大小.Width;
int Y = 调节点大小.Height;
int Height = value.Height;
int Width = value.Width;
传递控件 = new Rectangle(X, Y, Width, Height);
SetRectangles();
}
}

void SetRectangles()
{
//定义8个小正方形的范围
//左上
调节点边框[0] = new Rectangle(new Point(传递控件.X - 调节点大小.Width, 传递控件.Y - 调节点大小.Height), 调节点大小);
//上中间
调节点边框[4] = new Rectangle(new Point(传递控件.X + (传递控件.Width / 2) - (调节点大小.Width / 2), 传递控件.Y - 调节点大小.Height), 调节点大小);
//右上
调节点边框[1] = new Rectangle(new Point(传递控件.X + 传递控件.Width, 传递控件.Y - 调节点大小.Height), 调节点大小);
//左下
调节点边框[2] = new Rectangle(new Point(传递控件.X - 调节点大小.Width, 传递控件.Y + 传递控件.Height), 调节点大小);
//下中间
调节点边框[5] = new Rectangle(new Point(传递控件.X + (传递控件.Width / 2) - (调节点大小.Width / 2), 传递控件.Y + 传递控件.Height), 调节点大小);
//右下
调节点边框[3] = new Rectangle(new Point(传递控件.X + 传递控件.Width, 传递控件.Y + 传递控件.Height), 调节点大小);
//左中间
调节点边框[6] = new Rectangle(new Point(传递控件.X - 调节点大小.Width, 传递控件.Y + (传递控件.Height / 2) - (调节点大小.Height / 2)), 调节点大小);
//右中间
调节点边框[7] = new Rectangle(new Point(传递控件.X + 传递控件.Width, 传递控件.Y + (传递控件.Height / 2) - (调节点大小.Height / 2)), 调节点大小);

//整个包括周围边框的范围
本控件 = new Rectangle(new Point(0, 0), this.Bounds.Size);
}
private GraphicsPath 创建包含区域()
{
GraphicsPath path = new GraphicsPath();
调节框边框[0] = new Rectangle(0, 0, 当前控件.Width + (调节点大小.Width * 2) + 1, 调节点大小.Height + 1);
调节框边框[1] = new Rectangle(0, 调节点大小.Height + 1, 调节点大小.Width + 1, 当前控件.Bounds.Height + 调节点大小.Height + 1);
调节框边框[2] = new Rectangle(调节点大小.Width + 1, 当前控件.Bounds.Height + 调节点大小.Height - 1, 当前控件.Width + 调节点大小.Width + 2, 调节点大小.Height + 2);
调节框边框[3] = new Rectangle(当前控件.Width + 调节点大小.Width - 1, 调节点大小.Height + 1, 调节点大小.Width + 2, 当前控件.Height - 1);
path.AddRectangle(调节框边框[0]);
path.AddRectangle(调节框边框[1]);
path.AddRectangle(调节框边框[2]);
path.AddRectangle(调节框边框[3]);
return path;
}
public void 控件重画()
{
try
{
g.FillRectangles(Brushes.LightGray, 调节框边框); //填充用于调整的边框的内部
g.FillRectangles(Brushes.White, 调节点边框); //填充8个锚点的内部
g.DrawRectangles(Pens.Black, 调节点边框); //绘制8个锚点的黑色边线
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}


private void RectTracker_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (已经绘出 == false)
{
prevLeftClick = new Point(e.X, e.Y);
已经绘出 = true;
}
else
{
this.Visible = false;
Mouse_Move(this, e); //调整位置或大小
prevLeftClick = new Point(e.X, e.Y);
}
}
else
{
已经绘出 = false;
this.Visible = true;
更改指针样式(e.X, e.Y); //更新鼠标指针样式
}
}

private void RectTracker_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Create();
this.Visible = true;
}
private void RectTracker_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//画边框
控件重画();
}
public bool 更改指针样式(int x, int y)
{
Point point = new Point(x, y);
if (!本控件.Contains(point))
{
Cursor.Current = Cursors.Arrow;
return false;
}
else if (调节点边框[0].Contains(point))
{
Cursor.Current = Cursors.SizeNWSE;
CurrHitPlace = 调节点列表.HDS_TOPLEFT;
}
else if (调节点边框[3].Contains(point))
{
Cursor.Current = Cursors.SizeNWSE;
CurrHitPlace = 调节点列表.HDS_BOTTOMRIGHT;
}
else if (调节点边框[1].Contains(point))
{
Cursor.Current = Cursors.SizeNESW;
CurrHitPlace = 调节点列表.HDS_TOPRIGHT;
}
else if (调节点边框[2].Contains(point))
{
Cursor.Current = Cursors.SizeNESW;
CurrHitPlace = 调节点列表.HDS_BOTTOMLEFT;
}
else if (调节点边框[4].Contains(point))
{
Cursor.Current = Cursors.SizeNS;
CurrHitPlace = 调节点列表.HDS_TOP;
}
else if (调节点边框[5].Contains(point))
{
Cursor.Current = Cursors.SizeNS;
CurrHitPlace = 调节点列表.HDS_BOTTOM;
}
else if (调节点边框[6].Contains(point))
{
Cursor.Current = Cursors.SizeWE;
CurrHitPlace = 调节点列表.HDS_LEFT;
}
else if (调节点边框[7].Contains(point))
{
Cursor.Current = Cursors.SizeWE;
CurrHitPlace = 调节点列表.HDS_RIGHT;
}
else if (本控件.Contains(point))
{
Cursor.Current = Cursors.SizeAll;
CurrHitPlace = 调节点列表.HDS_NONE;
}
return true;
}
public void Mouse_Move(object sender, System.Windows.Forms.MouseEventArgs e)
{
//控件最小为 8x8
if (当前控件.Height < 8)
{
当前控件.Height = 8;
return;
}
else if (当前控件.Width < 8)
{
当前控件.Width = 8;
return;
}
switch (this.CurrHitPlace)
{
case 调节点列表.HDS_TOP:
当前控件.Height = 当前控件.Height - e.Y + prevLeftClick.Y;
if (当前控件.Height > 8)
当前控件.Top = 当前控件.Top + e.Y - prevLeftClick.Y;
break;
case 调节点列表.HDS_TOPLEFT:
当前控件.Height = 当前控件.Height - e.Y + prevLeftClick.Y;
if (当前控件.Height > 8)
当前控件.Top = 当前控件.Top + e.Y - prevLeftClick.Y;
当前控件.Width = 当前控件.Width - e.X + prevLeftClick.X;
if (当前控件.Width > 8)
当前控件.Left = 当前控件.Left + e.X - prevLeftClick.X;
break;
case 调节点列表.HDS_TOPRIGHT:
当前控件.Height = 当前控件.Height - e.Y + prevLeftClick.Y;
if (当前控件.Height > 8)
当前控件.Top = 当前控件.Top + e.Y - prevLeftClick.Y;
当前控件.Width = 当前控件.Width + e.X - prevLeftClick.X;
break;
case 调节点列表.HDS_RIGHT:
当前控件.Width = 当前控件.Width + e.X - prevLeftClick.X;
break;
case 调节点列表.HDS_BOTTOM:
当前控件.Height = 当前控件.Height + e.Y - prevLeftClick.Y;
break;
case 调节点列表.HDS_BOTTOMLEFT:
当前控件.Height = 当前控件.Height + e.Y - prevLeftClick.Y;
当前控件.Width = 当前控件.Width - e.X + prevLeftClick.X;
if (当前控件.Width > 8)
当前控件.Left = 当前控件.Left + e.X - prevLeftClick.X;
break;
case 调节点列表.HDS_BOTTOMRIGHT:
当前控件.Height = 当前控件.Height + e.Y - prevLeftClick.Y;
当前控件.Width = 当前控件.Width + e.X - prevLeftClick.X;
break;
case 调节点列表.HDS_LEFT:
当前控件.Width = 当前控件.Width - e.X + prevLeftClick.X;
if (当前控件.Width > 8)
当前控件.Left = 当前控件.Left + e.X - prevLeftClick.X;
break;
case 调节点列表.HDS_NONE:
当前控件.Location = new Point(当前控件.Location.X + e.X - prevLeftClick.X, 当前控件.Location.Y + e.Y - prevLeftClick.Y);
break;
}
}

}
}


举报

相关推荐

0 条评论