📄 mousecaptureform.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace CaptureScreen
{
/// <summary>
/// MouseCaptureForm 的摘要说明。
/// </summary>
public partial class MouseCaptureForm : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
//
//自定义变量
//
private Point pot;
private Rectangle area = Rectangle.Empty;
private Image img;
private int index = -1;
private string noselectString = "\n.请按下鼠标左键不放拖动选取截图区域\n\n .单击鼠标右键或按ESC取消截图";
private string selectingString = "\n.松开鼠标左键确定选取范围\n\n.按ESC重新选择";
private string selectedString = "\n.按鼠标左键调整选择范围\n\n .双击鼠标左键保存截图 \n\n.按鼠标右键重新选择 \n\n.ESC键取消截图";
private System.Windows.Forms.Label tiptext;
//
//系统生成定义
//
private System.ComponentModel.Container components = null;
public MouseCaptureForm()
{
this.Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
this.BackgroundImage = CaptureScreenForm.windowFullScreen();
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.tiptext = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// tiptext
//
this.tiptext.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
this.tiptext.ForeColor = System.Drawing.Color.White;
this.tiptext.Location = new System.Drawing.Point(10, 10);
this.tiptext.Name = "tiptext";
this.tiptext.Size = new System.Drawing.Size(166, 140);
this.tiptext.TabIndex = 0;
this.tiptext.MouseMove += new System.Windows.Forms.MouseEventHandler(this.tiptext_MouseMove);
//
// MouseCaptureForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(456, 320);
this.Controls.Add(this.tiptext);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "MouseCaptureForm";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultBounds;
this.Text = "鼠标截屏";
this.TopMost = true;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.MouseCaptureForm_KeyDown);
this.Load += new System.EventHandler(this.MouseCaptureForm_Load);
this.DoubleClick += new System.EventHandler(this.MouseCaptureForm_DoubleClick);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 重载MouseDown
/// </summary>
///
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
{
if (this.area == Rectangle.Empty && e.Button == MouseButtons.Left)
{
this.tiptext.Text = selectingString;
this.area.Location = new Point(e.X, e.Y);
}
this.pot = new Point(e.X, e.Y);
this.index = this.GetSelectedHandle(new Point(e.X, e.Y));
this.SetCursor();
}
}
/// <summary>
/// 设置鼠标方案
/// </summary>
private void SetCursor()
{
Cursor cr = Cursors.Default;
if (index == 1 || index == 5)
{
cr = Cursors.SizeNWSE;
}
else if (index == 2 || index == 6)
{
cr = Cursors.SizeNS;
}
else if (index == 3 || index == 7)
{
cr = Cursors.SizeNESW;
}
else if (index == 4 || index == 8)
{
cr = Cursors.SizeWE;
}
else if (index == 0)
{
cr = Cursors.SizeAll;
}
Cursor.Current = cr;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawRectangle(new Pen(this.ForeColor), this.area);
for (int i = 1; i < 9; i++)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Green), this.GetHandleRect(i));
}
}
private Rectangle GetHandleRect(int index)
{
Point point = GetHandle(index);
return new Rectangle(point.X - 3, point.Y - 3, 6, 6);
}
private Point GetHandle(int index)
{
int x, y, xCenter, yCenter;
xCenter = area.X + area.Width / 2;
yCenter = area.Y + area.Height / 2;
x = area.X;
y = area.Y;
switch (index)
{
case 1:
x = area.X;
y = area.Y;
break;
case 2:
x = xCenter;
y = area.Y;
break;
case 3:
x = area.Right;
y = area.Y;
break;
case 4:
x = area.Right;
y = yCenter;
break;
case 5:
x = area.Right;
y = area.Bottom;
break;
case 6:
x = xCenter;
y = area.Bottom;
break;
case 7:
x = area.X;
y = area.Bottom;
break;
case 8:
x = area.X;
y = yCenter;
break;
}
return new Point(x, y);
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
int left = area.Left;
int top = area.Top;
int right = area.Right;
int bottom = area.Bottom;
area.X = Math.Min(left, right);
area.Y = Math.Min(top, bottom);
area.Width = Math.Abs(left - right);
area.Height = Math.Abs(top - bottom);
this.tiptext.Text = selectedString;
if (e.Button == MouseButtons.Right)
{
if (this.area == Rectangle.Empty)
{
this.Close();
}
else
{
this.area = Rectangle.Empty;
this.Invalidate();
}
}
this.index = this.GetSelectedHandle(new Point(e.X, e.Y));
this.SetCursor();
}
private int GetSelectedHandle(Point p)
{
int index = -1;
for (int i = 1; i < 9; i++)
{
if (GetHandleRect(i).Contains(p))
{
index = i;
break;
}
}
if (this.area.Contains(p)) index = 0;
return index;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (this.Capture)
{
this.MoveHandleTo(new Point(e.X, e.Y));
this.Invalidate();
}
else
{
this.index = this.GetSelectedHandle(new Point(e.X, e.Y));
this.SetCursor();
}
}
private void MoveHandleTo(Point point)
{
int left = area.Left;
int top = area.Top;
int right = area.Right;
int bottom = area.Bottom;
switch (index)
{
case 0:
area.X += point.X - this.pot.X;
area.Y += point.Y - pot.Y;
this.pot = point;
return;
case 1:
left = point.X;
top = point.Y;
break;
case 2:
top = point.Y;
break;
case 3:
right = point.X;
top = point.Y;
break;
case 4:
right = point.X;
break;
case 5:
right = point.X;
bottom = point.Y;
break;
case 6:
bottom = point.Y;
break;
case 7:
left = point.X;
bottom = point.Y;
break;
case 8:
left = point.X;
break;
}
this.pot = point;
area.X = left;
area.Y = top;
area.Width = right - left;
area.Height = bottom - top;
}
private void MouseCaptureForm_DoubleClick(object sender, System.EventArgs e)
{
//修正截取图片过界提示内存不足BUG
int left = area.Left;
int right = area.Right;
int top = area.Top;
int bottom = area.Bottom;
if (left < Screen.PrimaryScreen.Bounds.Left)
{ left = Screen.PrimaryScreen.Bounds.Left; }
if (right > Screen.PrimaryScreen.Bounds.Right)
{ right = Screen.PrimaryScreen.Bounds.Right; }
if (top < Screen.PrimaryScreen.Bounds.Top)
{ top = Screen.PrimaryScreen.Bounds.Top; }
if (bottom > Screen.PrimaryScreen.Bounds.Bottom)
{ bottom = Screen.PrimaryScreen.Bounds.Bottom; }
area.X = left;
area.Y = top;
area.Width = right - left;
area.Height = bottom - top;
//截取选择区域图片
Bitmap bm = new Bitmap(this.BackgroundImage);
this.img = bm.Clone(this.area, System.Drawing.Imaging.PixelFormat.Format16bppArgb1555);
CaptureScreenForm.image = img;
CaptureScreenForm.ActiveForm.Invalidate();
this.Close();
}
private void MouseCaptureForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyData == Keys.Escape && System.Windows.Forms.Button.MouseButtons == MouseButtons.Left)
{
this.area = Rectangle.Empty;
this.Invalidate();
tiptext.Text = noselectString;
}
else if (e.KeyData == Keys.Escape)
{ this.Close(); }
}
private void tiptext_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (tiptext.Location.X == 10 && tiptext.Location.Y == 10)
{
Point formloc = new Point((Screen.PrimaryScreen.Bounds.Width - 176), 10);
tiptext.Location = formloc;
}
else
{ tiptext.Location = new Point(10, 10); }
}
private void MouseCaptureForm_Load(object sender, System.EventArgs e)
{
tiptext.Text = noselectString;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -