forked from int32cn/ICOM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot.py
162 lines (134 loc) · 4.46 KB
/
screenshot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
#coding=gb2312
#此模块主要提供抓图功能,支持以下三种抓图方式:
#1、抓取全屏,快捷键CTRL+F1
#2、抓取当前窗口,快捷键CTRL+F2
#3、抓取所选区域,快捷键CTRL+F3
#抓到之后,会自动弹出保存对话框,选择路径保存即可
#*******************************************
#更新记录
#0.1 2012-03-10 create by dyx1024
#********************************************
import os
import sys
from PIL import ImageGrab
import ctypes
import win32gui
import ctypes.wintypes
import pyhk
import wx
import screen_tray
def capture_fullscreen(SysTrayIcon):
'''''
Function:全屏抓图
Input:NONE
Output: NONE
author: socrates
blog:http://blog.csdn.net/dyx1024
date:2012-03-10
'''
#抓图
pic = ImageGrab.grab()
#保存图片
save_pic(pic)
def capture_current_windows(SysTrayIcon = None):
'''''
Function:抓取当前窗口
Input:NONE
Output: NONE
author: socrates
blog:http://blog.csdn.net/dyx1024
date:2012-03-10
'''
#窗口结构
class RECT(ctypes.Structure):
_fields_ = [('left', ctypes.c_long),
('top', ctypes.c_long),
('right', ctypes.c_long),
('bottom', ctypes.c_long)]
def __str__(self):
return str((self.left, self.top, self.right, self.bottom))
rect = RECT()
#获取当前窗口句柄
HWND = win32gui.GetForegroundWindow()
#取当前窗口坐标
ctypes.windll.user32.GetWindowRect(HWND,ctypes.byref(rect))
#调整坐标
rangle = (rect.left+2,rect.top+2,rect.right-2,rect.bottom-2)
#抓图
pic = ImageGrab.grab(rangle)
#保存
save_pic(pic)
def capture_choose_windows(SysTrayIcon):
'''''
Function:抓取选择的区域,没有自己写这个,借用QQ抓图功能
Input:NONE
Output: NONE
author: socrates
blog:http://blog.csdn.net/dyx1024
date:2012-03-10
'''
try:
#加载QQ抓图使用的dll
dll_handle = ctypes.cdll.LoadLibrary('CameraDll.dll')
except Exception:
try:
#如果dll加载失败,则换种方法使用,直接运行,如果还失败,退出
os.system("Rundll32.exe CameraDll.dll, CameraSubArea")
except Exception:
return
else:
try:
#加载dll成功,则调用抓图函数,注:没有分析清楚这个函数带的参数个数
#及类型,所以此语句执行后会报参数缺少4个字节,但不影响抓图功能,所
#以直接忽略了些异常
dll_handle.CameraSubArea(0)
except Exception:
return
def save_pic(pic, filename = '未命令图片.png'):
'''''
Function:使用文件对框,保存图片
Input:NONE
Output: NONE
author: socrates
blog:http://blog.csdn.net/dyx1024
date:2012-03-10
'''
app = wx.PySimpleApp()
wildcard = "PNG(*.png)|*.png"
dialog = wx.FileDialog(None, "Select a place", os.getcwd(),
filename, wildcard, wx.SAVE)
if dialog.ShowModal() == wx.ID_OK:
pic.save(dialog.GetPath().encode('gb2312'))
else:
pass
dialog.Destroy()
class hook_kev_screenshot:
def __init__(self):
self.data = screen_tray.SysTrayIcon
def capture_fullscreen(self):
capture_fullscreen(self.data)
def capture_current_windows(self):
capture_current_windows(self.data)
def capture_choose_windows(self):
capture_choose_windows(self.data)
def screenshot_main():
'''''
Function:主函数,注册快捷键
Input:NONE
Output: NONE
author: socrates
blog:http://blog.csdn.net/dyx1024
date:2012-03-10
'''
#创建hotkey句柄
hot_handle = pyhk.pyhk()
fun = hook_kev_screenshot()
#注册抓取全屏快捷键CTRL+F1
hot_handle.addHotkey(['Ctrl', 'F1'], fun.capture_fullscreen)
#注册抓取当前窗口快捷键CTRL+F2
hot_handle.addHotkey(['Ctrl', 'F2'], fun.capture_current_windows)
#注册抓取所选区域快捷键CTRL+F3
hot_handle.addHotkey(['Ctrl', 'F3'], fun.capture_choose_windows)
#开始运行
hot_handle.start()