先給你的程式添加一個Windows表單 ,Name:ScreenBody TopMost:true WindowState:Maximized
下面是一些欄位定義,事件函數和輔助函數:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace LCY.XY
{
public partial class ScreenBody : Form
{
/// <summary>
/// 主畫筆
/// </summary>
private Graphics mainPainter;
/// <summary>
/// 筆
/// </summary>
private Pen pen;
/// <summary>
/// 判斷滑鼠是否按下
/// </summary>
private bool isDowned;
/// <summary>
/// 矩形是否繪製完成
/// </summary>
private bool rectReady;
/// <summary>
/// 原始畫面
/// </summary>
private Image baseImage;
/// <summary>
/// 要儲存的畫面
/// </summary>
private Rectangle rect;
/// <summary>
/// 滑鼠按下的點
/// </summary>
private Point downPoint;
private int tmpx;
private int tmpy;
public ScreenBody()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ScreenBody_DoubleClick(object sender, EventArgs e)
{
if (((MouseEventArgs)e).Button == MouseButtons.Left && rect.Contains(((MouseEventArgs)e).X, ((MouseEventArgs)e).Y))
{
Image memory = new Bitmap(rect.Width, rect.Height);
Graphics g = Graphics.FromImage(memory);
g.CopyFromScreen(rect.X + 1, rect.Y + 1, 0, 0, rect.Size);
Clipboard.SetImage(memory);
this.Close();
}
}
/// <summary>
/// 按下滑鼠
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ScreenBody_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDowned = true;
if (!rectReady)
{
rect.X = e.X;
rect.Y = e.Y;
downPoint = new Point(e.X, e.Y);
}
if (rectReady)
{
tmpx = e.X;
tmpy = e.Y;
}
}
if (e.Button == MouseButtons.Right)
{
if (!rectReady)
{
this.Close();
return;
}
mainPainter.DrawImage(baseImage, 0, 0);
rectReady = false;
}
}
/// <summary>
/// 鬆開滑鼠
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ScreenBody_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (e.X < rect.X)
{
rect.X = e.X;
}
if (e.Y < rect.Y)
{
rect.Y = e.Y;
}
isDowned = false;
rectReady = true;
}
}
/// <summary>
/// 移動滑鼠
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ScreenBody_MouseMove(object sender, MouseEventArgs e)
{
if (!rectReady && isDowned)
{
Image newImage = drawScreen((Image)baseImage.Clone(), e.X, e.Y);
mainPainter.DrawImage(newImage, 0, 0);
newImage.Dispose();
}
if (rectReady)
{
if (rect.Contains(e.X, e.Y))
{
if (isDowned)
{
rect.X = rect.X + e.X - tmpx;
rect.Y = rect.Y + e.Y - tmpy;
tmpx = e.X;
tmpy = e.Y;
moveRect((Image)baseImage.Clone(), rect);
}
}
}
}
/// <summary>
/// 畫矩形
/// </summary>
/// <param name="Painter"></param>
/// <param name="mouse_x"></param>
/// <param name="mouse_y"></param>
private void drawRect(Graphics Painter, int mouse_x, int mouse_y)
{
int width = 0;
int heigth = 0;
if (mouse_y < rect.Y)
{
heigth = downPoint.Y - mouse_y;
}
else
{
heigth = mouse_y - downPoint.Y;
}
if (mouse_x < rect.X)
{
width = downPoint.X - mouse_x;
}
else
{
width = mouse_x - downPoint.X;
}
rect.Size = new Size(width, heigth);
Painter.DrawRectangle(pen, minOf(mouse_x, downPoint.X), minOf(mouse_y, downPoint.Y), width, heigth);
}
/// <summary>
/// 選地區時畫矩形
/// </summary>
/// <param name="back"></param>
/// <param name="mouse_x"></param>
/// <param name="mouse_y"></param>
/// <returns></returns>
private Image drawScreen(Image back, int mouse_x, int mouse_y)
{
Graphics painter = Graphics.FromImage(back);
drawRect(painter, mouse_x, mouse_y);
return back;
}
/// <summary>
/// 選好地區後移動矩形
/// </summary>
/// <param name="image"></param>
/// <param name="rect2"></param>
private void moveRect(Image image, Rectangle rect2)
{
Graphics painter = Graphics.FromImage(image);
painter.DrawRectangle(pen, rect2.X, rect2.Y, rect2.Width, rect2.Height);
mainPainter.DrawImage(image, 0, 0);
image.Dispose();
}
/// <summary>
/// 初始設定變數
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ScreenBody_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
mainPainter = this.CreateGraphics();
pen = new Pen(Brushes.Blue);
isDowned = false;
baseImage = this.BackgroundImage;
rect = new Rectangle();
rectReady = false;
}
/// <summary>
/// 返回較小的數
/// </summary>
/// <param name="f1"></param>
/// <param name="f2"></param>
/// <returns></returns>
private float minOf(float f1, float f2)
{
if (f1 > f2)
return f2;
else
return f1;
}
}
}
最後,在需要的地方調用以下函數:
/// <summary>
/// 顯示視窗
/// </summary>
void GetBitmap()
{
Image img = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
Graphics g = Graphics.FromImage(img);
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
ScreenBody body = new ScreenBody();
body.BackgroundImage = img;
body.Show();
}