Created
April 25, 2018 10:56
-
-
Save goraj/edbdc9f493f57d9e51399e6b05a80be7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Wed Mar 21 08:36:44 2018 | |
@author: user | |
""" | |
import cv2 | |
import numpy as np | |
import screeninfo as si | |
import win32gui as wg | |
import win32con as wc | |
class Window(object): | |
def __init__(self, | |
use_projector = False, | |
width = 500, | |
height = 500): | |
self.color_background = (0,0,0) | |
self.window = None | |
self.use_projector = use_projector | |
self.width = width | |
self.height = height | |
self.window_name = 'windowaligner' | |
self.x = 0 | |
self.y = 0 | |
self.setup_window() | |
""" | |
based on https://github.com/howardjohn/pyty/blob/master/src/window_api.py | |
""" | |
def remove_windowbar(self): | |
def hwnd_for_title(title): | |
def cb(hwnd, param): | |
title = ''.join(char for char in wg.GetWindowText(hwnd) if ord(char) <= 126) | |
d[title] = hwnd | |
d = {} | |
wg.EnumWindows(cb, title) | |
if title in d: | |
return d[title] | |
else: | |
return None | |
def restore(hwnd): | |
""" | |
Restores (unmaximizes) the window. | |
Args: | |
hwnd (int): The window handler. | |
""" | |
wg.ShowWindow(hwnd, wc.SW_RESTORE) | |
def maximize(hwnd): | |
""" | |
Maximizes the window. | |
Args: | |
hwnd (int): The window handler. | |
""" | |
wg.ShowWindow(hwnd, wc.SW_MAXIMIZE) | |
def remove_titlebar(hwnd): | |
""" | |
Sets window style to caption (no titlebar). | |
Args: | |
hwnd (int): The window handler. | |
""" | |
style = wg.GetWindowLong(hwnd, wc.GWL_STYLE) | |
style &= ~wc.WS_CAPTION | |
wg.SetWindowLong(hwnd, wc.GWL_STYLE, style) | |
maximize(hwnd) | |
restore(hwnd) | |
d = hwnd_for_title(self.window_name) | |
if d is not None: | |
remove_titlebar(d) | |
def setup_window(self): | |
if self.window is None: | |
self.window = cv2.namedWindow(self.window_name, cv2.WINDOW_AUTOSIZE) | |
self.remove_windowbar() | |
if self.use_projector: | |
monitor_index = 1 | |
projector_index = 0 | |
screen_mon = si.get_monitors()[monitor_index] | |
screen_proj = si.get_monitors()[projector_index] | |
# position of upper left corner | |
self.x = screen_mon.x + int(screen_proj.width/10*3)-150 | |
self.y = int(screen_proj.height/6) | |
self.width = int(screen_proj.width/10*4) | |
self.height = int(screen_proj.height/6*4) | |
#self.width = screen_proj.height//2 | |
#self.height = screen_proj.height//2 | |
else: | |
self.x = 200 | |
self.y = 200 | |
cv2.moveWindow(self.window_name, self.x, self.y) | |
self.buf = np.zeros((self.height, self.width, 3), dtype=np.uint8) | |
self.draw_crosshair() | |
def clear_buf(self): | |
self.buf = np.zeros((self.height, self.width, 3), dtype=np.uint8) | |
def move_window(self, x, y): | |
if self.x+x >= 0: | |
self.x += x | |
if self.y+y >= 0: | |
self.y += y | |
cv2.moveWindow(self.window_name, self.x, self.y) | |
cv2.waitKey(1) | |
def resize_window(self, px): | |
if self.height+px >= 0: | |
self.height += px | |
self.width += px | |
self.clear_buf() | |
self.draw_crosshair() | |
def draw_crosshair(self): | |
if self.height >= 3 and self.width >= 3: | |
#horizontal | |
pt1 = (0,self.height//2) | |
pt2 = (self.width, self.height//2) | |
cv2.line(self.buf, pt1, pt2, color=(255,255,255)) | |
#vertical | |
pt1 = (self.width//2, 0) | |
pt2 = (self.width//2, self.height) | |
cv2.line(self.buf, pt1, pt2, color=(255,255,255)) | |
if __name__ == '__main__': | |
wnd = Window() | |
functable = [None]*256 | |
#wasd | |
functable[97] = lambda: wnd.move_window(-2, 0) | |
functable[119] = lambda: wnd.move_window(0, -2) | |
functable[100] = lambda: wnd.move_window(2, 0) | |
functable[115] = lambda: wnd.move_window(0, 2) | |
#u | |
functable[117] = lambda: wnd.resize_window(-2) | |
#i | |
functable[105] = lambda: wnd.resize_window(2) | |
#d unten | |
#s 115 hoch | |
# wlinks | |
#a rechts | |
c = 0 | |
while c != 27: | |
cv2.imshow(wnd.window_name, wnd.buf) | |
c = cv2.waitKey(30)&255 | |
if functable[c]: | |
functable[c]() | |
print('(x,y) = ({},{})\n(w,h) = ({},{})'.format(wnd.x, wnd.y, wnd.width, wnd.height)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment