用C# 实现截图功能(类似QQ截图)

概述:

在公司,不能自己安装软件,也不能下载,但有时候需要截图。用PrintScreen键只能截取全屏,感觉很麻烦。于是决定自己编写一个截图工具 。

众所周知,QQ截图首先将截取全屏为一个图片,然后用在这个图片基础上截取需要的部分。本程序实现方法类似。

程序运行截图如下:


图中心矩形为即将截取区域

程序很粗糙,希望大家提出宝贵意见。

1,自定义矩形类MyRectangle

在QQ截图程序中,用户用鼠标绘制出的截图区域是可调整大小和位置的,在4个边的中点和4个顶点各有一个小矩形标记。(如图所示)



.NET Framework中本身没有这样的矩形,因此要自定义实现。

考虑到类的专用性,不必实现.Net Framework2.0中Rectangle的全部功能。
该MyRectangle类图如下:
MyRectangle需包含如下属性


int X 记录矩形左上角x坐标

int Y 记录矩形左上角y坐标

int DownPointX 绘制矩形时鼠标落点x坐标

int DownPointY 绘制矩形时鼠标落点y坐标

int Width 矩形宽

int Height 矩形高

int MinWidth 矩形最小宽度

int MinHeight 矩形最小高度

bool ChangeSizeMode 标识矩形当前绘制模式是否为“改变大小”

bool MoveMode 标识矩形当前绘制模式是否为“移动”

bool MouseOnLeftTop 标识鼠标当前位置是否在矩形左上角

bool MouseOnLeftMiddle 标识鼠标当前位置是否在矩形左边中点

bool MouseOnLeftBottom 标识鼠标当前位置是否在矩形左下角

bool MouseOnRightTop 标识鼠标当前位置是否在矩形右上角

bool MouseOnRightMiddle 标识鼠标当前位置是否在矩形右边中点

bool MouseOnRightBottom 标识鼠标当前位置是否在矩形右下角

bool MouseOnTopMiddle 标识鼠标当前位置是否在矩形顶边中点

bool MouseOnBottomMiddle 标识鼠标当前位置是否在矩形底边中点

bool MouseOnMiddle 标识鼠标当前位置是否在矩形中心

int LittleRectangleWidth 矩形周边8个小矩形的宽度

int LittleRectangleHeight 矩形周边8个小矩形的高度

Rectangle LeftTopRectangle 矩形左上角小矩形

Rectangle LeftMiddleRectangle 矩形左边中点小矩形

Rectangle LeftBottomRectangle 矩形左下角小矩形

Rectangle RightTopRectangle 矩形右上角小矩形

Rectangle RightMiddleRectangle 矩形右边中点小矩形

Rectangle RightBottomRectangle 矩形右下角小矩形

Rectangle TopMiddleRectangle 矩形顶边中点小矩形

Rectangle BottomMiddleRectangle 矩形底边中点小矩形

Rectangle Rect 主体矩形

Size Size 矩形大小

Image BackImage 背景图片
     
Cursor MyCursor 光标样式

 矩形本身包含监测当前绘制模式和绘制方法,主要方法成员如下:

SetLittleRectangle() 设置8个小矩形
       
Draw(Color backColor) 绘制方法,+1重载
       
ChangeSize(MouseEventArgs e) 改变矩形大小
       
Move(int newX, int newY) 改变矩形位置
       
CheckMouseLocation(MouseEventArgs e) 判断鼠标当前落点

setAllModeFalse() 将所有模式设定为false
       
public bool onChangingMode() 判断当前绘制模式是否为“改变大小”或“移动”
       
Initialize(int x, int y, int width, int height) 根据给定参数初始化矩形

MyRectagle类代码实现如下:

MyRectangle.class

2,建立截图主窗口

核心类MyRectangle已经完成,剩下的工作就是使用改类实现预想的截图功能。

用VS2005 新建Project,命名为ScreenCutter。将主窗口命名为MainForm,新建一个窗口命名为ScreenBody,将其 ShowInTaskbar属性设置为False,TopMost属性设置为True,FormBorderStyle属性设置为None,在 ScreenBody上添加一个panel控件panel1,设置BackColor属性为蓝色,在panel1上添加相应个数的label,如 labelLocation、labelWidth、labelHeight等,用于指示当前选区位置和大小,panel1最终样式为:
panel1

修改ScreenBody的引用命名空间为:

using System;
using System.Drawing;
using System.Windows.Forms;

在ScreenBody类中添加如下私有成员:

        private Graphics MainPainter;  //the main painter
        private bool isDowned;         //check whether the mouse is down
        private bool RectReady;         //check whether the rectangle is finished
        private Image baseImage;       //the back ground of the screen

        
private Point moveModeDownPoint;    //the mouse location when you move the rectangle

        
private MyRectangle myRectangle;    //the rectangle

        
private bool moveMode; //check whether the rectangle is on move mode or not
        private bool changeSizeMode;  //check whether the rectangle is on change size mode or not

修改ScreenBody构造函数:

        public ScreenBody()
        
{
            InitializeComponent();
            panel1.Location 
= new Point(this.Left, this.Top);
            myRectangle 
= new MyRectangle();
            moveModeDownPoint 
= new Point();
            
this.Cursor = myRectangle.MyCursor;
        }

添加ScreenBody窗口的DoubleClick、MouseDown、MouseUp、MouseMove及Load事件代码:

        private void ScreenBody_DoubleClick(object sender, EventArgs e)
        
{
            
if (((MouseEventArgs)e).Button == MouseButtons.Left && myRectangle.Contains(((MouseEventArgs)e).X, ((MouseEventArgs)e).Y))
            
{
                panel1.Visible 
= false;
                MainPainter.DrawImage(baseImage, 
00);
                Image memory 
= new Bitmap(myRectangle.Width, myRectangle.Height);
                Graphics g 
= Graphics.FromImage(memory);
                
                g.CopyFromScreen(myRectangle.X, myRectangle.Y, 
00, myRectangle.Size);
                Clipboard.SetImage(memory);
                
                
this.Close();
            }

        }


        
private void ScreenBody_MouseDown(object sender, MouseEventArgs e)
        
{
            
            
if (e.Button == MouseButtons.Left)
            
{
                
                isDowned 
= true;

                
if (!RectReady)
                
{
                    myRectangle.DownPointX 
= e.X;
                    myRectangle.DownPointY 
= e.Y;
                    myRectangle.X 
= e.X;
                    myRectangle.Y 
= e.Y;
                }

                
if (RectReady == true)
                
{
                    moveModeDownPoint 
= new Point(e.X, e.Y);    
                }

            }

            
if (e.Button == MouseButtons.Right)
            
{
                
if (!RectReady)
                
{
                    
                    
this.Close();
                    
return;
                }

                MainPainter.DrawImage(baseImage, 
00);
                myRectangle.Initialize(
0000);
                myRectangle.setAllModeFalse();
                
this.Cursor = myRectangle.MyCursor;
                RectReady 
= false;
            }


        }


        
private void ScreenBody_MouseUp(object sender, MouseEventArgs e)
        
{
            
if (e.Button == MouseButtons.Left)
            
{
                isDowned 
= false;
                RectReady 
= true;
            }

        }


        
private void ScreenBody_MouseMove(object sender, MouseEventArgs e)
        
{
            
            labelWidth.Text 
= myRectangle.Width.ToString();
            labelHeight.Text 
= myRectangle.Height.ToString();
            labelLocation.Text 
= ""+myRectangle.X.ToString() + ", Y " + myRectangle.Y.ToString();
            
if (!RectReady)
            
{
                
if (isDowned)
                
{
                    myRectangle.Draw(e, 
this.BackColor);
                }

            }

            
else
            
{
                myRectangle.CheckMouseLocation(e);

                
this.Cursor = myRectangle.MyCursor;
                
                
this.changeSizeMode = myRectangle.ChangeSizeMode;
                
this.moveMode = myRectangle.MoveMode&&myRectangle.Contains(moveModeDownPoint.X,moveModeDownPoint.Y);
                
if (changeSizeMode)
                
{
                    
this.moveMode = false;
                    myRectangle.Draw(BackColor);
                    myRectangle.ChangeSize(e);
                    myRectangle.Draw(BackColor);
                }

                
if (moveMode)
                
{
                    
this.changeSizeMode = false;
                    myRectangle.Draw(BackColor);
                    myRectangle.X 
= myRectangle.X + e.X - moveModeDownPoint.X;
                    myRectangle.Y 
= myRectangle.Y + e.Y - moveModeDownPoint.Y;

                    moveModeDownPoint.X 
= e.X;
                    moveModeDownPoint.Y 
= e.Y;

                    myRectangle.Draw(
this.BackColor);
                }

            }

        }


        
private void ScreenBody_Load(object sender, EventArgs e)
        
{
            
this.WindowState = FormWindowState.Maximized;
            MainPainter 
= this.CreateGraphics();
            isDowned 
= false;
            baseImage 
= this.BackgroundImage;
            panel1.Visible 
= true;
            RectReady 
= false;
            changeSizeMode 
= false;
            moveMode 
= false;

        }

为了不至截到panel1,添加panel1的MouseEnter事件如下:

        private void panel1_MouseEnter(object sender, EventArgs e)
        
{
            
if (panel1.Location==new Point(this.Left,this.Top))
            
{
                panel1.Location 
= new Point(this.Right-panel1.Width, this.Top);
            }

            
else
            
{
                panel1.Location 
= new Point(this.Left,this.Top);
            }

        }

至此,ScreenBody窗口完成,QQ截图功能可以通过热键触发,下面为本程序添加热键

3,创建热键类

网上有许多这方面的资料,本程序中这段代码取自互联网,如有版权问题请给我留言,我会尽快删除。

添加类HotKey

HotKey.cs文件内容如下

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ScreenCutter
{

    
class HotKey
    
{
        
//如果函数执行成功,返回值不为0。
        
//如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
        [DllImport("user32.dll", SetLastError = true)]
        
public static extern bool RegisterHotKey(
            IntPtr hWnd,                
//要定义热键的窗口的句柄
            int id,                     //定义热键ID(不能与其它ID重复)           
            uint fsModifiers,   //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
            Keys vk                     //定义热键的内容
            );

        [DllImport(
"user32.dll", SetLastError = true)]
        
public static extern bool UnregisterHotKey(
            IntPtr hWnd,                
//要取消热键的窗口的句柄
            int id                      //要取消热键的ID
            );

        
//定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
        [Flags()]
        
public enum KeyModifiers
        
{
            None 
= 0,
            Alt 
= 1,
            Ctrl 
= 2,
            Shift 
= 4,
            WindowsKey 
= 8
        }

    }

}

4,使用热键及托盘区图标

为了使程序更方便使用,程序启动的时候最下化到托盘区,在按下程序热键时会启动截图功能。这些功能在程序的主窗口MainForm类中实现。

为了在托盘区显示图标,为MainForm添加一个NotifyIcon控件,为其指定一Icon图标,并设定visable属性为true

为了实现可以更改热键,首先在项目属性的Setting中添加如下图成员:

setting

MainForm.cs文件代码如下:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using ScreenCutter.Properties;

namespace ScreenCutter
{
    
public enum KeyModifiers //组合键枚举
    {
        None 
= 0,
        Alt 
= 1,
        Control 
= 2,
        Shift 
= 4,
        Windows 
= 8
    }
 
    
    
public partial class MainForm : Form
    
{

        
private ContextMenu contextMenu1;
        
private MenuItem menuItem1;
        
private MenuItem menuItem2;

        
private ScreenBody body;
        
public MainForm()
        
{
            InitializeComponent();
            
this.components = new Container();
            
this.contextMenu1 = new ContextMenu();
            
this.menuItem2 = new MenuItem();
            
this.menuItem1 = new MenuItem();

            
this.contextMenu1.MenuItems.AddRange(
                        
new MenuItem[] this.menuItem1,this.menuItem2 });

            
this.menuItem1.Index = 1;
            
this.menuItem1.Text = "E&xit";
            
this.menuItem1.Click += new EventHandler(this.menuItem1_Click);

            
this.menuItem2.Index = 0;
            
this.menuItem2.Text = "S&et HotKey";
            
this.menuItem2.Click += new EventHandler(this.menuItem2_Click);

            notifyIcon1.ContextMenu 
= this.contextMenu1;

            notifyIcon1.Text 
= "Screen Cutter";
            notifyIcon1.Visible 
= true;

            body 
= null;

        }


        
private void MainForm_SizeChanged(object sender, EventArgs e)
        
{
            
if (this.WindowState == FormWindowState.Minimized)
            
{
                
this.Hide();
                
this.notifyIcon1.Visible = true;
            }


        }


        

        
private void CutScreen()
        
{
            
            Image img 
= new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
            Graphics g 
= Graphics.FromImage(img);
            g.CopyFromScreen(
new Point(00), new Point(00), Screen.AllScreens[0].Bounds.Size);
            body 
= new ScreenBody();
            body.BackgroundImage 
= img;
            body.Show();
        }


        
private void ProcessHotkey(Message m) //按下设定的键时调用该函数
        {
            IntPtr id 
= m.WParam; //IntPtr用于表示指针或句柄的平台特定类型
            string sid = id.ToString();
            
switch (sid)
            
{
                
case "100":
                    CutScreen();
                    
break;
                
default:
                    
break;
            }

        }


        
private void MainForm_Load(object sender, EventArgs e)
        
{
            
uint ctrHotKey = (uint)KeyModifiers.Control;
            
if (Settings.Default.isAltHotKey)
            
{
                ctrHotKey 
=(uint)(KeyModifiers.Alt | KeyModifiers.Control);
            }

            
            HotKey.RegisterHotKey(Handle, 
100, ctrHotKey, Settings.Default.HotKey);//这时热键为Alt+CTRL+A
        }


        
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        
{
            HotKey.UnregisterHotKey(Handle, 
100);//卸载第1个快捷键
        }


        
//重写WndProc()方法,通过监视系统消息,来调用过程
        protected override void WndProc(ref Message m)//监视Windows消息
        {
            
const int WM_HOTKEY = 0x0312;//如果m.Msg的值为0x0312那么表示用户按下了热键
            switch (m.Msg)
            
{
                
case WM_HOTKEY:
                    ProcessHotkey(m);
//按下热键时调用ProcessHotkey()函数
                    break;
            }

            
base.WndProc(ref m); //将系统消息传递自父类的WndProc
        }

        
        
private void menuItem1_Click(object Sender, EventArgs e)
        
{
            HotKey.UnregisterHotKey(Handle, 
100);//卸载第1个快捷键
            this.notifyIcon1.Visible = false;
            
this.Close();
        }

        
private void menuItem2_Click(object Sender, EventArgs e)
        
{
            SetHotKey setHotKey 
= new SetHotKey();
            setHotKey.ShowDialog();
        }

    }

}

5,添加设定热键功能:

新建窗口,命名为SetHotkey,该窗口样式及主要控件命名如下图所示

setHotKey

设定窗口主体FormBorderStyle属性值为FixedToolWindow,Text属性为SetHotKey,MaximizeBox和MinimizeBox属性为false。
添加checkBox1的(ApplicationSettings)-(PropertyBinding)-Checked为isCtrlHotKey,CheckState为Checked,Enable属性为false,Text属性为Ctrl
添加checkBox2的(ApplicationSettings)-(PropertyBinding)-Checked为isAltHotKey,CheckState为Checked,Enable属性为true,Text属性为Alt
comboBox1的Items值为
A
Z
X

为按钮btnDefault添加click事件

        private void btnDefault_Click(object sender, EventArgs e)
        
{
            Settings.Default.HotKey 
= Keys.A;
            Settings.Default.Save();
            
this.Close();
        }

为按钮btnOk添加click事件

        private void btnOK_Click(object sender, EventArgs e)
        
{
            
switch (comboBox1.SelectedIndex)
            
{
                
case 0:
                    Settings.Default.HotKey 
= Keys.A;
                    
break;
                
case 1:
                    Settings.Default.HotKey 
= Keys.Z;
                    
break;
                
case 2:
                    Settings.Default.HotKey 
= Keys.X;
                    
break;
                
default:
                    
break;
            }

            Settings.Default.Save();
            
this.Close();
        }

为按钮btnCancel添加click事件

        private void btnCancel_Click(object sender, EventArgs e)
        
{
            
this.Close();
        }

为SetHotkey窗口添加load事件

        private void SetHotKey_Load(object sender, EventArgs e)
        {
            comboBox1.Text 
= Settings.Default.HotKey.ToString();
        }

6,防止程序多次运行

同样,网上有许多这方面的资料,本部分代码基本来自互联网,如有版权问题请给我留言,我将立即删除

为防止程序多次运行,修改Program.cs文件内容如下:

using System;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ScreenCutter
{

    
static class Program
    
{
        [DllImport(
"User32.dll")]
        
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
        [DllImport(
"User32.dll")]
        
private static extern bool SetForegroundWindow(IntPtr hWnd);
        
private const int WS_SHOWNORMAL = 1;
        
/// <summary>
        
/// The main entry point for the application.
        
/// </summary>

        [STAThread]
        
static void Main()
        
{
            Process instance 
= RunningInstance();
            
if (instance == null)
            
{
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(
false);
                Application.Run(
new MainForm());
            }

            
else
            
{
                HandleRunningInstance(instance);
            }
 
        }


        
public static Process RunningInstance()
        
{
            Process current 
= Process.GetCurrentProcess();
            Process[] processes 
= Process.GetProcessesByName(current.ProcessName);

            
//Loop   through   the   running   processes   in   with   the   same   name
            foreach (Process process in processes)
            
{
                
//Ignore   the   current   process
                if (process.Id != current.Id)
                
{
                    
//Make   sure   that   the   process   is   running   from   the   exe   file.
                    if (Assembly.GetExecutingAssembly().Location.Replace("/""""==
                        current.MainModule.FileName)
                    
{
                        
//Return   the   other   process   instance.
                        return process;
                    }

                }

            }


            
//No   other   instance   was   found,   return   null.
            return null;
        }

        
public static void HandleRunningInstance(Process instance)
        
{
            
//Make   sure   the   window   is   not   minimized   or   maximized
            ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);

            
//Set   the   real   intance   to   foreground   window
            SetForegroundWindow(instance.MainWindowHandle);
        }

    }

}

至此,该截图程序基本完成,实现了类似QQ截图的功能。(默认热键为Ctrl+Alt+A)

注意:程序中用到了一些图片,Icon文件和cur文件,请复制系统目录(C:"WINDOWS"Cursors)下的hcross.cur、 move_m.cur、size1_m.cur、size2_m.cur、size3_m.cur、size4_m.cur文件到.." ScreenCutter"ScreenCutter"Cursors目录下,在.."ScreenCutter"ScreenCutter"Icons 目录下添加相应图标,在.."ScreenCutter"ScreenCutter"Images目录下添加相应图片。如路径不同,请在代码中自行更改。

感谢您的关注,愿您留下宝贵意见。

感谢博友们的关注,现奉上源代码。另注:这个程序是我刚刚学习C#时候写的,难免有不专业的地方,希望多多包含,更希望不要被误导。

posted @ 2008-03-24 09:53  Neeo  阅读(19224)  评论(55编辑  收藏  举报