def ScreenshotByWin32API(): hwnd = win32gui.GetDesktopWindow() hwndDC = win32gui.GetWindowDC(hwnd) mfcDC = win32ui.CreateDCFromHandle(hwndDC) saveDC = mfcDC.CreateCompatibleDC() saveBitMap = win32ui.CreateBitmap() w = win32print.GetDeviceCaps(hwndDC, win32con.DESKTOPHORZRES) h = win32print.GetDeviceCaps(hwndDC, win32con.DESKTOPVERTRES) saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) saveDC.SelectObject(saveBitMap) saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY) #add cursor curFlags, curH, (curX, curY) = win32gui.GetCursorInfo() if curH != 0: saveDC.DrawIcon((curX, curY), curH) #see https://stackoverflow.com/questions/5999007/active-window-screenshot-with-python-pil-and-windows-api-how-to-deal-with-round bmpinfo = saveBitMap.GetInfo() bmpstr = saveBitMap.GetBitmapBits(True) im = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, 'raw', 'BGRX', 0, 1) #release memory win32gui.DeleteObject(saveBitMap.GetHandle()) saveDC.DeleteDC() mfcDC.DeleteDC() win32gui.ReleaseDC(hwnd, hwndDC) return im
def get_real_size(): hDC = win32gui.GetDC(0) # 横向分辨率 w = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES) # 纵向分辨率 h = win32print.GetDeviceCaps(hDC, win32con.DESKTOPVERTRES) return w, h
def getWH(self): """获取真实的分辨率""" hDC = win32gui.GetDC(0) # 横向分辨率 w = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES) # 纵向分辨率 h = win32print.GetDeviceCaps(hDC, win32con.DESKTOPVERTRES) return w,h
def get_real_resolution(self): # 获取真实的分辨率 # 避免高分屏因为Windows系统放大像素导致的错误 hDC = win32gui.GetDC(0) # 横向分辨率 w = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES) # 纵向分辨率 h = win32print.GetDeviceCaps(hDC, win32con.DESKTOPVERTRES) return w, h
def getMousePos(): hDC = win32gui.GetDC(0) pos = win32api.GetCursorPos() width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN) height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN) w = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES) h = win32print.GetDeviceCaps(hDC, win32con.DESKTOPVERTRES) zoomx = w / width zoomh = h / height pos = (int(pos[0] * zoomx), int(pos[1] * zoomh)) return pos
def get_windows_color_depth(): import win32gui, win32con, win32print hwin = win32gui.GetDesktopWindow() hwindc = win32gui.GetWindowDC(hwin) ans = win32print.GetDeviceCaps(hwindc, win32con.BITSPIXEL) win32gui.ReleaseDC(hwin, hwindc) return ans
def get_resolution(self): hDC = win32gui.GetDC(0) self.resolution = win32print.GetDeviceCaps( hDC, win32con.DESKTOPHORZRES) / win32api.GetSystemMetrics( win32con.SM_CXSCREEN) self.logger.get_log().debug('屏幕分辨率为:' + str(self.resolution)) return None
def getScale(self): hDC = win32gui.GetDC(0) widthOriginal = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES) #heightOriginal = win32print.GetDeviceCaps(hDC, win32con.DESKTOPVERTRES) #dpiOriginal = win32print.GetDeviceCaps(hDC, win32con.LOGPIXELSX) widthReal = GetSystemMetrics(0) #heightReal = GetSystemMetrics(1) return widthOriginal / widthReal
def convert_window_ltrb(display_ltrb): # get屏幕缩放 zoom_horz = GetSystemMetrics(0) zoom_vert = GetSystemMetrics(1) # get分辨率设置 hdc = win32gui.GetDC(0) res_horz = win32print.GetDeviceCaps(hdc, win32con.DESKTOPHORZRES) res_vert = win32print.GetDeviceCaps(hdc, win32con.DESKTOPVERTRES) # 修正尺寸 left = round(display_ltrb[0] / zoom_horz * res_horz) top = round(display_ltrb[1] / zoom_vert * res_vert) right = round(display_ltrb[2] / zoom_horz * res_horz) bottom = round(display_ltrb[3] / zoom_vert * res_vert) return (left, top, right, bottom)
def get_scale(): h_dc = win32gui.GetDC(0) # 系统向显示器输出的分辨率 width = win32api.GetSystemMetrics(win32con.SM_CXSCREEN) # 显示器的分辨率 HORZRES = win32print.GetDeviceCaps(h_dc, win32con.DESKTOPHORZRES) # 纵向分辨率 # VERTRES = win32print.GetDeviceCaps(h_dc, win32con.DESKTOPVERTRES) return HORZRES / width
def getDPI(self): #获取win10的缩放比例 ''' win10的缩放比例设置比较奇怪 根据网上资料高级自定义缩放和默认缩放不一致,需要设置两个参数,比较后返回 当用户使用默认方法设置缩放时,应该使用GetDeviceCaps(DESKTOPHORZRES)/GetDeviceCaps(HORZRES)获得缩放比例 当用户使用高级模式中的自定义缩放时,应该使用GetDeviceCaps(LOGPIXELSX)再除以0.96(再除以100,因为算出来的数字是百分比) 该方法待研究,Image.crop方法似乎在非100%缩放情况下无法直接正常工作,留待后续研究,使用ctypes.windll.user32.SetProcessDPIAware()暂时处理 ''' hDC = win32gui.GetDC(0) dpiA = win32print.GetDeviceCaps( hDC, win32con.DESKTOPHORZRES) / win32print.GetDeviceCaps( hDC, win32con.HORZRES) dpiB = win32print.GetDeviceCaps(hDC, win32con.LOGPIXELSX) / 0.96 / 100 if dpiA == 1: return dpiB elif dpiB == 1: return dpiA elif dpiA == dpiB: return dpiA else: return None
def main(*args): # check platform # platform is stored in string platform # platform = platform.system() # if(platform == 'Windows'): # print("OS: Windows") # elif(platform == 'Linux'): # print("OS: Linux") # else: # print("OS: iOS") num_args = len(sys.argv) print("Num textblocks: ", ((num_args -7) /5)) if num_args == 7: print("No textblocks") elif (num_args -7 ) % 5 == 0: print("Ok") else: print("Not a valid input") def make_textblock(text, posx, posy, style, size): textblock = TextBlock(text, posx, posy, style, size) return textblock class TextBlock(object): """ store text_field data """ text = "" posx = 0 posy = 0 font_style = "" font_size = 0 def __init__(self, text, posx, posy, font_style, font_size): self.text = text self.posx = int(posx) self.posy = int(posy) self.font_style = font_style self.font_size = int(font_size) font_family = sys.argv[1] img_posx = int(sys.argv[2]) img_posy = int(sys.argv[3]) img_path = sys.argv[4] img_height = int(sys.argv[5]) img_width = int(sys.argv[6]) text_blocks = [] arg_inc = 7 if (num_args == 7): print("No textblocks") else: while(arg_inc<num_args): text_blocks.append(make_textblock(sys.argv[arg_inc], sys.argv[arg_inc+1], sys.argv[arg_inc+2], sys.argv[arg_inc+3], sys.argv[arg_inc+4])) arg_inc = arg_inc + 5 print("Font family: " + font_family) print("img_posx: " , img_posx) print("img_posy: " , img_posy) print("img_path: " + img_path) print("img_height: " , img_height) print("img_width: " , img_width) for text_block in text_blocks: print("Field " + text_block.text + "'s text: " + text_block.text) print("Field " + text_block.text + "'s posx: ", text_block.posx) print("Field " + text_block.text + "'s posy: ", text_block.posy) print("Field " + text_block.text + "'s font style: " + text_block.font_style) print("Field " + text_block.text + "'s font size: ", text_block.font_size) # if you just want to use the default printer, you need # to retrieve its name. printer = win32print.GetDefaultPrinter() print("Printer: " + printer) # open the printer. hprinter = win32print.OpenPrinter(printer) # retrieve default settings. this code does not work on # win95/98, as GetPrinter does not accept two devmode = win32print.GetPrinter(hprinter, 2)["pDevMode"] # # Taken from print_desktop.py # Not sure what it does # # dmsize=win32print.DocumentProperties(0, hprinter, printer, None, None, 0) # ## dmDriverExtra should be total size - fixed size # driverextra=dmsize - pywintypes.DEVMODEType().Size ## need a better way to get DEVMODE.dmSize # dm=pywintypes.DEVMODEType(driverextra) # dm.Fields=dm.Fields|win32con.DM_ORIENTATION|win32con.DM_COPIES # dm.Orientation=win32con.DMORIENT_LANDSCAPE # dm.Copies=2 # win32print.DocumentProperties(0, hprinter, printer, dm, dm, win32con.DM_IN_BUFFER|win32con.DM_OUT_BUFFER) # change paper size and orientation # constants are available here: # http://msdn.microsoft.com/library/default.asp? # url=/library/en-us/intl/nls_Paper_Sizes.asp # number 10 envelope is 20 # This doesn't do anything upon inspection # devmode.PaperSize = 333 # devmode.Orientation = 2 # create dc using new settings. # first get the integer hDC value. note that we need the name. hdc = win32gui.CreateDC("WINSPOOL", printer, devmode) printerwidth=win32print.GetDeviceCaps(hdc, win32con.PHYSICALWIDTH) printerheight=win32print.GetDeviceCaps(hdc, win32con.PHYSICALHEIGHT) # next create a PyCDC from the hDC. dc = win32ui.CreateDCFromHandle(hdc) print("Printer length x width:") print(printerwidth) print(printerheight) # now you can set the map mode, etc. and actually print. # you need to set the map mode mainly so you know how # to scale your output. I do everything in points, so setting # the map mode as "twips" works for me. dc.SetMapMode(win32con.MM_TWIPS) # 1440 per inch # here's that scaling I mentioned: scale_factor = 20 # i.e. 20 twips to the point # Variables # font_size = 30 bmp = Image.open (img_path) print("bmp.size[0]") print(bmp.size[0]) print("bmp.size[1]") print(bmp.size[1]) # if bmp.size[0] > bmp.size[1]: # bmp = bmp.rotate (90) # font = win32ui.CreateFont({ # "name": font_family, # "height": int(scale_factor * font_size), # "weight": 1, # }) # 1 inch = scale_factor * 72 # 1 inch = 1440 twips # start the document. the description variable is a string # which will appear in the print queue to identify the job. dc.StartDoc("Nametag printjob") dib = ImageWin.Dib (bmp) # # SAMPLE, EXPLICIT COORDINATE DRAW # 2.40" x 3.90" with 0.12" FEED # # dib.draw (dc.GetHandleOutput (), ( # int(1.39 * scale_factor * 72), # int(.2 * scale_factor * -72), # int(2.39 * scale_factor * 72), # int(1.2 * scale_factor * -72) # )) # dc.TextOut(int(.89 * scale_factor * 72), int(1.3 * scale_factor * -72), "Grant") # dc.TextOut(int(1.89 * scale_factor * 72), int(1.3 * scale_factor * -72), "George") # dc.SelectObject(font_small) # dc.TextOut(int(1.49 * scale_factor * 72), int(1.8 * scale_factor * -72), "Title") # END SAMPLE dib.draw (dc.GetHandleOutput (), ( img_posx, img_posy * -1, img_posx + img_width, (img_posy + img_height) * -1 )) # to draw anything (other than text) you need a pen. # the variables are pen style, pen width and pen color. pen = win32ui.CreatePen(0, int(scale_factor), 0L) # SelectObject is used to apply a pen or font object to a dc. dc.SelectObject(pen) # again with the SelectObject call. # dc.SelectObject(font) for text_block in text_blocks: font = win32ui.CreateFont({ "name": font_family, "height": scale_factor * text_block.font_size, "weight": 1 }) dc.SelectObject(font) print (text_block.text) print (text_block.posx) print (text_block.posy) dc.TextOut(text_block.posx, text_block.posy * -1, text_block.text) # Half Vertical Line # dc.MoveTo((int(1.89 * scale_factor * 72), int(0 *scale_factor * -72))) # dc.LineTo((int(1.89 * scale_factor * 72), int(2.4 * scale_factor* -72))) # # Half Horizontal Line # dc.MoveTo((int(0 * scale_factor * 72), int(1.2 *scale_factor * -72))) # dc.LineTo((int(3.78 * scale_factor * 72), int(1.2 * scale_factor* -72))) # # 1/3 Horizontal Line # dc.MoveTo((int(0 * scale_factor * 72), int(.8 *scale_factor * -72))) # dc.LineTo((int(3.78 * scale_factor * 72), int(.8 * scale_factor* -72))) # # 2/3 Horizontal Line # dc.MoveTo((int(0 * scale_factor * 72), int(1.6 *scale_factor * -72))) # dc.LineTo((int(3.78 * scale_factor * 72), int(1.6 * scale_factor* -72))) # must not forget to tell Windows we're done. dc.EndDoc() dc.DeleteDC()
def getScreenSize(): hDC = win32gui.GetDC(0) width_mm = win32print.GetDeviceCaps(hDC, win32con.HORZSIZE) height_mm = win32print.GetDeviceCaps(hDC, win32con.VERTSIZE) return width_mm, height_mm
print('Printer handle: ', p) print_processor = win32print.GetPrinter(p, 2)['pPrintProcessor'] ## call with last parm set to 0 to get total size needed for printer's DEVMODE dmsize = win32print.DocumentProperties(0, p, pname, None, None, 0) ## dmDriverExtra should be total size - fixed size driverextra = dmsize - pywintypes.DEVMODEType( ).Size ## need a better way to get DEVMODE.dmSize dm = pywintypes.DEVMODEType(driverextra) dm.Fields = dm.Fields | win32con.DM_ORIENTATION | win32con.DM_COPIES dm.Orientation = win32con.DMORIENT_LANDSCAPE dm.Copies = 2 win32print.DocumentProperties(0, p, pname, dm, dm, win32con.DM_IN_BUFFER | win32con.DM_OUT_BUFFER) pDC = win32gui.CreateDC(print_processor, pname, dm) printerwidth = win32print.GetDeviceCaps(pDC, win32con.PHYSICALWIDTH) printerheight = win32print.GetDeviceCaps(pDC, win32con.PHYSICALHEIGHT) hwnd = win32gui.GetDesktopWindow() l, t, r, b = win32gui.GetWindowRect(hwnd) desktopheight = b - t desktopwidth = r - l dDC = win32gui.GetWindowDC(hwnd) dcDC = win32gui.CreateCompatibleDC(dDC) dcBM = win32gui.CreateCompatibleBitmap(dDC, desktopwidth, desktopheight) win32gui.SelectObject(dcDC, dcBM) win32gui.StretchBlt(dcDC, 0, 0, desktopwidth, desktopheight, dDC, 0, 0, desktopwidth, desktopheight, win32con.SRCCOPY) pcDC = win32gui.CreateCompatibleDC(pDC)
@Author : Wang.Yu @Version : 1.0 @Contact : [email protected] @License : (C)Copyright 2021, Personal exclusive right. ''' import pdb import logging import time import win32gui import win32con import win32print import lackey TIMEOUT_LIMIT = 80 # 8s WORK_AREA = lackey.Region( 0, 0, win32print.GetDeviceCaps(win32gui.GetDC(0), win32con.DESKTOPHORZRES), win32print.GetDeviceCaps(win32gui.GetDC(0), win32con.DESKTOPVERTRES)) # (x, y, w, h = args) DEFAULT_SIMILARITY_THRESHOLD = 0.85 # init screen scope def initGlobalRegion(uiaRect): global WORK_AREA left = uiaRect.left top = uiaRect.top width = uiaRect.right - uiaRect.left height = uiaRect.bottom - uiaRect.top WORK_AREA = lackey.Region(left, top, width, height) logging.info('Working area is (%d,%d,%d,%d)' % (left, top, width, height))
def getScreenRes(): hDC = win32gui.GetDC(0) width = win32print.GetDeviceCaps(hDC, win32con.HORZRES) height = win32print.GetDeviceCaps(hDC, win32con.VERTRES) return width, height
# ----------------------------------画窗口----------------------------------# # 主窗口 win = tkinter.Tk() ico_path = resource_path(r'./OneDrive.ico') if os.path.exists(ico_path): win.iconbitmap(ico_path) win.title('OneDrive for Business 直链') # 窗口分辨率及位置设置 # https://stackoverflow.com/questions/41315873/attempting-to-resolve-blurred-tkinter-text-scaling-on-windows-10-high-dpi-disp # win.winfo_screenheight() & win.winfo_screenwidth() 会根据Windows缩放比例变化,无法计算正确的 dpi # 参考 https://www.cnblogs.com/micenote/p/12165669.html 获取屏幕分辨率 hDC = win32gui.GetDC(0) # 横向分辨率 HORZRES = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES) # 纵向分辨率 VERTRES = win32print.GetDeviceCaps(hDC, win32con.DESKTOPVERTRES) # PROCESS_DPI_UNAWARE = 0, # PROCESS_SYSTEM_DPI_AWARE = 1, # PROCESS_PER_MONITOR_DPI_AWARE = 2 ctypes.windll.shcore.SetProcessDpiAwareness(1) win.tk.call('tk', 'scaling', get_dpi() / 72) win.geometry( ("%dx%d+%d+%d" % (HORZRES / 2, VERTRES / 2, HORZRES / 4, VERTRES / 4))) # 原链接 LabelFrame frame_oriStr = ttk.LabelFrame(win, text="原链接", labelanchor="nw") # rel 代表 relative frame_oriStr.place(relx=0.01, rely=0.03, relwidth=0.98, relheight=0.2) # 获取剪贴板,设置 Entry 组件
def get_handle(resolution=[1920, 1080], order=0, sim="ANY"): #now only the 夜神 is supported # sim : ANY表示为任何第一个检测到的窗体, 其余为config上的第一个名称 handlelist = [] handle_infor = globalvar.get_handle_infor() win32gui.EnumWindows( lambda hWnd, param: param.append( [hWnd, win32gui.GetClassName(hWnd), win32gui.GetWindowText(hWnd)]), handlelist) exist = False for i in range(0, len(handle_infor), 2): win = win32gui.FindWindow(None, handle_infor[i]) if win == 0: continue else: if handle_infor[i + 1] == 'None': if sim in ['ANY', handle_infor[i]]: tmp_list = [] exist = True for i1, i2, i3 in handlelist: if i3 == handle_infor[i]: tmp_list.append(i1) win = tmp_list[order] break else: continue else: hWndChildList = [] win32gui.EnumChildWindows( win, lambda hWnd, param: param.append([ hWnd, win32gui.GetClassName(hWnd), win32gui.GetWindowText(hWnd) ]) if win32gui.GetWindowText(hWnd) in [handle_infor[i + 1]] else None, hWndChildList) try: win = hWndChildList[0][0] print("当前检测到{}".format(handle_infor[i])) if sim in ['ANY', handle_infor[i]]: exist = True break else: continue except: continue if exist == False: return -1 # win = win32gui.FindWindow(None, handle_infor[0]) # if win==0: # win = win32gui.FindWindow(None, handle_infor[2]) # hWndChildList = [] # win32gui.EnumChildWindows(win, lambda hWnd, param: param.append([hWnd # , win32gui.GetClassName(hWnd) # , win32gui.GetWindowText(hWnd)]) # if win32gui.GetWindowText(hWnd) in [handle_infor[3]] else None, hWndChildList) # try: # win = hWndChildList[0][0] # print("当前为mumu模拟器") # except: # return -1 # else: # hWndChildList = [] # win32gui.EnumChildWindows(win, lambda hWnd, param: param.append([hWnd # , win32gui.GetClassName(hWnd) # , win32gui.GetWindowText(hWnd)]) # if win32gui.GetWindowText(hWnd) in ['QWidgetClassWindow',handle_infor[1]] else None, hWndChildList) # try: # win = hWndChildList[0][0] # print("当前为夜神模拟器") # except: # return -1 # rect = win32gui.GetWindowRect(win) hwndDC = win32gui.GetDC(0) defalut_xdpi = 96 default_ydpi = 96 # 创建设备描述表 x_dpi = win32print.GetDeviceCaps(hwndDC, win32con.LOGPIXELSX) y_dpi = win32print.GetDeviceCaps(hwndDC, win32con.LOGPIXELSY) print("x轴dpi{}".format(x_dpi)) print("y轴dpi{}".format(y_dpi)) left, top, right, bot = win32gui.GetWindowRect(win) # w = int((right - left) * (x_dpi/defalut_xdpi)) # h = int((bot - top) * (y_dpi/default_ydpi)) w = int((right - left)) h = int((bot - top)) globalvar.set_window_resolution([w, h]) print("当前窗体大小为{}x{}".format(w, h)) if w == resolution[0] and h == resolution[1]: pass else: #print('resolution isn\'t {}p'.format(resolution[1])) pass return win
def get_real_resolution(): hdc = win32gui.GetDC(0) w = win32print.GetDeviceCaps(hdc, win32con.DESKTOPHORZRES) h = win32print.GetDeviceCaps(hdc, win32con.DESKTOPVERTRES) return w, h
# 'hub': '192.168.0.118:48000', # 'platform': 'WINDOWS', # 'version': '10', # 'user_dir': r'C:\Users\tedv\AppData\Local\Google\Chrome\User Data', # 'grid_dir': r'C:\Soft\selenium' # } # RPCServer # RPC_Server = '192.168.0.150' # 废弃 自动获取 RPC_Server_Port = 8888 # 上传文件路径 FILE_DIR = os.path.join(BASE_DIR, 'Upload') # 显示器DPI缩放 # DPI = 1.25 # DPI改自动获取 hDC = win32gui.GetDC(0) DEFAULT_DPI = win32print.GetDeviceCaps( hDC, win32con.DESKTOPHORZRES) / win32print.GetDeviceCaps( hDC, win32con.HORZRES) ADVANCED_DPI = win32print.GetDeviceCaps(hDC, win32con.LOGPIXELSX) / 0.96 / 100 DPI = DEFAULT_DPI if ADVANCED_DPI == 1.0 else ADVANCED_DPI # 邮箱配置 MAIL = False SENDER = '*****@*****.**' SENDER_NAME = 'Ted' SMTP_CODE = '' RECEIVERS = [ '*****@*****.**', ] # 更新脚本路径 UPDATE_BAT_DIR = os.path.join(BASE_DIR, 'Utils', 'RPC') # 注册测试集配置文件 RPC_SERVER_SUITES = ['register.yaml']
def get_dpi(): hDC = win32gui.GetDC(0) # dpi = win32print.GetDeviceCaps(hDC, win32con.LOGPIXELSX) w = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES) h = win32print.GetDeviceCaps(hDC, win32con.DESKTOPVERTRES) return w,h
def get_zoom_ratio(): hdc = win32gui.GetDC(0) dpi = win32print.GetDeviceCaps(hdc, win32con.LOGPIXELSX) return round(dpi / 96, 2)