代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
实现鼠标选择矩形截图
作者:
zgke
/ 发布于
2014/7/29
/
942
当按下BUTTON1的时候可以在屏幕上选择一个矩形进行截图全部的类先建立个新项目 为WINDOWS库类.这里因为需要全局钩子..我在WINFORM里不知道为什么设置不了全局钩子.但在DLL里就可以.....可恶的C#........复制下面代码到CLASS1.CS里
使用方法 private void button1_Click(object sender, EventArgs e) { s.GerScreenFormRectangle(); } private Zgke.CopyScreen s; private void Form1_Load(object sender, EventArgs e) { s = new Zgke.CopyScreen(); s.GetScreenImage+=new Zgke.CopyScreen.GetImage(s_GetScreenImage); } void s_GetScreenImage(Image p_Image) { pictureBox1.Image = p_Image; } using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Drawing; using System.Windows.Forms; namespace Zgke { public class API { [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr GetDesktopWindow(); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SetWindowsHookEx(int hookid, HookProc pfnhook, IntPtr hinst, int threadid); public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam); [DllImport("kernel32.dll", ExactSpelling = true, CharSet = CharSet.Auto)] public static extern int GetCurrentThreadId(); [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern bool UnhookWindowsHookEx(IntPtr hhook); public enum WindowsHookCodes { WH_MSGFILTER = (-1), WH_JOURNALRECORD = 0, WH_JOURNALPLAYBACK = 1, WH_KEYBOARD = 2, WH_GETMESSAGE = 3, WH_CALLWNDPROC = 4, WH_CBT = 5, WH_SYSMSGFILTER = 6, WH_MOUSE = 7, WH_HARDWARE = 8, WH_DEBUG = 9, WH_SHELL = 10, WH_FOREGROUNDIDLE = 11, WH_CALLWNDPROCRET = 12, WH_KEYBOARD_LL = 13, WH_MOUSE_LL = 14 } } /// <summary> /// 一个根据矩形截图类 /// zgke@Sina.com /// qq:116149 /// </summary> public class CopyScreen { /// <summary> /// 屏幕大小 /// </summary> private Size ScreenSize { get { return Screen.PrimaryScreen.Bounds.Size; } } /// <summary> /// 鼠标位置 /// </summary> private Point MousePoint { get { return Cursor.Position; } } /// <summary> /// 私有方法获取屏幕图形(全部图形) /// </summary> public Bitmap ScreenImage { get { Bitmap m_BackBitmap = new Bitmap(ScreenSize.Width, ScreenSize.Height); Graphics _Graphics = Graphics.FromImage(m_BackBitmap); _Graphics.CopyFromScreen(new Point(0, 0), new Point(0, 0), ScreenSize); _Graphics.Dispose(); return m_BackBitmap; } } /// <summary> /// 钩子 /// </summary> private HookMessage m_HookMessage; /// <summary> /// 屏幕句柄 /// </summary> private IntPtr m_ScreenForm; /// <summary> /// 图形 /// </summary> private Bitmap m_Image; public delegate void GetImage(Image p_Image); /// <summary> /// 获取屏幕截图 /// </summary> public event GetImage GetScreenImage; /// <summary> /// 构造 /// </summary> public CopyScreen() { m_ScreenForm = API.GetDesktopWindow(); m_HookMessage = new HookMessage(API.WindowsHookCodes.WH_MOUSE_LL, true); m_HookMessage.GetHook += new HookMessage.GetHookMessage(m_HookMessage_GetHook); } /// <summary> /// 钩子事件 /// </summary> /// <param name="p_Code"></param> /// <param name="p_wParam"></param> /// <param name="p_lParam"></param> /// <param name="p_Send"></param> void m_HookMessage_GetHook(int p_Code, IntPtr p_wParam, IntPtr p_lParam, ref bool p_Send) { if (m_StarMouse) { switch (p_wParam.ToInt32()) { case 512: //Move MouseMove(); break; case 513: //Down MouseDown(); p_Send = false; break; case 514: //Up MouseUp(); p_Send = false; break; default: m_StarMouse = false; break; } } } /// <summary> /// 根据矩形 如果Width是正直接返回 如果使-会转换成正的矩形 保证大小位置不变 /// </summary> /// <param name="p_Rectangle">矩形</param> /// <returns>正矩形</returns> public static Rectangle GetUprightRectangle(Rectangle p_Rectangle) { Rectangle _Rect = p_Rectangle; if (_Rect.Width < 0) { int _X = _Rect.X; _Rect.X = _Rect.Width + _Rect.X; _Rect.Width = _X - _Rect.X; } if (_Rect.Height < 0) { int _Y = _Rect.Y; _Rect.Y = _Rect.Height + _Rect.Y; _Rect.Height = _Y - _Rect.Y; } return _Rect; } private Rectangle m_MouseRectangle = new Rectangle(0, 0, 0, 0); private bool m_DrawStar = false; private void MouseDown() { m_MouseRectangle.X = MousePoint.X; m_MouseRectangle.Y = MousePoint.Y; m_DrawStar = true; } private void MouseMove() { if (m_DrawStar) { ControlPaint.DrawReversibleFrame(m_MouseRectangle, Color.Transparent, FrameStyle.Dashed); m_MouseRectangle.Width = MousePoint.X - m_MouseRectangle.X; m_MouseRectangle.Height = MousePoint.Y - m_MouseRectangle.Y; ControlPaint.DrawReversibleFrame(m_MouseRectangle, Color.White, FrameStyle.Dashed); } } private void MouseUp() { ControlPaint.DrawReversibleFrame(m_MouseRectangle, Color.Transparent, FrameStyle.Dashed); m_DrawStar = false; m_StarMouse = false; Rectangle _ScreenRectangle = GetUprightRectangle(m_MouseRectangle); m_MouseRectangle.X = 0; m_MouseRectangle.Y = 0; m_MouseRectangle.Width = 0; m_MouseRectangle.Height = 0; if (GetScreenImage != null) { if (_ScreenRectangle.Width != 0 && _ScreenRectangle.Height != 0) GetScreenImage(m_Image.Clone(_ScreenRectangle, m_Image.PixelFormat)); } } private bool m_StarMouse = false; /// <summary> /// 获取图形 /// </summary> public void GerScreenFormRectangle() { m_Image = ScreenImage; m_StarMouse = true; } /// <summary> /// 获取图形 /// </summary> public void GetScreen() { if (GetScreenImage != null) GetScreenImage(ScreenImage); } } /// <summary> /// 用钩子获取消息 /// zgke@Sina.com /// </summary> public class HookMessage { private IntPtr m_HookEx; /// <summary> /// 设置自己进程的钩子 /// </summary> /// <param name="p_HookCodes">钩子类型</param> public HookMessage(API.WindowsHookCodes p_HookCodes) { m_HookEx = API.SetWindowsHookEx((int)p_HookCodes, new API.HookProc(SetHookProc), IntPtr.Zero, API.GetCurrentThreadId()); } /// <summary> /// 设置进程的钩子 /// </summary> /// <param name="p_HookCodes">钩子类型</param> /// <param name="p_ThreadID">全局钩子</param> public HookMessage(API.WindowsHookCodes p_HookCodes, bool p_Zero) { IntPtr _Value = System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]); m_HookEx = API.SetWindowsHookEx((int)p_HookCodes, new API.HookProc(SetHookProc), _Value, 0); } /// <summary> /// 关闭钩子 /// </summary> public void UnHookMessage() { if (API.UnhookWindowsHookEx(m_HookEx)) { m_HookEx = IntPtr.Zero; } } public delegate void GetHookMessage(int p_Code, IntPtr p_wParam, IntPtr p_lParam, ref bool p_Send); public event GetHookMessage GetHook; private IntPtr SetHookProc(int p_Code, IntPtr p_wParam, IntPtr p_lParam) { GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); bool _SendMessage = true; if (GetHook != null) GetHook(p_Code, p_wParam, p_lParam, ref _SendMessage); if (!_SendMessage) return new IntPtr(1); return IntPtr.Zero; } } }
试试其它关键字
矩形截图
截图
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
zgke
贡献的其它代码
(
46
)
.
让程序只保证一个进程的类
.
获取PictureBox的SizeMode为Zoom图形的大小
.
获取RICHTEXTBOX所有图形的位置和大小
.
实现系统热键盘注册的类.
.
操作CUR的类
.
读写 Photoshop PSD文件 操作类
.
获取系统鼠标指针
.
隐藏滚动条
.
实现BCD码
.
获取EXCEL所有表名
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3