代码
internal enum SizeGrip
{
None = 0,
Top,
Bottom,
Left,
Right,
TopLeft,
TopRight,
BottomLeft,
BottomRight,
All
}
internal class OperateObject
{
private OperateType _operateType;
private Color _color;
private object _data;
public OperateObject() {
}
public OperateObject(
OperateType operateType, Color color, object data)
{
_operateType = operateType;
_color = color;
_data = data;
}
public OperateType OperateType
{
get {
return _operateType; }
set {
_operateType = value; }
}
public Color Color
{
get {
return _color; }
set {
_color = value; }
}
public object Data
{
get {
return _data; }
set {
_data = value; }
}
}
internal enum OperateType
{
None = 0,
DrawRectangle,
DrawEllipse,
DrawArrow,
DrawLine,
DrawText
}
internal class OperateManager : IDisposable
{
private List<OperateObject> _operateList;
private static readonly int MaxOperateCount = 1000;
public OperateManager()
{
}
public List<OperateObject> OperateList
{
get
{
if (_operateList == null)
{
_operateList = new List<OperateObject>(100);
}
return _operateList;
}
}
public int OperateCount
{
get {
return OperateList.Count; }
}
public void AddOperate(
OperateType operateType,
Color color,
object data)
{
OperateObject obj = new OperateObject(
operateType, color, data);
if (OperateList.Count > MaxOperateCount)
{
OperateList.RemoveAt(0);
}
OperateList.Add(obj);
}
public bool RedoOperate()
{
if (OperateList.Count > 0)
{
OperateList.RemoveAt(OperateList.Count - 1);
return true;
}
return false;
}
public void Clear()
{
OperateList.Clear();
}
#region IDisposable 成员
public void Dispose()
{
if (_operateList != null)
{
_operateList.Clear();
_operateList = null;
}
}
#endregion
}
public enum DrawStyle
{
None = 0,
Rectangle,
Ellipse,
Arrow,
Text,
Line
}
internal class DrawTextData
{
private string _text;
private Font _font;
private Rectangle _textRect;
private bool _completed;
public DrawTextData() {
}
public DrawTextData(string text, Font font, Rectangle textRect)
{
_text = text;
_font = font;
_textRect = textRect;
}
public string Text
{
get {
return _text; }
set {
_text = value; }
}
public Font Font
{
get {
return _font; }
set {
_font = value; }
}
public Rectangle TextRect
{
get {
return _textRect; }
set {
_textRect = value; }
}
public bool Completed
{
get {
return _completed; }
set {
_completed = value; }
}
}
internal class NativeMethods
{
public const int WS_EX_TRANSPARENT = 0x00000020;
[DllImport("user32.dll")]
public static extern bool ClipCursor(ref RECT lpRect);
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr ptr);
[DllImport("user32.dll")]
public static extern int ReleaseDC(IntPtr hwnd, IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool BitBlt(
IntPtr hObject, int nXDest, int nYDest, int nWidth,
int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc,
TernaryRasterOperations dwRop);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr LoadLibrary(string lpFileName);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public RECT(int left, int top, int right, int bottom)
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}
public RECT(Rectangle rect)
{
Left = rect.Left;
Top = rect.Top;
Right = rect.Right;
Bottom = rect.Bottom;
}
public Rectangle Rect
{
get
{
return new Rectangle(
Left,
Top,
Right - Left,
Bottom - Top);
}
}
public Size Size
{
get
{
return new Size(Right - Left, Bottom - Top);
}
}
public static RECT FromXYWH(int x, int y, int width, int height)
{
return new RECT(x,
y,
x + width,
y + height);
}
public static RECT FromRectangle(Rectangle rect)
{
return new RECT(rect.Left,
rect.Top,
rect.Right,
rect.Bottom);
}
}
public enum TernaryRasterOperations
{
SRCCOPY = 0x00CC0020,
SRCPAINT = 0x00EE0086,
SRCAND = 0x008800C6,
SRCINVERT = 0x00660046,
SRCERASE = 0x00440328,
NOTSRCCOPY = 0x00330008,
NOTSRCERASE = 0x001100A6,
MERGECOPY = 0x00C000CA,
MERGEPAINT = 0x00BB0226,
PATCOPY = 0x00F00021,
PATPAINT = 0x00FB0A09,
PATINVERT = 0x005A0049,
DSTINVERT = 0x00550009,
BLACKNESS = 0x00000042,
WHITENESS = 0x00FF0062,
}
}
public class CaptureImageTool : Form
{
private static SolidBrush mask = new SolidBrush(Color.FromArgb(100, 0, 0, 0));
private Image ScreenImage;
#region Fields
private Image _image;
private CaptureImageToolColorTable _colorTable;
private Cursor _selectCursor = Cursors.Default;
private Cursor _drawCursor = Cursors.Cross;
private Point _mouseDownPoint;
private Point _endPoint;
private bool _mouseDown;
private Rectangle _selectImageRect;
private Rectangle _selectImageBounds;
private bool _selectedImage;
private SizeGrip _sizeGrip;
private Dictionary<SizeGrip, Rectangle> _sizeGripRectList;
private OperateManager _operateManager;
private List<Point> _linePointList;
private static readonly Font TextFont =
new Font("Times New Roman", 12F, FontStyle.Bold, GraphicsUnit.Point, 0);
#endregion
#region Constructors
public CaptureImageTool()
{
Init();
KeyUp += CaptureImageTool_KeyUp;
}
#endregion
#region Properties
[Browsable(false)]
[