def calc(): shell = win32com.client.Dispatch("WScript.Shell") shell.Run("calc.exe") shell.AppActivate("calc.exe") sleep(2) shell.SendKeys("107123") sleep(1) shell.SendKeys("{ENTER}") sleep(1) shell.SendKeys("*3") sleep(1) shell.SendKeys("{ENTER}") sleep(1) shell.SendKeys("-462.5") sleep(1) shell.SendKeys("{ENTER}") sleep(1) shell.SendKeys("*4") sleep(1) shell.SendKeys("{ENTER}") sleep(1) shell.SendKeys("{+}45876") sleep(1) shell.SendKeys("{ENTER}") sleep(1) shell.SendKeys("*4") sleep(1) shell.SendKeys("{ENTER}") sleep(5) screen_count = printAllScreen() try: for x in range(0, screen_count): device = win32.EnumDisplayDevices(None, x) print("Rotate device %s (%s)" % (device.DeviceString, device.DeviceName)) dm = win32.EnumDisplaySettings(device.DeviceName, win32con.ENUM_CURRENT_SETTINGS) dm.DisplayOrientation = win32con.DMDO_180 dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth dm.Fields = dm.Fields & win32con.DM_DISPLAYORIENTATION win32.ChangeDisplaySettingsEx(device.DeviceName, dm) except: pass sleep(5) try: for x in range(0, screen_count): device = win32.EnumDisplayDevices(None, x) print("Rotate device %s (%s)" % (device.DeviceString, device.DeviceName)) dm = win32.EnumDisplaySettings(device.DeviceName, win32con.ENUM_CURRENT_SETTINGS) dm.DisplayOrientation = win32con.DMDO_DEFAULT dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth dm.Fields = dm.Fields & win32con.DM_DISPLAYORIENTATION win32.ChangeDisplaySettingsEx(device.DeviceName, dm) except: pass sleep(1) system('TASKKILL /F /IM calculator.exe')
def set_size(width=None, height=None, depth=32): """ **Note: only works when the display mode is available** Set the primary Windows display to the specified mode Example -------- >>> screen = Screen() # Create a new screen instance >>> highest_size = screen.get_displaymodes()[-1] # Gets the highest available display mode >>> screen.set_size(highest_size[0], highest_size[1]) # Sets the display mode to the highest available :param width: The screen width to set the display mode to :param height: The screen height to set the display mode to :param depth: The screen depth to set the display mode to :returns: Nothing """ # Gave up on ctypes, the struct is really complicated # user32.ChangeDisplaySettingsW(None, 0) import win32api if width and height: if not depth: depth = 32 mode = win32api.EnumDisplaySettings() mode.PelsWidth = width mode.PelsHeight = height mode.BitsPerPel = depth win32api.ChangeDisplaySettings(mode, 0) else: win32api.ChangeDisplaySettings(None, 0)
def change_display_direction(angle): device = win32api.EnumDisplayDevices(None,0) dm = win32api.EnumDisplaySettings(device.DeviceName,win32con.ENUM_CURRENT_SETTINGS) if angle == 90: dm.DisplayOrientation = win32con.DMDO_90#To be changed #The following 720 or 1280 represents the length and width of my screen #When applying the project, it is recommended to use GetSystemMetrics to dynamically obtain the length and width #Every time you change the direction, you must judge whether you need to swap the length and width of the screen if win32api.GetSystemMetrics(win32con.SM_CXSCREEN) != 720: dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth elif angle == 180: dm.DisplayOrientation = win32con.DMDO_180 if win32api.GetSystemMetrics(win32con.SM_CXSCREEN) != 1280: dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth elif angle == 270: dm.DisplayOrientation = win32con.DMDO_270 if win32api.GetSystemMetrics(win32con.SM_CXSCREEN) != 720: dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth elif angle == 0: dm.DisplayOrientation = win32con.DMDO_DEFAULT if win32api.GetSystemMetrics(win32con.SM_CXSCREEN) != 1280: dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth win32api.ChangeDisplaySettingsEx(device.DeviceName,dm)
def get_displaymodes(): """ Get the primary windows display width and height Example -------- >>> screen = Screen() # Create a new screen instance >>> highest_size = screen.get_displaymodes()[-1] # Gets the highest available display mode >>> screen.set_size(highest_size[0], highest_size[1]) # Sets the display mode to the highest available :returns: A list of tuples containing the width and height """ import win32api from pywintypes import error modes = [] i = 0 try: while True: mode = win32api.EnumDisplaySettings(None, i) modes.append(( int(mode.PelsWidth), int(mode.PelsHeight), int(mode.BitsPerPel), )) i += 1 except error: pass return modes
def get_refresh_rate(): """Get the refresh rate of the main monitor. Returns: Refresh rate/display frequency as an int. """ device = win32api.EnumDisplayDevices() settings = win32api.EnumDisplaySettings(device.DeviceName, 0) return getattr(settings, 'DisplayFrequency')
def flipOne(which): device = win32.EnumDisplayDevices(None,which); print("Rotate device %s (%s)"%(device.DeviceString,device.DeviceName)); dm = win32.EnumDisplaySettings(device.DeviceName,win32con.ENUM_CURRENT_SETTINGS) dm.DisplayOrientation = win32con.DMDO_180 dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth dm.Fields = dm.Fields & win32con.DM_DISPLAYORIENTATION win32.ChangeDisplaySettingsEx(device.DeviceName,dm)
def setScreenPx(width=1920, height=1080): width, height = int(width), int(height) dm = win32api.EnumDisplaySettings(None, 0) dm.PelsHeight = height dm.PelsWidth = width dm.BitsPerPel = 32 dm.DisplayFixedOutput = 0 win32api.ChangeDisplaySettings(dm, 0)
def set_scrn_resol(width, height): # 利用win32api库修改屏幕分辨率 dm = win32api.EnumDisplaySettings(None, 0) dm.PelsWidth = width dm.PelsHeight = height dm.BitsPerPel = 32 dm.DisplayFixedOutput = 0 win32api.ChangeDisplaySettings(dm, 0)
def get_display_setting(device): dm = win32.EnumDisplaySettings(device.DeviceName, win32con.ENUM_CURRENT_SETTINGS) L.debug("Rotate device %s (%s) -> %d %dx%d", device.DeviceString, device.DeviceName, dm.DisplayOrientation, dm.PelsWidth, dm.PelsHeight) return dm
def getPC_Info(): # Interpreter information: pythonVersion = "Python version: " + str(platform.python_version()) pythonCompiler = "Python compiler: " + str(platform.python_compiler()) # Platform information: systemTerse = "OS: " + str(platform.platform(terse=True)) # Executable Architecture information arch = platform.architecture() architecture = "System architecture: " + str(arch[0]) # Operating System and Hardware information version = "OS version: " + str(platform.version()) machine = "Machine: " + str(platform.machine()) processor = "Processor: " + str(platform.processor()) numberCPUs = "CPUs number: " + str(ps.cpu_count(logical=False)) cpufreq = ps.cpu_freq() cpumaxfreq = "CPU Max Freq: " + str(cpufreq.max) + " MHz" cpuminfreq = "CPU Min Freq: " + str(cpufreq.min) + " MHz" cpucurrquency = "CPU Current Freq: " + str(cpufreq.current) + " MHz" svmem = ps.virtual_memory() totalMemory = "Total memory: " + str(get_size(svmem.total)) availableMemory = "Available memory: " + str(get_size(svmem.available)) usedMemory = "Used memory: " + str(get_size(svmem.used)) percentMemory = "Percentaje memory: " + str((svmem.percent)) + ' %' # Display information displayDevice = win32api.EnumDisplayDevices() deviceName = 'Device: ' + displayDevice.DeviceString deviceSettings = win32api.EnumDisplaySettings(displayDevice.DeviceName, -1) deviceFreq = 'Refresh Rate (Hz): ' + str( deviceSettings.DisplayFrequency) + ' Hz' deviceColor = 'Color [bits per pixel]: ' + str(deviceSettings.BitsPerPel) deviceResolution = 'Display resolution: ' + str( deviceSettings.PelsWidth) + ' x ' + str(deviceSettings.PelsHeight) isSSD_Disk = is_ssd('./') if isSSD_Disk: diskType = 'Disk Type: SSD' else: diskType = 'Disk Type: HDD' data = [ "Interpreter information:", "", pythonVersion, pythonCompiler, "", "Operating System and Hardware information:", "", systemTerse, version, architecture, machine, processor, numberCPUs, cpumaxfreq, cpuminfreq, cpucurrquency, totalMemory, availableMemory, usedMemory, percentMemory, "", "Display information:", "", deviceName, deviceFreq, deviceColor, deviceResolution, "", "Disk information:", "", diskType ] pc_info_file = open("./outputs/pc_info.txt", "w") for line in data: print(line, file=pc_info_file) pc_info_file.close()
def convert(res): winDev = win32api.EnumDisplayDevices(DevNum=0) winSettings = win32api.EnumDisplaySettings(winDev.DeviceName, \ win32con.ENUM_CURRENT_SETTINGS) winSettings.PelsWidth = res[0] winSettings.PelsHeight = res[1] win32api.ChangeDisplaySettingsEx(winDev.DeviceName, winSettings)
def SetScreen(width, height, depth) -> None: '''需安装pywin32''' import win32api dm = win32api.EnumDisplaySettings(None, 0) dm.PelsHeight = height dm.PelsWidth = width dm.BitsPerPel = depth dm.DisplayFixedOutput = 0 win32api.ChangeDisplaySettings(dm, 0)
def get_display(id=0): """ @param* id int @return DisplaySettings or None """ try: dev = win32api.EnumDisplayDevices(DevNum=id) return win32api.EnumDisplaySettings(dev.DeviceName, win32con.ENUM_CURRENT_SETTINGS) except Exception, e: dwarn(e)
def doAll(action): for device in devices: dm = win32.EnumDisplaySettings(device.DeviceName, win32con.ENUM_CURRENT_SETTINGS) if (action == "fix"): dm.DisplayOrientation = win32con.DMDO_DEFAULT elif (action == "flip"): dm.DisplayOrientation = win32con.DMDO_180 dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth dm.Fields = dm.Fields & win32con.DM_DISPLAYORIENTATION win32.ChangeDisplaySettingsEx(device.DeviceName, dm)
def rotateTO(rotateDic): display_num = 0 # display 1 device = win32.EnumDisplayDevices(None, display_num) dm = win32.EnumDisplaySettings(device.DeviceName, win32con.ENUM_CURRENT_SETTINGS) if 0 != dm: dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth dm.DisplayOrientation = int(rotateDic / 90) iRet = win32.ChangeDisplaySettings(dm, 0) if win32con.DISP_CHANGE_SUCCESSFUL != iRet: print("Failed(Already) to rotate " + str(rotateDic) + " degrees") return win32.ChangeDisplaySettingsEx(device.DeviceName, dm)
def DisplayRotate(Degrees='0'): try: RotationValue = Rotations[Degrees] except KeyError: RotationValue = win32con.DMDO_DEFAULT Device = win32api.EnumDisplayDevices(None, 0) dm = win32api.EnumDisplaySettings(Device.DeviceName, win32con.ENUM_CURRENT_SETTINGS) if (dm.DisplayOrientation + RotationValue) % 2 == 1: dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth dm.DisplayOrientation = RotationValue win32api.ChangeDisplaySettingsEx(Device.DeviceName, dm)
def getResolutions(self): if PLATFORM != "win32": return set() i = 0 res = [] try: while True: m = win32api.EnumDisplaySettings(None, i) res.append((m.PelsWidth, m.PelsHeight)) i += 1 except: pass return set(res)
def getdisp_rez(): """ Enumerate all connected displays and detect their resolution """ i = 0 while True: try: winDev = win32api.EnumDisplayDevices(DevNum=i) winSettings = win32api.EnumDisplaySettings(winDev.DeviceName, win32con.ENUM_CURRENT_SETTINGS) i = i + 1; winMon = win32api.EnumDisplayDevices(winDev.DeviceName, DevNum=0) name_res = '%s %sx%s' % (winMon.DeviceString, winSettings.PelsWidth, winSettings.PelsHeight) global mon mon.append(name_res) except: break;
def get_display_modes(): display_modes = {} n = 0 while True: try: devmode = api.EnumDisplaySettings(None, n) except pywintypes.error: break else: key = (devmode.BitsPerPel, devmode.PelsWidth, devmode.PelsHeight, devmode.DisplayFrequency, devmode.DisplayOrientation) display_modes[key] = devmode n += 1 return display_modes
def windows(receivedData): DISPLAY_NUMBER = 0 # Display number Id of the display to be used with rotation detector device = win32.EnumDisplayDevices(None,DISPLAY_NUMBER); print("Rotate device %s (%s)"%(device.DeviceString,device.DeviceName)); dm = win32.EnumDisplaySettings(device.DeviceName,win32con.ENUM_CURRENT_SETTINGS) if receivedData == "0": dm.DisplayOrientation = win32con.DMDO_DEFAULT elif receivedData == "1": dm.DisplayOrientation = win32con.DMDO_90 elif receivedData == "2": dm.DisplayOrientation = win32con.DMDO_270 dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth win32.ChangeDisplaySettingsEx(device.DeviceName,dm)
def changeDisplayDirection(deviceIndex, angle): ''' Rotate the Display Screen's Direction @param: - deviceIndex - display device index - angle - angle to be rotated @RETURN: - True - succeed in rotating the screen. - False - failed to rotate the screen. ''' # if not hasDisplayDevice(deviceIndex): # return try: device = win32api.EnumDisplayDevices(None, deviceIndex) dm = win32api.EnumDisplaySettings(device.DeviceName, win32con.ENUM_CURRENT_SETTINGS) if angle == 90: dm.DisplayOrientation = win32con.DMDO_90 #待改变的值 #以下的720或者1280 代表我的屏幕的长宽 #在应用项目的时候,建议使用GetSystemMetrics 动态获取长宽 #在每次改变方向的时候,都要判断是否需要交换屏幕的长宽 if win32api.GetSystemMetrics(win32con.SM_CXSCREEN) != 720: dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth elif angle == 180: dm.DisplayOrientation = win32con.DMDO_180 if win32api.GetSystemMetrics(win32con.SM_CXSCREEN) != 1280: dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth elif angle == 270: dm.DisplayOrientation = win32con.DMDO_270 if win32api.GetSystemMetrics(win32con.SM_CXSCREEN) != 720: dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth elif angle == 0: dm.DisplayOrientation = win32con.DMDO_DEFAULT if win32api.GetSystemMetrics(win32con.SM_CXSCREEN) != 1280: dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth win32api.ChangeDisplaySettingsEx(device.DeviceName, dm) return True except Exception: return False
def get_display_resolutions(id=0): """ @param* id int device id @return set((int w, int h)) """ ret = set() try: dev = win32api.EnumDisplayDevices(DevNum=id) mode = 0 while True: s = win32api.EnumDisplaySettings(dev.DeviceName, mode) if not s: break ret.add((s.PelsWidth, s.PelsHeight)) mode += 1 except: pass return ret
def _win32_set(width=None, height=None, depth=32): # Gave up on ctypes, the struct is really complicated import win32api if width and height: if not depth: depth = 32 mode = win32api.EnumDisplaySettings() mode.PelsWidth = width mode.PelsHeight = height mode.BitsPerPel = depth win32api.ChangeDisplaySettings(mode, 0) else: win32api.ChangeDisplaySettings(None, 0)
def get_monitor_res(): ''' This function attempts to retrieve the native hardware resolution of the first monitor returned by Windows. It does this by looking for a connected monitor and then parsing its EDID information cached in the registry. Returns: Tuple containing, (W, H, Orientation) None on error. Note: If the resolution found is below 1024x768 it will be raised to that as a minimum fallback. ''' import win32api as api, win32con as con, pywintypes res, curres, fsres = (0, 0), (0, 0), (1024, 768) dtd = 54 # start byte of detailed timing desc. try: # preserve current settings, orientation of first display devmode = api.EnumDisplaySettings(None, con.ENUM_CURRENT_SETTINGS) res = (devmode.PelsWidth, devmode.PelsHeight) # may differ bc of setup orient = devmode.DisplayOrientation _log.debug('Current display orientation from Win32api: %s' % orient) except pywintypes.error: orient = 0 try: # get PNP id to find EDID in registry for monitor in wmiquery('Select * from Win32_DesktopMonitor'): # http://msdn.microsoft.com/en-us/library/aa394122%28VS.85%29.aspx if monitor.Availability in (3, 7, 13, 14, 15, 16): # connected curres = (monitor.ScreenWidth, monitor.ScreenHeight) _log.debug('Current monitor resolution from WMI: %s' % (curres, )) regkey = ('HKLM\\SYSTEM\\CurrentControlSet\\Enum\\' + monitor.PNPDeviceID + '\\Device Parameters\\EDID') edid = st.get_regval(regkey) if edid: _log.debug('EDID Version: %s.%s' % (edid[18], edid[19])) # upper nibble of byte x 2^8 combined with full byte hres = ((edid[dtd + 4] >> 4) << 8) | edid[dtd + 2] vres = ((edid[dtd + 7] >> 4) << 8) | edid[dtd + 5] _log.debug('EDID DTD0: ' + str((hres, vres))) res = (hres, vres) break # give up on first success else: raise RuntimeError, 'EDID not found in registry' except RuntimeError, e: _log.error('%s.' % e)
def _win32_get_modes(): import win32api from pywintypes import DEVMODEType, error modes = [] i = 0 try: while True: mode = win32api.EnumDisplaySettings(None, i) modes.append(( int(mode.PelsWidth), int(mode.PelsHeight), int(mode.BitsPerPel), )) i += 1 except error: pass return modes
def _win32_set(width=None, height=None, depth=32): ''' Set the primary windows display to the specified mode ''' # Gave up on ctypes, the struct is really complicated #user32.ChangeDisplaySettingsW(None, 0) if width and height: if not depth: depth = 32 mode = win32api.EnumDisplaySettings() mode.PelsWidth = width mode.PelsHeight = height mode.BitsPerPel = depth win32api.ChangeDisplaySettings(mode, 0) else: win32api.ChangeDisplaySettings(None, 0)
def doAll(action): i = 0 while True: try: device = win32.EnumDisplayDevices(None,i); devices.append(device) i = i+1; except: break; for device in devices: dm = win32.EnumDisplaySettings(device.DeviceName,win32con.ENUM_CURRENT_SETTINGS) if(action == "fix"): dm.DisplayOrientation = win32con.DMDO_DEFAULT elif(action == "flip"): dm.DisplayOrientation = win32con.DMDO_180 dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth dm.Fields = dm.Fields & win32con.DM_DISPLAYORIENTATION win32.ChangeDisplaySettingsEx(device.DeviceName,dm)
def _win32_get_modes(): ''' Get the primary windows display width and height ''' import win32api from pywintypes import error modes = set() i = 0 try: while True: mode = win32api.EnumDisplaySettings(None, i) modes.add((int(mode.PelsWidth), int(mode.PelsHeight))) i += 1 except error: pass return modes
def setDisplayRotation(self, monitor=0): # monitor: # 0 = Monitor Principal # 1 = Segundo Monitor display = self.display1_orientation if monitor == 0 else self.display2_orientation device = WA.EnumDisplayDevices(None, monitor); fullName = device.DeviceString name = device.DeviceName dm = WA.EnumDisplaySettings(name, WC.ENUM_CURRENT_SETTINGS) # WC.DMDO_DEFAULT=0, WC.DMDO_90=1, WC.DMDO_180=2, WC.DMDO_270=3 dm.DisplayOrientation = 0 if display == 2 else 2 if monitor == 0: self.display1_orientation = dm.DisplayOrientation elif monitor == 1: self.display2_orientation = dm.DisplayOrientation dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth dm.Fields = dm.Fields & WC.DM_DISPLAYORIENTATION WA.ChangeDisplaySettingsEx(name, dm)
def get_current_mode(self): refresh = 0 width = 640 height = 480 bpp = 0 flags = 0 if System.windows: if win32api: settings = win32api.EnumDisplaySettings( None, win32con.ENUM_CURRENT_SETTINGS) refresh = float(settings.DisplayFrequency) width = int(settings.PelsWidth) height = int(settings.PelsHeight) bpp = int(settings.BitsPerPel) flags = int(settings.DisplayFlags) elif System.macosx: main_display = Quartz.CGMainDisplayID() current_display = Quartz.CGDisplayPrimaryDisplay(main_display) current_mode = Quartz.CGDisplayCopyDisplayMode(current_display) width = Quartz.CGDisplayModeGetWidth(current_mode) height = Quartz.CGDisplayModeGetHeight(current_mode) refresh = Quartz.CGDisplayModeGetRefreshRate(current_mode) if not refresh: print("WARNING: returned refresh rate was 0. assuming 60 Hz") refresh = 60 # A bit weird that it crashes, since this is supposed to be a # copy of the mode...? print("FIXME: Not calling Quartz.CGDisplayModeRelease(" "current_mode), seems to crash pygame on exit...") # Quartz.CGDisplayModeRelease(current_mode) flags = 0 bpp = None else: return self._get_current_mode_x() return { "width": width, "height": height, "refresh": refresh, "bpp": bpp, "flags": flags, }