windll et la console¶
exemple pour redimensionner une console windows
def console_resize(width=80, height=24, buffer_height=600):
'''Sets up the console size and buffer height.
<at> param width {int} Width of console in column value.
<at> param height {int} Height of console in row value.
<at> param buffer_height {int} Buffer console height in row value.
'''
from ctypes import windll, byref, create_string_buffer
from ctypes.wintypes import SMALL_RECT, _COORD
# Active console screen buffer
# STD_OUTPUT_HANDLE -> -11, STD_ERROR_HANDLE -> -12)
STDERR = -12
# SMALL_RECT input
LEFT = 0
TOP = 0
RIGHT = width - 1
BOTTOM = height - 1
# handle
hdl = windll.kernel32.GetStdHandle(STDERR)
csbi = create_string_buffer(22)
res = windll.kernel32.GetConsoleScreenBufferInfo(hdl, csbi)
if res:
import struct
(bufx, bufy, curx, cury, wattr,
left, top, right, bottom,
maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
current_width = right - left + 1
current_height = bottom - top + 1
current_buffer_height = bufy
if buffer_height < height:
buffer_height = height
# order of resizing avoiding some problems
if current_buffer_height > buffer_height:
rect = SMALL_RECT(LEFT, TOP, RIGHT, BOTTOM) # (left, top, right, bottom)
windll.kernel32.SetConsoleWindowInfo(hdl, True, byref(rect))
bufsize = _COORD(width, buffer_height) # columns, rows
windll.kernel32.SetConsoleScreenBufferSize(hdl, bufsize)
else:
bufsize = _COORD(width, buffer_height) # columns, rows
windll.kernel32.SetConsoleScreenBufferSize(hdl, bufsize)
rect = SMALL_RECT(LEFT, TOP, RIGHT, BOTTOM) # (left, top, right, bottom)
windll.kernel32.SetConsoleWindowInfo(hdl, True, byref(rect))
console_resize()
Pour connaitre la taille d’un terminal
Pour déplacer une fenêtre
import sys
from ctypes import windll, byref, create_string_buffer, c_wchar_p, c_long, Structure, byref
from ctypes.wintypes import SMALL_RECT, _COORD
from optparse import OptionParser
# winuser.h line 3232
SWP_NOSIZE = 1
SWP_NOMOVE = 2
SWP_NOZORDER = 4
SWP_NOREDRAW = 8
SWP_NOACTIVATE = 16
SWP_FRAMECHANGED = 32
SWP_SHOWWINDOW = 64
SWP_HIDEWINDOW = 128
SWP_NOCOPYBITS = 256
SWP_NOOWNERZORDER = 512
SWP_NOSENDCHANGING = 1024
SWP_DRAWFRAME = SWP_FRAMECHANGED
SWP_NOREPOSITION = SWP_NOOWNERZORDER
SWP_DEFERERASE = 8192
SWP_ASYNCWINDOWPOS = 16384
class RECT(Structure):
_fields_ = [
('left', c_long),
('top', c_long),
('right', c_long),
('bottom', c_long)
]
def top_most(title, e, x=0, y=0, cx=0, cy=0):
h = windll.user32.FindWindowA(None, title)
if h==0:
return None
if(e == True):
windll.user32.SetWindowPos(h, -1,x,y,cx,cy,SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW)
else:
windll.user32.SetWindowPos(h, -2,x,y,cx,cy,SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW)
top_most("NameOfWindows", False, x=0)
connaitre la taille et la position d’une fenêtre
from ctypes import windll, byref, create_string_buffer, c_wchar_p, c_long, Structure, byref
from ctypes.wintypes import SMALL_RECT, _COORD
from optparse import OptionParser
class RECT(Structure):
_fields_ = [
('left', c_long),
('top', c_long),
('right', c_long),
('bottom', c_long)
]
def get_pos(title):
h = windll.user32.FindWindowA(None, title)
if h==0:
return None
former = RECT(0,0,0,0)
windll.user32.GetWindowRect(h, byref(former))
return former
def get_size_screen():
return windll.user32.GetSystemMetrics(0), windll.user32.GetSystemMetrics(1)
screen_width, screen_height = get_size_screen()
print get_pos("NameOfWindows").left
Pour faire disparaitre le cursor
import sys
import os
if os.name == 'nt':
import msvcrt
import ctypes
class _CursorInfo(ctypes.Structure):
_fields_ = [("size", ctypes.c_int),
("visible", ctypes.c_byte)]
def hide_cursor():
if os.name == 'nt':
ci = _CursorInfo()
handle = ctypes.windll.kernel32.GetStdHandle(-11)
ctypes.windll.kernel32.GetConsoleCursorInfo(handle, ctypes.byref(ci))
ci.visible = False
ctypes.windll.kernel32.SetConsoleCursorInfo(handle, ctypes.byref(ci))
elif os.name == 'posix':
sys.stdout.write("\033[?25l")
sys.stdout.flush()
def show_cursor():
if os.name == 'nt':
ci = _CursorInfo()
handle = ctypes.windll.kernel32.GetStdHandle(-11)
ctypes.windll.kernel32.GetConsoleCursorInfo(handle, ctypes.byref(ci))
ci.visible = True
ctypes.windll.kernel32.SetConsoleCursorInfo(handle, ctypes.byref(ci))
elif os.name == 'posix':
sys.stdout.write("\033[?25h")
sys.stdout.flush()