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 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 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 set_display_orientation(device, orientation): dm = get_display_setting(device) if need_swap_pels(dm.DisplayOrientation, orientation): dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth dm.DisplayOrientation = orientation dm.Fields = win32con.DM_DISPLAYORIENTATION | win32con.DM_PELSWIDTH | win32con.DM_PELSHEIGHT result = win32.ChangeDisplaySettingsEx(device.DeviceName, dm) return result
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 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 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 rotate_to(self, degrees): if degrees == 90: rotation_val = win32con.DMDO_90 elif degrees == 180: rotation_val = win32con.DMDO_180 elif degrees == 270: rotation_val = win32con.DMDO_270 elif degrees == 0: rotation_val = win32con.DMDO_DEFAULT else: raise ValueError( "Display can only be rotated to 0, 90, 180, or 270 degrees.") dm = self.devicemodeW if ((dm.DisplayOrientation + rotation_val) % 2 == 1): dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth dm.DisplayOrientation = rotation_val win32api.ChangeDisplaySettingsEx(self.device, dm)
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 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 apply_config(self) -> int: """ apply the config changes to windows. :return: successful when it return 0 """ return win32api.ChangeDisplaySettingsEx(self.adapter.DeviceName, self.config)