-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreenFetcherWorker.py
69 lines (59 loc) · 2.5 KB
/
screenFetcherWorker.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
import win32gui
import win32con
from PIL import ImageGrab
import time
class screenFetcherWorker():
def __init__(self):
self.imageCounter = 0
self.titleOfWindowToFetch = ""
self.iniForegroundWindowTitle = win32gui.GetWindowText(win32gui.GetForegroundWindow())
def setTitleOfWindowToFetch(self, title):
self.titleOfWindowToFetch = title
def PrintTopWindow(self, hwnd):
# ----- Prints the title of the top window -----
print(hwnd)
def MinimizeTopWindow(self):
# ----- Minimizes the top windows -----
win32gui.ShowWindow(win32gui.GetForegroundWindow(), win32con.SW_MINIMIZE)
def RestoreWin(self, hwnd):
# ----- Restores (or Maximizes) the previously minimized top windows -----
ClassName = None
WindowName = self.iniForegroundWindowTitle
win32gui.ShowWindow(win32gui.FindWindow(ClassName, WindowName ), win32con.SW_RESTORE)
def MaximizeFGWin(self, WindowName):
# ----- Restores (or Maximizes) the previously minimized top windows -----
ClassName = None
win32gui.ShowWindow(win32gui.FindWindow(ClassName, WindowName ), win32con.SW_MAXIMIZE)
def CloseFGWin(self):
window_handle = win32gui.GetWindowText(win32gui.GetForegroundWindow())
win32gui.PostMessage(window_handle, WM_CLOSE,0,0)
def snapshotFGWin(self, img_name="", window_title=""):
ClassName=None
if window_title=="":
window_title = self.titleOfWindowToFetch
hwnd = win32gui.FindWindow(ClassName, window_title)
if hwnd==0:
print("\n ERROR: Window for the program not found!")
self.RestoreWin(self.iniForegroundWindowTitle)
exit()
self.RestoreWin(hwnd)
win32gui.SetForegroundWindow(hwnd)
time.sleep(1)
#win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)
bbox = win32gui.GetWindowRect(hwnd)
img = ImageGrab.grab(bbox)
if img_name=="":
img_name = "snapshots/screen_" + str(self.imageCounter) + ".jpg"
self.imageCounter += 1
img.save(img_name, quality="web_high")
def sendKeysToActiveWin(self, keys):
# Example: %{TAB} ==> ALT + TAB
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys(keys)
''' ----- Example of Usage -----
sfw = screenFetcherWorker()
sfw.setTitleOfWindowToFetch("Skillsoft Course Player - Google Chrome")
sfw.MinimizeTopWindow()
sfw.snapshotFGWin()
'''