def AutoSizeDialog(dialog, center=True): classNameBuffer = ctypes.c_buffer(1024) children = [] editBoxes = [] for child in EnumChildWindows(dialog): children.append(child) user32.GetClassNameA(child, classNameBuffer, 1024) if classNameBuffer.value.lower() == 'edit': editBoxes.append(child) editBoxes.sort( lambda x, y: cmp(GetWindowRect(x).top, GetWindowRect(y).top)) for editBox in editBoxes: text = GetText(editBox) user32.SetWindowTextA(editBox, 'X\r\nX') if user32.SendMessageA(editBox, EM_GETLINECOUNT, 0, 0) < 2: user32.SetWindowTextA(editBox, text) else: lineHeight = HIWORD( user32.SendMessageA(editBox, EM_POSFROMCHAR, 3, 0)) - HIWORD( user32.SendMessageA(editBox, EM_POSFROMCHAR, 0, 0)) user32.SetWindowTextA(editBox, text) newHeight = user32.SendMessageA( editBox, EM_GETLINECOUNT, 0, 0) * lineHeight + 2 * LOWORD( user32.SendMessageA(editBox, EM_GETMARGINS, 0, 0)) editBoxRect = GetWindowRect(editBox) oldHeight = height(editBoxRect) if newHeight != oldHeight: topLeft = wintypes.POINT(editBoxRect.left, editBoxRect.top) user32.ScreenToClient(dialog, ctypes.byref(topLeft)) user32.MoveWindow(editBox, topLeft.x, topLeft.y, width(editBoxRect), newHeight, False) for child in children: childRect = GetWindowRect(child) if childRect.top >= editBoxRect.bottom: topLeft = wintypes.POINT(childRect.left, childRect.top) user32.ScreenToClient(dialog, ctypes.byref(topLeft)) user32.MoveWindow(child, topLeft.x, topLeft.y + newHeight - oldHeight, width(childRect), height(childRect), False) dRect = GetWindowRect(dialog) dRect.bottom += newHeight - oldHeight topLeft = wintypes.POINT(dRect.left, dRect.top) user32.MoveWindow(dialog, topLeft.x, topLeft.y, width(dRect), height(dRect), False) user32.InvalidateRect(dialog, None, True) if center: CenterWindow(dialog)
def _on_notify(self, wparam, lparam): """Handles ``WM_NOTIFY``. If this is a left button click, this icon will be activated. If a menu is registered and this is a right button click, the popup menu will be displayed. """ if lparam == win32.WM_LBUTTONDOWN: self() elif self._menu_handle and lparam == win32.WM_RBUTTONDOWN: # TrackPopupMenuEx does not behave unless our systray window is the # foreground window win32.SetForegroundWindow(self._hwnd) # Get the cursor position to determine where to display the menu point = wintypes.POINT() win32.GetCursorPos(ctypes.byref(point)) # Display the menu and get the menu item identifier; the identifier # is the menu item index hmenu, descriptors = self._menu_handle index = win32.TrackPopupMenuEx( hmenu, win32.TPM_RIGHTALIGN | win32.TPM_BOTTOMALIGN | win32.TPM_RETURNCMD, point.x, point.y, self._menu_hwnd, None) if index > 0: descriptors[index - 1](self)
def get_hmonitor_by_point(x, y): point = wintypes.POINT() point.x = x point.y = y return ctypes.windll.user32.MonitorFromPoint(point, 0)
def show(self, hWnd, x=None, y=None): ''' Show the menu. This will block until an item is selected or the menu looses focus and closes. - param hWnd: the window which will be the parent of this menu param x/y: location for showing the menu. If None, current mouse position will be used ''' assert type(hWnd) == int, 'hWnd must be an int, not ' + str( type(hWnd).__name__) if None in (x, y): p = wintypes.POINT() ret, err = self.__GetCursorPos(byref(p)), self.__GetLastError() if ret == 0: raise ctypes.WinError(err) x, y = p.x, p.y self.__SetForegroundWindow( hWnd ) # No error check. This failes sometimes, but it's not really a failure # and it's normal # ret will be the selected item, or 0 if nothing was selected. # If there's an error, 0 is returned, so there's no way of knowing if there was an error # or if the menu was just dismissed, so no error checking is done ret = self.__TrackPopupMenu( self.__menu, self.TPM_BOTTOMALIGN | self.TPM_LEFTALIGN | self.TPM_NONOTIFY | self.TPM_RETURNCMD, x, y, 0, hWnd, 0) if ret > 0: self.__callbacks[ret]()
def GetCursorPos(): """ GetCursorPos from Win32. Get current mouse cursor positon. Return Tuple[int, int], two ints tuple (x, y). """ point = wintypes.POINT(0, 0) windll.user32.GetCursorPos(byref(point)) return point.x, point.y
def get_clientpos(self, x, y): """ Translate screen coordinates to active window coordinates int x: The x coordinate. int y: The y coordinate. Returns tuple (x, y) of window coordinates """ point = wintypes.POINT(x, y) hwind = self.user32.GetActiveWindow() windll.user32.ScreenToClient(hwind, ctypes.byref(point)) return ctypes.c_long(point.x).value, ctypes.c_long(point.y).value
def move_cb(self, *args): pos = wintypes.POINT() GetCursorPos(byref(pos)) x, y = pos.x, pos.y w = GetSystemMetrics(win32con.SM_CXSMICON) h = GetSystemMetrics(win32con.SM_CYSMICON) x += self.offset_x y += self.offset_y log("move_cb%s x=%s, y=%s, size=%i, %i", args, x, y, w, h) self.recalculate_geometry(x, y, w, h) if self.mouseover_cb: self.mouseover_cb(x, y)
def getWindowFromCursorPos(): ''' Returns the handle of the top window where the cursor is currently located ''' # Get current Cursor position point = wintypes.POINT() windll.user32.GetCursorPos(byref(point)) print('Found Cursor location (x={0},y={1})'.format(point.x, point.y)) # Get window handle from Cursor position hwnd = windll.user32.WindowFromPoint(point) print('Found window {0} ({1})'.format(hwnd, getWindowText(hwnd))) # The hwnd returned could be that of a button or any other sub-element # So get the top window of that element # (This should work with windows that aren't visually located under # their parent) topHwnd = windll.user32.GetAncestor(hwnd, GA_ROOT) print('Found top window {0} ({1})'.format(topHwnd, getWindowText(topHwnd))) return topHwnd
def _position_get(self): point = wintypes.POINT() if self.__GetCursorPos(ctypes.byref(point)): return (point.x, point.y) else: return None
from ctypes import * from ctypes import wintypes from time import sleep #呼叫Windows系統動態連結程式庫user32.dll user32 = windll.user32 p = wintypes.POINT() buffer = create_string_buffer(255) while True: sleep(0.5) #取得滑鼠位置 user32.GetCursorPos(byref(p)) #取得滑鼠所在位置的視窗控制碼 HWnd = user32.WindowFromPoint(p) #註解的程式碼本來可以實現星號密碼查看,在Win7以後的系統失效了 #dwStyle = user32.GetWindowLongA(HWnd, -16) #-16是GWL_STYLE訊息的值 #user32.SetWindowWord(HWnd, -16, 0) sleep(0.2) #取得視窗文字 user32.SendMessageA(HWnd, 13, 255, byref(buffer)) #13是WM_GETTEXT訊息的值 #user32.SetWindowLongA(HWnd, -16, dwStyle) print(buffer.value.decode('big5'))
def _position_get(): point = wintypes.POINT() if windll.user32.GetCursorPos(ctypes.byref(point)): return point.x, point.y else: return None
import math import ctypes from ctypes import wintypes, windll path = sys.argv[1] wndName = "ESC close, x next, z previous, c clean, w up, s down, a left, d right" warpwndname = 'warped plate' imgs = os.listdir(path) total = len(imgs) i = 0 box = [] pts = [] labels = [] n = 0 breakfile = 'lbreakpoint.txt' cursor = wintypes.POINT() lastx = 0 lasty = 0 alpha = 0 beta = 0 print("%d images loaded" % total) if os.path.exists(breakfile): with open(breakfile, "r") as p: i = int(p.read()) print('load breakpointL %d' % i)
def get_cursor_position(): point = wintypes.POINT() GETCURSORPOS(byref(point)) return (point.x, point.y)