Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

How do I use the Windows Magnification API in Python to invert the screen?

In Python, how can you invert the entire screen on a Windows PC without using keyboard shortcuts (as these are unreliable and can be turned off easily) using the ctypes module?

I've noticed it's possible using the Windows Magnification API, but that is designed for C. Using ctypes, I may be able to interface with the DLL, and invert the screen. I can initialize it:

from ctypes import *

class RECT(Structure):
    _fields_ = [('left', c_long),
                ('top', c_long),
                ('right', c_long),
                ('bottom', c_long)]
magnification_api = CDLL('magnification.dll')

# declare types
BOOL = c_bool
FLOAT = c_float
INT = c_int
LPRECT = LPRECT = POINTER(RECT)
PBOOL = PBOOL = POINTER(c_bool)

# MagInitialize
magnification_api.MagInitialize.restype = BOOL

# MagUninitialize 
magnification_api.MagUninitialize.restype = BOOL

magnification_api.MagInitialize() # initialize the API

magnification_api.MagUninitialize() # uninitialize

How do I use the API to invert the screen?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the Windows Magnification API through the ctypes library:

from ctypes import *

class RECT(Structure):
    _fields_ = [('left', c_long),
                ('top', c_long),
                ('right', c_long),
                ('bottom', c_long)]
magnification_api = CDLL('magnification.dll')

# declare types
BOOL = c_bool
FLOAT = c_float
INT = c_int
LPRECT = LPRECT = POINTER(RECT)
PBOOL = PBOOL = POINTER(c_bool)
PMAGCOLOREFFECT = c_float * 25
MAGCOLOREFFECT = MAGCOLOREFFECT = POINTER(PMAGCOLOREFFECT)

# MagInitialize
magnification_api.MagInitialize.restype = BOOL

# MagUninitialize 
magnification_api.MagUninitialize.restype = BOOL

# MagSetFullscreenColorEffect
magnification_api.MagSetFullscreenColorEffect.restype = BOOL
magnification_api.MagSetFullscreenColorEffect.argtypes = (MAGCOLOREFFECT,)

magnification_api.MagInitialize() # initialize the API

magnification_api.MagSetFullscreenColorEffect((c_float * 25)(-1, 0, 0, 0, 0, 0, -1, 0, 0,  0, 0,  0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1)) # invert the screen

magnification_api.MagUninitialize() # use this to reset

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...