📄 recttracker.cs
字号:
namespace Imps.Client.Pc.Controls
{
using System;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
public class RectTracker : IDisposable
{
private bool bFillHandle;
private Bitmap bufferImage;
public DlgtDrawBlendedBufferImage dlgtDrawBlendedBufferImage;
public DlgtDrawImageToForm dlgtDrawImageToForm;
public DlgtDrawOriginalBufferImage dlgtDrawOriginalBufferImage;
public DlgtDrawOriginalImage dlgtDrawOriginalImage;
private TrackerHit draggedHandle;
private string EmptySelectionTipText;
private Brush filledHandleBrush;
private static int handleOffsetX = 3;
private static int handleOffsetY = 3;
private Pen handlePen;
private Rectangle[] handleRects;
private static Size handleSize = new Size(6, 6);
private int height;
private static Size inflateSize = Size.Add(handleSize, new Size(0, 0));
private Size offsetSize;
private Cursor originalCursor;
private Control owner;
private string SelectionTipText;
private Imps.Client.Pc.Controls.SelectStatus selectStatus;
private Size tipSize;
private Pen trackerPen;
private Label trackerTip;
private int width;
private int x;
private int y;
public RectTracker(Control _owner) : this(_owner, null, null, false, null)
{
}
public RectTracker(Control _owner, Pen _trackerPen) : this(_owner, _trackerPen, null, false, null)
{
}
public RectTracker(Control _owner, Pen _trackerPen, Pen _handlePen) : this(_owner, _trackerPen, _handlePen, false, null)
{
}
public RectTracker(Control _owner, Pen _trackerPen, Pen _handlePen, bool _bFillHandle, Brush _filledHandleBrush)
{
this.handleRects = new Rectangle[8];
this.draggedHandle = TrackerHit.hitNothing;
this.handlePen = new Pen(Color.Black, 1f);
this.EmptySelectionTipText = string.Empty;
this.SelectionTipText = string.Empty;
this.tipSize = new Size(200, 30);
this.trackerPen = new Pen(Color.Black, 1f);
this.offsetSize = new Size(0, 0);
if (_owner == null)
{
throw new ArgumentNullException("RectTracker");
}
this.owner = _owner;
this.originalCursor = this.owner.Cursor;
this.trackerTip = new Label();
this.trackerTip.set_AutoSize(true);
this.trackerTip.BackColor = SystemColors.Info;
this.trackerTip.Text = string.Empty;
this.trackerTip.BorderStyle = BorderStyle.FixedSingle;
this.trackerTip.TextAlign = ContentAlignment.MiddleLeft;
this.trackerTip.set_UseCompatibleTextRendering(true);
this.trackerTip.Visible = false;
this.owner.Controls.Add(this.trackerTip);
this.EmptySelectionTipText = "1、按住鼠标左键,选取截屏区域\r\n2、按ESC键或鼠标右键退出截屏功能";
this.SelectionTipText = "拖动节点可调整选择区域\r\n双击鼠标左键,确认发送截屏区域\r\n按ESC键或鼠标右键取消选择区域";
this.InitilizeTracker();
this.owner.Paint += new PaintEventHandler(this.DoPaint);
this.owner.MouseMove += new MouseEventHandler(this.DoMouseMove);
this.owner.MouseDown += new MouseEventHandler(this.DoMouseDown);
this.owner.MouseUp += new MouseEventHandler(this.DoMouseUp);
this.owner.MouseLeave += new EventHandler(this.DoMouseLeave);
if (_trackerPen != null)
{
this.TrackerPen = _trackerPen;
}
if (_handlePen != null)
{
this.HandlePen = _handlePen;
}
if (_filledHandleBrush != null)
{
this.FilledHandleBrush = _filledHandleBrush;
}
this.bFillHandle = _bFillHandle;
}
private void DirectPaintTracker(Point offsetPoint, Graphics g)
{
if (!this.SelectionIsEmpty())
{
Rectangle rect = new Rectangle(offsetPoint, new Size(Math.Abs(this.TrueRect.Width), Math.Abs(this.TrueRect.Height)));
g.DrawRectangle(this.TrackerPen, rect);
this.RefreshHandleRects(rect);
g.DrawRectangles(this.handlePen, this.handleRects);
if (this.bFillHandle)
{
g.FillRectangles(this.FilledHandleBrush, this.handleRects);
}
}
}
public virtual void Dispose()
{
if (this.filledHandleBrush != null)
{
this.filledHandleBrush.Dispose();
}
if (this.handlePen != null)
{
this.handlePen.Dispose();
}
if (this.trackerPen != null)
{
this.trackerPen.Dispose();
}
}
private void DoMouseDown(object sender, MouseEventArgs e)
{
this.OnMouseDown(e);
}
private void DoMouseHover(object sender, MouseEventArgs e)
{
}
private void DoMouseLeave(object sender, EventArgs e)
{
this.OnMouseLeave(e);
}
private void DoMouseMove(object sender, MouseEventArgs e)
{
this.OnMouseMove(e);
}
private void DoMouseUp(object sender, MouseEventArgs e)
{
this.OnMouseUp(e);
}
private void DoPaint(object sender, PaintEventArgs e)
{
if (Imps.Client.Pc.Controls.SelectStatus.Selected == this.SelectStatus)
{
try
{
if (e.ClipRectangle.IntersectsWith(this.InvalidatedRect))
{
this.OnPaintBackground(e);
this.OnPaint(e);
}
}
catch (Exception exception)
{
Trace.WriteLine(exception.ToString());
}
}
}
public TrackerHit HitTest(Point testPoint)
{
for (int i = 0; i < 8; i++)
{
if (this.handleRects[i].Contains(testPoint))
{
return (TrackerHit) i;
}
}
if (this.TrueRect.Contains(testPoint))
{
return TrackerHit.hitMiddle;
}
return TrackerHit.hitNothing;
}
public void InitilizeTracker()
{
this.selectStatus = Imps.Client.Pc.Controls.SelectStatus.Empty;
this.TrueRect = Rectangle.Empty;
this.RefreshHandleRects(this.TrueRect);
this.offsetSize = new Size(0, 0);
this.draggedHandle = TrackerHit.hitNothing;
this.SetCursor(this.draggedHandle);
}
public void Invalidate()
{
this.Invalidate(this.InvalidatedRect);
}
public void Invalidate(Rectangle rect)
{
if ((this.owner != null) && (Rectangle.Empty != rect))
{
this.owner.Invalidate(rect);
}
}
protected virtual void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.trackerTip.Hide();
CaptureHelper.UpdateWindow(this.owner.Handle);
if (this.selectStatus == Imps.Client.Pc.Controls.SelectStatus.Empty)
{
this.selectStatus = Imps.Client.Pc.Controls.SelectStatus.Selecting;
this.SetBounds(e.X, e.Y, 0, 0);
}
else if (Imps.Client.Pc.Controls.SelectStatus.Selected == this.selectStatus)
{
this.draggedHandle = this.HitTest(new Point(e.X, e.Y));
if (TrackerHit.hitNothing != this.draggedHandle)
{
this.selectStatus = Imps.Client.Pc.Controls.SelectStatus.Selecting;
this.offsetSize = new Size(e.X - this.x, e.Y - this.y);
}
}
}
}
protected virtual void OnMouseHover(MouseEventArgs e)
{
}
protected virtual void OnMouseLeave(EventArgs e)
{
this.owner.Cursor = this.originalCursor;
this.trackerTip.Hide();
}
protected virtual void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (Imps.Client.Pc.Controls.SelectStatus.Selecting != this.selectStatus)
{
return;
}
Rectangle rect = new Rectangle();
rect = this.InvalidatedRect;
switch (this.draggedHandle)
{
case TrackerHit.hitNothing:
this.SetBounds(this.x, this.y, e.X - this.x, e.Y - this.y);
break;
case TrackerHit.hitTopLeft:
this.SetBounds(e.X, e.Y, this.TrueRect.Right - e.X, this.TrueRect.Bottom - e.Y);
break;
case TrackerHit.hitTopRight:
this.SetBounds(this.x, e.Y, e.X - this.x, this.TrueRect.Bottom - e.Y);
break;
case TrackerHit.hitBottomRight:
this.SetBounds(this.x, this.y, e.X - this.x, e.Y - this.y);
break;
case TrackerHit.hitBottomLeft:
this.SetBounds(e.X, this.y, this.TrueRect.Right - e.X, e.Y - this.TrueRect.Top);
break;
case TrackerHit.hitTop:
this.SetBounds(this.x, e.Y, this.width, this.TrueRect.Bottom - e.Y);
break;
case TrackerHit.hitRight:
this.SetBounds(this.x, this.y, e.X - this.x, this.height);
break;
case TrackerHit.hitBottom:
this.SetBounds(this.x, this.y, this.width, e.Y - this.y);
break;
case TrackerHit.hitLeft:
this.SetBounds(e.X, this.y, this.TrueRect.Right - e.X, this.height);
break;
case TrackerHit.hitMiddle:
this.SetBounds(e.X - this.offsetSize.Width, e.Y - this.offsetSize.Height, this.width, this.height);
break;
}
Rectangle bufferRect = new Rectangle();
bufferRect = this.InvalidatedRect;
if (bufferRect.Contains(rect))
{
this.bufferImage = new Bitmap(bufferRect.Width, bufferRect.Height);
using (Graphics g = Graphics.FromImage(this.bufferImage))
{
if (this.dlgtDrawBlendedBufferImage != null)
{
this.dlgtDrawBlendedBufferImage(bufferRect, g);
}
Point offsetPoint = new Point(0, 0);
offsetPoint.X = handleOffsetX;
offsetPoint.Y = handleOffsetY;
if (this.dlgtDrawOriginalBufferImage != null)
{
Rectangle trueRect = new Rectangle();
trueRect = this.TrueRect;
CaptureHelper.NormalizeRect(ref trueRect);
this.dlgtDrawOriginalBufferImage(trueRect, offsetPoint, g);
}
this.DirectPaintTracker(offsetPoint, g);
if (this.dlgtDrawImageToForm != null)
{
this.dlgtDrawImageToForm(bufferRect, this.bufferImage);
}
return;
}
}
if (rect.Contains(bufferRect))
{
this.bufferImage = new Bitmap(rect.Width, rect.Height);
using (Graphics graphics2 = Graphics.FromImage(this.bufferImage))
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -