def OnPaint(self, hwnd, msg, wp, lp): dc, ps = win32gui.BeginPaint(hwnd) wndrect = win32gui.GetClientRect(hwnd) wndwidth = wndrect[2] - wndrect[0] wndheight = wndrect[3] - wndrect[1] win32clipboard.OpenClipboard() try: try: hbitmap = win32clipboard.GetClipboardData( win32clipboard.CF_BITMAP) except TypeError: font = win32gui.LOGFONT() font.lfHeight = 15 #int(wndheight/20) font.lfWidth = 15 #font.lfHeight # font.lfWeight=150 hf = win32gui.CreateFontIndirect(font) win32gui.SelectObject(dc, hf) win32gui.SetBkMode(dc, win32con.TRANSPARENT) win32gui.SetTextColor(dc, win32api.RGB(0, 0, 0)) win32gui.DrawText( dc, 'No bitmaps are in the clipboard\n(try pressing the PrtScn button)', -1, (0, 0, wndwidth, wndheight), win32con.DT_CENTER) else: bminfo = win32gui.GetObject(hbitmap) dcDC = win32gui.CreateCompatibleDC(None) win32gui.SelectObject(dcDC, hbitmap) win32gui.StretchBlt(dc, 0, 0, wndwidth, wndheight, dcDC, 0, 0, bminfo.bmWidth, bminfo.bmHeight, win32con.SRCCOPY) win32gui.DeleteDC(dcDC) win32gui.EndPaint(hwnd, ps) finally: win32clipboard.CloseClipboard() return 0
def IconToFile(hlib, tmpDirName, group_name): libc = ctypes.CDLL(ctypes.util.find_library('c')) libc.memcpy.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t] libc.memcpy.restype = ctypes.c_char_p # patch FindResourceW, ctypes.windll.kernel32.SizeofResource FindResourceW = ctypes.windll.kernel32.FindResourceW FindResourceW.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p ] FindResourceW.restype = ctypes.c_void_p SizeofResource = ctypes.windll.kernel32.SizeofResource SizeofResource.argtypes = [ctypes.c_void_p, ctypes.c_void_p] SizeofResource.restype = ctypes.c_size_t hRes = win32api.LoadResource(hlib, win32con.RT_GROUP_ICON, group_name) mem_icon_dir = ctypes.windll.kernel32.LockResource(hRes) # 32 bits color; 16 and 256 colors are too old icon_size = 256 icon_name = ctypes.windll.user32.LookupIconIdFromDirectoryEx( mem_icon_dir, True, icon_size, icon_size, 0x00000000) sys.stderr.write("icon_name=%s\n" % str(icon_name)) hResInfo = FindResourceW(hlib, icon_name, win32con.RT_ICON) size = ctypes.windll.kernel32.SizeofResource(hlib, hResInfo) rec = win32api.LoadResource(hlib, win32con.RT_ICON, icon_name) mem_icon = ctypes.windll.kernel32.LockResource(rec) # And this is some differ (copy data to Python buffer) binary_data = (ctypes.c_ubyte * size)() libc.memcpy(binary_data, mem_icon, size) hIconRet = ctypes.windll.user32.CreateIconFromResourceEx( binary_data, size, True, 0x00030000, 0, 0, 0x00000000) info = win32gui.GetIconInfo(hIconRet) bminfo = win32gui.GetObject(info[4]) # generate bitmap by drawing the icon hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0)) hbmp = win32ui.CreateBitmap() hbmp.CreateCompatibleBitmap(hdc, bminfo.bmWidth, bminfo.bmHeight) hcdc = hdc.CreateCompatibleDC() hcdc.SelectObject(hbmp) win32gui.DrawIconEx(hcdc.GetHandleOutput(), 0, 0, hIconRet, bminfo.bmWidth, bminfo.bmHeight, 0, 0, 0x0003) # Need temp directory or generate image on the fly, for inclusion into an HTML page. # Temp file visible from HTTP server. # An alternative is to send the file content to a socket stream. # MIME type is "image/bmp" imgFilNam = "icon-%03dx%03d-%05d-%03d.bmp" % ( bminfo.bmWidth, bminfo.bmHeight, group_name, icon_name) if tmpDirName: imgFilNam = tmpDirName + imgFilNam sys.stderr.write("Generating %s\n" % imgFilNam) hbmp.SaveBitmapFile(hcdc, imgFilNam) win32gui.DestroyIcon(hIconRet) return imgFilNam
def IconToFile(hlib, group_name): """The group might be a string or an integer and its type must be kept.""" # patch FindResourceW, ctypes.windll.kernel32.SizeofResource FindResourceW = ctypes.windll.kernel32.FindResourceW FindResourceW.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p ] FindResourceW.restype = ctypes.c_void_p SizeofResource = ctypes.windll.kernel32.SizeofResource SizeofResource.argtypes = [ctypes.c_void_p, ctypes.c_void_p] SizeofResource.restype = ctypes.c_size_t hRes = win32api.LoadResource(hlib, win32con.RT_GROUP_ICON, group_name) mem_icon_dir = ctypes.windll.kernel32.LockResource(hRes) # 32 bits color; 16 and 256 colors are too old icon_size = 256 icon_name = ctypes.windll.user32.LookupIconIdFromDirectoryEx( mem_icon_dir, True, icon_size, icon_size, 0x00000000) h_res_info = FindResourceW(hlib, icon_name, win32con.RT_ICON) size = ctypes.windll.kernel32.SizeofResource(hlib, h_res_info) rec = win32api.LoadResource(hlib, win32con.RT_ICON, icon_name) mem_icon = ctypes.windll.kernel32.LockResource(rec) # And this is some differ (copy data to Python buffer) binary_data = (ctypes.c_ubyte * size)() ctypes.memmove(binary_data, mem_icon, size) h_icon_ret = ctypes.windll.user32.CreateIconFromResourceEx( binary_data, size, True, 0x00030000, 0, 0, 0x00000000) info = win32gui.GetIconInfo(h_icon_ret) bminfo = win32gui.GetObject(info[4]) # generate bitmap by drawing the icon hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0)) hbmp = win32ui.CreateBitmap() hbmp.CreateCompatibleBitmap(hdc, bminfo.bmWidth, bminfo.bmHeight) hcdc = hdc.CreateCompatibleDC() hcdc.SelectObject(hbmp) win32gui.DrawIconEx(hcdc.GetHandleOutput(), 0, 0, h_icon_ret, bminfo.bmWidth, bminfo.bmHeight, 0, 0, 0x0003) # MIME type is "image/bmp" # The group name might be a number: 110 etc... or a string such as 'ICO_MYCOMPUTER'. # This is the prefix of the temporary BMP file name containing the extracted icon. img_fil_nam_prefix = "icon-%03dx%03d-%s-%03d" % ( bminfo.bmWidth, bminfo.bmHeight, str(group_name), icon_name) # The destructor will remove the file. obj_temp_file = lib_util.TmpFile(img_fil_nam_prefix, "bmp") img_fil_nam = obj_temp_file.Name hbmp.SaveBitmapFile(hcdc, img_fil_nam) win32gui.DestroyIcon(h_icon_ret) return obj_temp_file
def screen(self): """PIL Image of current window screen. (the window must be on the top) reference: https://msdn.microsoft.com/en-us/library/dd183402(v=vs.85).aspx""" # opengl windows cannot get from it's hwnd, so we use the screen hwnd = win32gui.GetDesktopWindow() # get window size and offset left, top, right, bottom = self.rect width, height = right - left, bottom - top # the device context of the window hdcwin = win32gui.GetWindowDC(hwnd) # make a temporary dc hdcmem = win32gui.CreateCompatibleDC(hdcwin) # make a temporary bitmap in memory, this is a PyHANDLE object hbmp = win32gui.CreateCompatibleBitmap(hdcwin, width, height) # select bitmap for temporary dc win32gui.SelectObject(hdcmem, hbmp) # copy bits to temporary dc win32gui.BitBlt(hdcmem, 0, 0, width, height, hdcwin, left, top, win32con.SRCCOPY) # check the bitmap object infomation bmp = win32gui.GetObject(hbmp) bi = BITMAPINFOHEADER() bi.biSize = ctypes.sizeof(BITMAPINFOHEADER) bi.biWidth = bmp.bmWidth bi.biHeight = bmp.bmHeight bi.biPlanes = bmp.bmPlanes bi.biBitCount = bmp.bmBitsPixel bi.biCompression = 0 # BI_RGB bi.biSizeImage = 0 bi.biXPelsPerMeter = 0 bi.biYPelsPerMeter = 0 bi.biClrUsed = 0 bi.biClrImportant = 0 # calculate total size for bits pixel = bmp.bmBitsPixel size = ((bmp.bmWidth * pixel + pixel - 1) / pixel) * 4 * bmp.bmHeight buf = (ctypes.c_char * size)() # read bits into buffer windll.gdi32.GetDIBits(hdcmem, hbmp.handle, 0, bmp.bmHeight, buf, ctypes.byref(bi), win32con.DIB_RGB_COLORS) # make a PIL Image img = Image.frombuffer('RGB', (bmp.bmWidth, bmp.bmHeight), buf, 'raw', 'BGRX', 0, 1) img = img.transpose(Image.FLIP_TOP_BOTTOM) # cleanup win32gui.DeleteObject(hbmp) win32gui.DeleteObject(hdcmem) win32gui.ReleaseDC(hwnd, hdcwin) return img
def OnPaint(self, hWnd, msg, wparam, lparam): dc, ps = win32gui.BeginPaint(hWnd) wndrect = win32gui.GetClientRect(hWnd) wndwidth = wndrect[2] - wndrect[0] wndheight = wndrect[3] - wndrect[1] if os.path.isfile(FILENAME): hBitmap = win32gui.LoadImage(0, FILENAME, win32con.IMAGE_BITMAP, 0, 0, win32con.LR_LOADFROMFILE) hdcBitmap = win32gui.CreateCompatibleDC(dc) hOldBitmap = win32gui.SelectObject(hdcBitmap, hBitmap) bminfo = win32gui.GetObject(hBitmap) win32gui.SetStretchBltMode(dc, win32con.COLORONCOLOR) win32gui.StretchBlt(dc, 0, 0, wndwidth, wndheight, hdcBitmap, 0, 0, bminfo.bmWidth, bminfo.bmHeight, win32con.SRCCOPY) win32gui.SelectObject(hdcBitmap, hOldBitmap) win32gui.EndPaint(hWnd, ps) return 0
def __init_screen_handles(self): # opengl windows cannot get from it's hwnd, so we use the screen hwnd = win32gui.GetDesktopWindow() # get screen size and offset left, top, right, bottom = self.rect width, height = right-left, bottom-top # the device context of the window hdcwin = win32gui.GetWindowDC(hwnd) # make a temporary dc hdcmem = win32gui.CreateCompatibleDC(hdcwin) # make a temporary bitmap in memory, this is a PyHANDLE object hbmp = win32gui.CreateCompatibleBitmap(hdcwin, width, height) # select bitmap for temporary dc win32gui.SelectObject(hdcmem, hbmp) # check the bitmap object infomation bmp = win32gui.GetObject(hbmp) bi = BITMAPINFOHEADER() bi.biSize = ctypes.sizeof(BITMAPINFOHEADER) bi.biWidth = bmp.bmWidth bi.biHeight = bmp.bmHeight bi.biPlanes = bmp.bmPlanes bi.biBitCount = bmp.bmBitsPixel bi.biCompression = 0 # BI_RGB bi.biSizeImage = 0 bi.biXPelsPerMeter = 0 bi.biYPelsPerMeter = 0 bi.biClrUsed = 0 bi.biClrImportant = 0 # calculate total size for bits pixel = bmp.bmBitsPixel size = ((bmp.bmWidth * pixel + pixel - 1)/pixel) * 4 * bmp.bmHeight buf = (ctypes.c_char * size)() self._hdcwin = hdcwin self._hdcmem = hdcmem self._bi = bi self._hbmp = hbmp self._buf = buf
def _win_stock_font(id): h = gui.GetStockObject(id) lf = gui.GetObject(h) return Font._from_win_logfont(lf)