Viewer

 

C#编写的抓屏程序和源代码

参考了网上的一些代码写了个抓屏程序,主要是根据自己的需求慢慢写的。

自己用着感觉也还不错,满足一般的要求应该是足够了,在这里共享一下程序和代码。

首次原创,转载请注明来源。

(注:运行程序需要Framework 2.0,项目是用VS2008建的)

 


关于程序:
下载

程序图标(用的QQ的图标)只显示在系统栏中,可通过启动程序、左键单击任务栏中的程序图标或输入设置的快捷键(默认为PrintScreen)开始抓屏。

用鼠标左键确定抓屏区域,确定好区域之后双击左键保存抓取区域的图片。

单击右键或点击ESC键取消抓屏操作并将程序隐藏在任务栏中。

右键单击任务栏中的程序图标弹出程序菜单,可退出程序,进行程序的设置和查看关于对话框。

 


关于代码:
下载

抓屏:

本程序主要功能是抓屏,先说说抓屏。

抓屏主窗体为FormCatcher,包含的控件主要是一个用于保存图片和画图的PictureBox,然后是一些成员变量。


得到屏幕图片的主要原理是通过 System.Drawing.Graphics 类的成员函数public void CopyFromScreen(Point upperLeftSource, Point upperLeftDestination, Size blockRegionSize)将屏幕复制到System.Drawing.Bitmap类的实例中,然后怎么操作就随便了。

用于拷图操作的私有函数:

private Bitmap GetScreenImage(Point upperLeftSource, Point upperLeftDestination, Size blockRegionSize)
{
    Bitmap bmp 
= new Bitmap(blockRegionSize.Width, blockRegionSize.Height);
    Graphics g 
= Graphics.FromImage(bmp);
    g.CopyFromScreen(upperLeftSource, upperLeftDestination, blockRegionSize);
    g.Dispose();
    
return bmp;
}

 

初始化抓屏操作的函数,用PictureBox控件pbCatchArea覆盖全屏并将其背景图设为拷屏得到的图片,然后初始化抓屏状态_csCurrentStep,显示激活抓屏窗体

public void RestartCatch()
{
   
pbCatchArea.Cursor = Cursors.Default;
    this.Bounds = Screen.PrimaryScreen.Bounds;
    
this.pbCatchArea.Bounds = Screen.PrimaryScreen.Bounds;    this.pbCatchArea.BackgroundImage = this.GetScreenImage(new Point(00), new Point(00), new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
    _csCurrentStep 
= CatchSatus.Start;
    
this.Show();
    
this.Activate();
}

 

程序主要的代码是在Mouse的MouseDown,MouseMove和MouseDown三个事件中根据不同的抓屏状态_csCurrentStep和鼠标点击时的参数调整各个成员变量的值,然后在pbCatchArea的Paint事件中根据这些成员变量的值进行画图或写字符串的操作,最后在MouseDoubleClick中保存抓屏区域的图。

有必要把几个重要的窗体类的成员变量说明一下:

private Point _pLeftTopPoint = new Point(00), _pRightBottomPoint = new Point(00);
private Point _pSizedPoint = new Point(00), _pCurrentPoint = new Point(00);
private CatchSatus _csCurrentStep = CatchSatus.Start;
private Position _psSizedPosition = Position.Outside;
private DateTime _dtMouseClickTime = System.DateTime.Now;
private TimeSpan MOUSE_DOUBLE_CLICK_MAX_SPAN = new TimeSpan(0000300);

首先是4个点,分别是确定抓屏区域时的左上点_pLeftTopPoint、右下点_pRightBottomPoint、移动或调整抓屏区域时的参照点_pSizedPoint和鼠标所在的当前点_pCurrentPoint。前两个点用于确定抓屏区域;_pSizedPoint的值是调整抓屏区域时鼠标左键按下时的点坐标,根据此点和鼠标的移动调整抓屏区域;_pCurrentPoint用于确定在决定抓屏区域时提示字符串应该写在屏幕的左上角还是右上角。
枚举变量_csCurrentStep保存抓屏操作的当前状态。
枚举变量_psSizedPosition用于标示鼠标与已经确定了的抓屏区域的相对位置,在调整抓屏区域时感觉此变量进行不同的操作。
_dtMouseClickTimeMOUSE_DOUBLE_CLICK_MAX_SPAN用来区分MouseDown和MouseDoubleClick事件。


MouseDown事件,单击左键调整成员变量并调用pbCatchArea.Invalidate()激活pbCatchArea的Paint事件,单击右键则隐藏程序:

Code

 

MouseMove事件,也是处理逻辑然后激活pbCatchArea的Paint事件:

Code


MouseUp事件,调整抓屏区域的左上点和右下点:

private void pbCatchArea_MouseUp(object sender, MouseEventArgs e)
{
    
if (_csCurrentStep == CatchSatus.WaitingSave || _csCurrentStep == CatchSatus.Resizing)
    
{
        AdjustPoints();
        _csCurrentStep 
= CatchSatus.WaitingSave;
    }

}

因为确定抓屏区域时鼠标点下的第一个点不一定就是左上点,调整抓屏区域的大小时也一样,AdjustPoints()作用就是重新调整_pLeftTopPoint和_pRightBottomPoint。

 

pbCatchArea的Paint事件响应:

private void pbCatchArea_Paint(object sender, PaintEventArgs e)
{
    
switch (_csCurrentStep)
    
{
        
case CatchSatus.Start:
            DrawReferLine(e.Graphics);
            DrawString(e.Graphics, 
string.Format("{0}", _pRightBottomPoint));
            
break;
        
case CatchSatus.Pending:
            DrawReferLine(e.Graphics);
            DrawCatchRectangle(e.Graphics);
            DrawString(e.Graphics, 
string.Format("{0}->{1} 宽:{2};高:{3}", _pLeftTopPoint, _pRightBottomPoint, _pRightBottomPoint.X - _pLeftTopPoint.X, _pRightBottomPoint.Y - _pLeftTopPoint.Y));
            
break;
        
case CatchSatus.Resizing:
            DrawCatchRectangle(e.Graphics);
            DrawString(e.Graphics, 
string.Format("{0}->{1} 宽:{2};高:{3}", _pLeftTopPoint, _pRightBottomPoint, _pRightBottomPoint.X - _pLeftTopPoint.X, _pRightBottomPoint.Y - _pLeftTopPoint.Y));
            
break;
        
case CatchSatus.WaitingSave:
        
case CatchSatus.Saving:
            DrawCatchRectangle(e.Graphics);
            
break;
        
default:
            
break;
    }

}
 

MouseDoulbeClick事件保存抓屏区域内的图:

Code


到这整个抓屏操作就完成了。

 

全局快捷键的设置和调用:

快捷键处理类:
快捷键处理类的代码主要是参考http://www.cnblogs.com/TianFang/archive/2007/05/14/745489.html写的,就做了一点点的小改动:

  1. Regist:如果注册的函数已经在keymap中存在则不重复注册;
  2. UnRegist:加了同步更新keymap;
  3. ProcessHotKey:传进来的参数由Windows消息改成了消息的值,在这里消息的值即为回调函数的Id。

Code

 

快捷键的设置:

程序启动时通过Setting.Default(Setting类的实例)读取设置文件Config.xml中设置的快捷键值,然后通过HotkeyHelper.Regist(this.Handle, kModifier, kMain, RestartCatch)注册快捷键。

处理快捷键是在消息处理函数中:

protected override void WndProc(ref Message m)
{
    
switch (m.Msg)
    
{
        
case FormCatcher.UM_RESTART:
            
//试图运行程序的多个实例时转为重新开始截图的操作
            this.RestartCatch();
            
break;
        
case FormCatcher.WM_HOTKEY:
            
//正在保存图片时不处理快捷键
            if (_csCurrentStep != CatchSatus.Saving)
            
{
                HotkeyHelper.ProcessHotKey(m.WParam.ToInt32());
            }

            
break;
        
default:
            
break;
    }

    
base.WndProc(ref m);
}

 

只运行一个程序实例: 

也是参考网上的代码用互斥变量实现的。
不同之处是如果找到此程序已运行则发送用户消息UM_RESTART给此进程的frmCatcher窗体。
然后在窗体FormCatcher类中的消息处理函数(如上代码)中做处理,在这里是调用RestartCatch()重启抓屏操作。

给已运行的进程中frmCatcher窗体发送消息的代码如下:

private static void HandleRunningInstance()
{
    Process pRunning 
= null;
    Process pCurrent 
= Process.GetCurrentProcess();
    Process[] psSameName 
= Process.GetProcessesByName(pCurrent.ProcessName);
    
foreach (Process p in psSameName)
    
{
        
if (p.Id != pCurrent.Id)
        
{
            pRunning 
= p;
            
break;
        }

    }

    
if (pRunning != null)
    
{
        IntPtr mainWindowHandle 
= pRunning.MainWindowHandle == IntPtr.Zero ? FindWindow(null"frmCatcher") : pRunning.MainWindowHandle;
        
//发送消息给程序主窗体
        SendMessage(mainWindowHandle, FormCatcher.UM_RESTART, IntPtr.Zero, IntPtr.Zero);
    }

}

此函数用到了Windows API FindWindow和SendMessage。

 

另外,程序还包括其他三个类:
FormSetting:程序的设置窗体类,包括全局快捷键的设置。
Setting:封装了程序的各个设置选项,并通过XmlHelper进行程序设置的初始化和保存。
XmlHelper:Xml文档操作类,也就是读取和保存简单的Xml文档。

 

posted on 2008-09-04 09:45  Viewer  阅读(4519)  评论(21编辑  收藏  举报

导航