This commit is contained in:
themixray 2021-12-27 20:35:20 +03:00
parent 6f81da14ff
commit 759912eee4
2 changed files with 124 additions and 88 deletions

View file

@ -4,10 +4,14 @@ from datetime import datetime as _dt
from pygwin.image import save as _s from pygwin.image import save as _s
from pygwin._pg import pg as _pg from pygwin._pg import pg as _pg
import pygwin.image as _img import pygwin.image as _img
try:
import win32job as _w32j import win32job as _w32j
import win32api as _w32a import win32api as _w32a
import win32con as _w32c import win32con as _w32c
import win32gui as _w32g import win32gui as _w32g
nonwin32api = False
except:
nonwin32api = True
import requests as _req import requests as _req
import tempfile as _tf import tempfile as _tf
import threading as _t import threading as _t
@ -70,36 +74,45 @@ class _win(_surface):
try:self.tray.stop() try:self.tray.stop()
except:pass except:pass
def focus(self): def focus(self):
if not nonwin32api:
self.hide() self.hide()
self.show() self.show()
_w32g.BringWindowToTop(self.hwnd) _w32g.BringWindowToTop(self.hwnd)
_w32g.ShowWindow(self.hwnd, _w32c.SW_SHOWNORMAL) _w32g.ShowWindow(self.hwnd, _w32c.SW_SHOWNORMAL)
_w32g.SetForegroundWindow(self.hwnd) _w32g.SetForegroundWindow(self.hwnd)
def hide(self): def hide(self):
if not nonwin32api:
_w32g.ShowWindow(self.hwnd, _w32c.SW_HIDE) _w32g.ShowWindow(self.hwnd, _w32c.SW_HIDE)
def show(self): def show(self):
if not nonwin32api:
_w32g.ShowWindow(self.hwnd, _w32c.SW_SHOW) _w32g.ShowWindow(self.hwnd, _w32c.SW_SHOW)
def move(self, x, y): def move(self, x, y):
rect = _w32g.GetWindowRect(self.hwnd) if not nonwin32api:
rect = self._getRect()
_w32g.MoveWindow(self.hwnd, int(x), int(y), _w32g.MoveWindow(self.hwnd, int(x), int(y),
rect[2]-x, rect[3]-y, 0) rect[2]-x, rect[3]-y, 0)
def screenshot(self, path): def screenshot(self, path):
_s(self._orig, path) _s(self._orig, path)
return path return path
def center(self,x=_w32a.GetSystemMetrics(0)/2, def center(self,x=_pg.display.get_desktop_sizes()[0][0]/2,
y=_w32a.GetSystemMetrics(1)/2): y=_pg.display.get_desktop_sizes()[0][1]/2):
self.move(x-self.size[0]/2,y-self.size[1]/2) self.move(x-self.size[0]/2,y-self.size[1]/2)
def _getRect(self):
if not nonwin32api:
return _w32g.GetWindowRect(self.hwnd)
def denyDrag(self): def denyDrag(self):
if not nonwin32api:
self._isallowdrag = True self._isallowdrag = True
def loop(self): def loop(self):
while self._isallowdrag: while self._isallowdrag:
pos = _m.get_position() pos = _m.get_position()
pos = [pos[i]-self.position[i] for i in range(2)] pos = [pos[i]-self.position[i] for i in range(2)]
if pos[0] < _w32g.GetWindowRect(self.hwnd)[2]-137: if pos[0] < self._getRect()[2]-137:
if pos[1] < 30: if pos[1] < 30:
_m.release('left') _m.release('left')
_t.Thread(target=lambda:loop(self),daemon=1).start() _t.Thread(target=lambda:loop(self),daemon=1).start()
def allowDrag(self): def allowDrag(self):
if not nonwin32api:
self._isallowdrag = False self._isallowdrag = False
# def smartDrag(self, x): # def smartDrag(self, x):
# self.allowDrag() # self.allowDrag()
@ -128,7 +141,8 @@ class _win(_surface):
# _t.Thread(target=lambda:loop(self),daemon=1).start() # _t.Thread(target=lambda:loop(self),daemon=1).start()
@property @property
def position(self): def position(self):
rect = _w32g.GetWindowRect(self.hwnd) if not nonwin32api:
rect = self._getRect()
x = rect[0] x = rect[0]
y = rect[1] y = rect[1]
return (x, y) return (x, y)
@ -143,9 +157,11 @@ class _win(_surface):
return int(self.rawFps) return int(self.rawFps)
@property @property
def hwnd(self): def hwnd(self):
if not nonwin32api:
return _pg.display.get_wm_info()['window'] return _pg.display.get_wm_info()['window']
@property @property
def visible(self): def visible(self):
if not nonwin32api:
return _w32g.IsWindowVisible(self._win) return _w32g.IsWindowVisible(self._win)
def create(title=None, size=(0,0), icon=None, resizable=False, noframe=False): def create(title=None, size=(0,0), icon=None, resizable=False, noframe=False):
@ -162,6 +178,7 @@ def create(title=None, size=(0,0), icon=None, resizable=False, noframe=False):
return _win(icon) return _win(icon)
def ramLimit(memory_limit): def ramLimit(memory_limit):
if not nonwin32api:
hjob = _w32j.CreateJobObject(None,job_name) hjob = _w32j.CreateJobObject(None,job_name)
if breakaway: if breakaway:
info = _w32j.QueryInformationJobObject(hjob,_w32j.JobObjectExtendedLimitInformation) info = _w32j.QueryInformationJobObject(hjob,_w32j.JobObjectExtendedLimitInformation)

View file

@ -1,65 +1,84 @@
from pygwin._pg import pg
try:
import win32console as w32con import win32console as w32con
import win32con as w32c import win32con as w32c
import win32gui as w32g import win32gui as w32g
import win32api as w32a import win32api as w32a
nonwin32api = True
except:
nonwin32api = False
import pyautogui as pag import pyautogui as pag
class console: class console:
def __init__(self): def __init__(self):
if not nonwin32api:
self._hwnd = w32con.GetConsoleWindow() self._hwnd = w32con.GetConsoleWindow()
@property @property
def hwnd(self): def hwnd(self):
if not nonwin32api:
return self._hwnd return self._hwnd
def focus(self): def focus(self):
if not nonwin32api:
self.hide() self.hide()
self.show() self.show()
w32g.BringWindowToTop(self.hwnd) w32g.BringWindowToTop(self.hwnd)
w32g.ShowWindow(self.hwnd, w32c.SW_SHOWNORMAL) w32g.ShowWindow(self.hwnd, w32c.SW_SHOWNORMAL)
w32g.SetForegroundWindow(self.hwnd) w32g.SetForegroundWindow(self.hwnd)
def unfocus(self):
pass
def hide(self): def hide(self):
if not nonwin32api:
w32g.ShowWindow(self.hwnd, w32c.SW_HIDE) w32g.ShowWindow(self.hwnd, w32c.SW_HIDE)
def show(self): def show(self):
if not nonwin32api:
w32g.ShowWindow(self.hwnd, w32c.SW_SHOW) w32g.ShowWindow(self.hwnd, w32c.SW_SHOW)
def move(self, x, y): def move(self, x, y):
if not nonwin32api:
w32g.SetWindowPos(self.hwnd, x, y, self.size[0], self.size[1]) w32g.SetWindowPos(self.hwnd, x, y, self.size[0], self.size[1])
def resize(self, width, height): def resize(self, width, height):
if not nonwin32api:
w32g.SetWindowPos(self.hwnd, self.position[0], self.position[1], width, height) w32g.SetWindowPos(self.hwnd, self.position[0], self.position[1], width, height)
def minimize(self): def minimize(self):
if not nonwin32api:
w32g.ShowWindow(hwnd, w32c.SW_MINIMIZE) w32g.ShowWindow(hwnd, w32c.SW_MINIMIZE)
return self.size return self.size
def maximize(self): def maximize(self):
if not nonwin32api:
w32g.ShowWindow(hwnd, w32c.SW_MAXIMIZE) w32g.ShowWindow(hwnd, w32c.SW_MAXIMIZE)
return self.size return self.size
def title(): def title():
def fget(self): def fget(self):
if not nonwin32api:
return w32con.GetConsoleTitle() return w32con.GetConsoleTitle()
def fset(self, value): def fset(self, value):
if not nonwin32api:
w32con.SetConsoleTitle(str(value)) w32con.SetConsoleTitle(str(value))
def fdel(self): def fdel(self):
pass pass
return locals() return locals()
title = property(**title()) title = property(**title())
def center(self,x=w32a.GetSystemMetrics(0)/2, def center(self,x=_pg.display.get_desktop_sizes()[0][0]/2,
y=w32a.GetSystemMetrics(1)/2): y=_pg.display.get_desktop_sizes()[0][1]/2):
if not nonwin32api:
self.move(x-self.size[0]/2,y-self.size[1]/2) self.move(x-self.size[0]/2,y-self.size[1]/2)
@property @property
def visible(self): def visible(self):
if not nonwin32api:
return w32g.IsWindowVisible(self.hwnd) return w32g.IsWindowVisible(self.hwnd)
@property @property
def position(self): def position(self):
if not nonwin32api:
rect = w32g.GetWindowRect(self.hwnd) rect = w32g.GetWindowRect(self.hwnd)
x = rect[0]+7 x = rect[0]+7
y = rect[1] y = rect[1]
return (x, y) return (x, y)
@property @property
def size(self): def size(self):
if not nonwin32api:
rect = w32g.GetWindowRect(self.hwnd) rect = w32g.GetWindowRect(self.hwnd)
w = rect[2] - self.position[0]-7 w = rect[2] - self.position[0]-7
h = rect[3] - self.position[1]-7 h = rect[3] - self.position[1]-7
return (w, h) return (w, h)
def screenshot(self, path): def screenshot(self, path):
if not nonwin32api:
rect = self.position+self.size rect = self.position+self.size
self.focus() self.focus()
pag.screenshot(path, region=rect) pag.screenshot(path, region=rect)