|
|
using System;using System.Collections.Generic;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;
namespace Customize.Controls{ public partial class ImageMask : System.Windows.Forms.Control { public string fileName;
public ImageMask() { this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); //this.BackColor = Color.Empty;
this.BackColor = Color.Transparent; //this.BackColor = Color.FromArgb(50, Color.Red);
//以下是设置双缓冲的代码 要不然在下面onPaint里的绘制半透明背景会让你很伤心,或者上面那句设置半透明背景用了过后拖动时也会让你跟蜗牛一样。
this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
this.SetStyle(ControlStyles.DoubleBuffer, true); }
public Size border;
//public void showBorder() { }
private Point m_MousePoint; private Size m_Size; private Point m_LastPoint;
protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); this.m_LastPoint = this.Location; this.m_MousePoint = this.PointToScreen(e.Location); this.m_Size = this.Size; //this.BackColor = Color.Empty;
}
protected override void OnMouseMove(MouseEventArgs e) {
base.OnMouseMove(e);
if (arow != arrowType.none) { reSize(e); return; }
if (e.Y <= recArrow[4].Bottom && e.Y >= recArrow[4].Top && e.X >= recArrow[4].Left && e.X <= recArrow[4].Right)//左上
Cursor = Cursors.SizeNWSE; else if (e.Y <= recArrow[5].Bottom && e.Y >= recArrow[5].Top && e.X >= recArrow[5].Left && e.X <= recArrow[5].Right)//左下
Cursor = Cursors.SizeNESW; else if (e.Y <= recArrow[6].Bottom && e.Y >= recArrow[6].Top && e.X >= recArrow[6].Left && e.X <= recArrow[6].Right)//右上
Cursor = Cursors.SizeNESW; else if (e.Y <= recArrow[7].Bottom && e.Y >= recArrow[7].Top && e.X >= recArrow[7].Left && e.X <= recArrow[7].Right)//右下
Cursor = Cursors.SizeNWSE; else if (e.Y <= recArrow[0].Bottom && e.Y >= recArrow[0].Top)//上
Cursor = Cursors.SizeNS; else if (e.Y <= recArrow[1].Bottom && e.Y >= recArrow[1].Top)//下
Cursor = Cursors.SizeNS; else if (e.X >= recArrow[2].Left && e.X <= recArrow[2].Right)//左
Cursor = Cursors.SizeWE; else if (e.X >= recArrow[3].Left && e.X <= recArrow[3].Right)//右
Cursor = Cursors.SizeWE; else Cursor = Cursors.SizeAll;
if (e.Button == MouseButtons.Left) { Point t = this.PointToScreen(e.Location); Point l = this.m_LastPoint;
if (e.Y <= recArrow[4].Bottom && e.Y >= recArrow[4].Top && e.X >= recArrow[4].Left && e.X <= recArrow[4].Right)//左上
arow = arrowType.leftUp; else if (e.Y <= recArrow[5].Bottom && e.Y >= recArrow[5].Top && e.X >= recArrow[5].Left && e.X <= recArrow[5].Right)//左下
arow = arrowType.leftDown; else if (e.Y <= recArrow[6].Bottom && e.Y >= recArrow[6].Top && e.X >= recArrow[6].Left && e.X <= recArrow[6].Right)//右上
arow = arrowType.rightUp; else if (e.Y <= recArrow[7].Bottom && e.Y >= recArrow[7].Top && e.X >= recArrow[7].Left && e.X <= recArrow[7].Right)//右下
arow = arrowType.rightDown; else if (e.Y <= recArrow[0].Bottom && e.Y >= recArrow[0].Top)//上
arow = arrowType.up; else if (e.Y <= recArrow[1].Bottom && e.Y >= recArrow[1].Top)//下
arow = arrowType.down; else if (e.X >= recArrow[2].Left && e.X <= recArrow[2].Right)//左
arow = arrowType.left; else if (e.X >= recArrow[3].Left && e.X <= recArrow[3].Right)//右
arow = arrowType.right; else arow = arrowType.none;
l.Offset(t.X - this.m_MousePoint.X, t.Y - this.m_MousePoint.Y); if (arow != arrowType.none) reSize(e); else { this.Location = l; Refresh();//这句很重要立即重绘 不然拖动到时候会出现卡卡 的现象 ,找了半天原因
} } }
public void reSize(MouseEventArgs e) { Point t = this.PointToScreen(e.Location); Point l = this.m_LastPoint;
l.Offset(t.X - this.m_MousePoint.X, t.Y - this.m_MousePoint.Y);
switch (arow) { case arrowType.up: { this.Height = m_Size.Height - (t.Y - this.m_MousePoint.Y); this.Location = new Point(m_LastPoint.X, l.Y); break; } case arrowType.down: { this.Height = m_Size.Height + (t.Y - this.m_MousePoint.Y); break; } case arrowType.left: { this.Width = m_Size.Width - (t.X - this.m_MousePoint.X); this.Location = new Point(l.X, m_LastPoint.Y); break; } case arrowType.right: { this.Width = m_Size.Width + (t.X - this.m_MousePoint.X); break; } case arrowType.leftUp: { this.Width = m_Size.Width - (t.X - this.m_MousePoint.X); this.Height = m_Size.Height - (t.Y - this.m_MousePoint.Y); this.Location = new Point(l.X, l.Y); break; } case arrowType.leftDown: { this.Width = m_Size.Width - (t.X - this.m_MousePoint.X); this.Height = m_Size.Height + (t.Y - this.m_MousePoint.Y); this.Location = new Point(l.X, m_LastPoint.Y); break; } case arrowType.rightUp: { this.Width = m_Size.Width + (t.X - this.m_MousePoint.X); this.Height = m_Size.Height - (t.Y - this.m_MousePoint.Y); this.Location = new Point(m_LastPoint.X, l.Y); break; } case arrowType.rightDown: { this.Width = m_Size.Width + (t.X - this.m_MousePoint.X); this.Height = m_Size.Height + (t.Y - this.m_MousePoint.Y); break; } } this.Refresh(); }
public enum arrowType { up, down, left, right, leftUp, leftDown, rightUp, rightDown, none }
public arrowType arow = arrowType.none;
Rectangle[] recArrow = new Rectangle[8];//8个手柄
public Rectangle area;//选择区域
public readonly int blank = 8;//边距
protected override void OnPaint(PaintEventArgs e) { int side = 6;//手柄矩形的边长
recArrow[0] = new Rectangle(new Point(this.Width / 2 - side / 2, blank - side / 2), new Size(side, side)); recArrow[1] = new Rectangle(new Point(this.Width / 2 - side / 2, this.Height - blank - side / 2), new Size(side, side)); recArrow[2] = new Rectangle(new Point(blank - side / 2, this.Height / 2 - side / 2), new Size(side, side)); recArrow[3] = new Rectangle(new Point(this.Width - blank - side / 2, this.Height / 2 - side / 2), new Size(side, side)); recArrow[4] = new Rectangle(new Point(blank - side / 2, blank - side / 2), new Size(side, side)); recArrow[5] = new Rectangle(new Point(blank - side / 2, this.Height - blank - side / 2), new Size(side, side)); recArrow[6] = new Rectangle(new Point(this.Width - blank - side / 2, blank - side / 2), new Size(side, side)); recArrow[7] = new Rectangle(new Point(this.Width - blank - side / 2, this.Height - blank - side / 2), new Size(side, side));
foreach (Rectangle item in recArrow) e.Graphics.DrawRectangle(Pens.Red, item); area = new Rectangle(new Point(8, 8), new Size(this.Width - 8 * 2, this.Height - 8 * 2));
////加上半透明效果看倒是好看了 重绘的时候卡的凶 ,期待有解决方法
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; System.Drawing.Color cor = System.Drawing.Color.FromArgb(50, Color.Red); System.Drawing.SolidBrush bsh = new System.Drawing.SolidBrush(cor); e.Graphics.FillRectangle(bsh, area);
e.Graphics.DrawRectangle(Pens.Red, area); }
protected override void OnMouseUp(MouseEventArgs e) { arow = arrowType.none; //this.BackColor = Color.FromArgb(0, Color.Red);
}
}}
|