def BrowseCallback(hwnd, uMsg, lParam, lpData): if uMsg == BFFM_INITIALIZED: if actionButtonLabel: label = unicode(actionButtonLabel, errors='replace') user32.SendMessageW(hwnd, BFFM_SETOKTEXT, 0, label) if cancelButtonLabel: label = unicode(cancelButtonLabel, errors='replace') cancelButton = user32.GetDlgItem(hwnd, IDCANCEL) if cancelButton: user32.SetWindowTextW(cancelButton, label) if windowTitle: title = unicode(windowTitle, erros='replace') user32.SetWindowTextW(hwnd, title) if defaultLocation: user32.SendMessageW(hwnd, BFFM_SETSELECTIONW, 1, defaultLocation.replace('/', '\\')) if location: x, y = location desktopRect = wintypes.RECT() user32.GetWindowRect(0, ctypes.byref(desktopRect)) user32.SetWindowPos(hwnd, 0, desktopRect.left + x, desktopRect.top + y, 0, 0, SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER) else: CenterWindow(hwnd) return 0
def fillRect(self, rect, brush): bounds = wintypes.RECT() bounds.left = rect[0] bounds.top = rect[1] bounds.right = rect[2] bounds.bottom = rect[3] windll.user32.FillRect(self.hDC, ctypes.byref(bounds), brush.handle)
def __init__(self, pid, name, hwnd): __rect = wintypes.RECT() self.pid = pid self.hwnd = hwnd self.name = name self.__user32 = windll.user32 self.__user32.GetWindowRect(self.hwnd, byref(__rect)) self.rect = Rect(__rect.left, __rect.top, __rect.right, __rect.bottom)
def update(self): rect = wintypes.RECT() user32.GetWindowRect(self.window_handle, ctypes.byref(rect)) self.left = rect.left self.right = rect.right self.width = abs(rect.right - rect.left) self.top = rect.top self.bottom = rect.bottom self.height = abs(rect.bottom - rect.top)
def __init__(self, window_name): self.hwin = win32gui.GetDesktopWindow() hwnd = windll.user32.FindWindowW(None, window_name) print("hwnd: ", hwnd) r = wintypes.RECT() windll.user32.GetWindowRect(hwnd, byref(r)) self.width = r.right - r.left self.height = r.bottom - r.top self.left, self.top = r.left, r.top
def get_poe_window_location(self): hwnd = windll.user32.FindWindowW(0, 'Path of Exile') rect = wintypes.RECT() windll.user32.GetWindowRect(hwnd, pointer(rect)) height_offset = 39 # Offset to get correct height width_offset = 16 # -||- width height = rect.bottom - rect.top - height_offset width = rect.right - rect.left - width_offset # 7 and 31 are magical offset numbers related to Windows-border size return (width, height, rect.left + 7, rect.top + 31)
def hookProc(hdlg, uiMsg, wParam, lParam): if uiMsg == WM_INITDIALOG: dialog = user32.GetParent(hdlg) if message: handle = user32.FindWindowExA(hdlg, 0, 0, 0) user32.SetWindowTextA(handle, message) if actionButtonLabel: user32.SendMessageA(dialog, CDM_SETCONTROLTEXT, IDOK, actionButtonLabel) if cancelButtonLabel: user32.SendMessageA(dialog, CDM_SETCONTROLTEXT, IDCANCEL, cancelButtonLabel) elif uiMsg == WM_NOTIFY: if not hookProcInitDone: hookProcInitDone.append(True) dialog = user32.GetParent(hdlg) desktop = user32.GetDesktopWindow() if message: clientRect = wintypes.RECT() user32.GetClientRect(dialog, ctypes.byref(clientRect)) dlgRect = wintypes.RECT() user32.GetWindowRect(hdlg, ctypes.byref(dlgRect)) user32.SetWindowPos(hdlg, 0, 0, 0, width(clientRect), height(dlgRect), SWP_NOMOVE | SWP_NOZORDER) handle = user32.GetDlgItem(hdlg, IDC_STATIC) user32.SetWindowPos(handle, 0, 0, 0, width(clientRect), height(dlgRect), SWP_NOMOVE | SWP_NOZORDER) if location: x, y = location desktopRect = wintypes.RECT() user32.GetWindowRect(desktop, ctypes.byref(desktopRect)) user32.SetWindowPos(dialog, 0, desktopRect.left + x, desktopRect.top + y, 0, 0, SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER) else: # center CenterWindow(dialog) return 0
def get_window_rect(self, idx): """ 핸들에 대응하는 윈도우의 좌표정보를 반환한다 param: <int> hwnd: 윈도우 핸들 return: <tuple> x1, y1, x2, y2 """ f = windll.dwmapi.DwmGetWindowAttribute rect = wintypes.RECT() DWMWA_EXTENDED_FRAME_BOUNDS = 9 f(wintypes.HWND(self.bslist[idx][0]), wintypes.DWORD(DWMWA_EXTENDED_FRAME_BOUNDS), byref(rect), sizeof(rect)) return rect.left, rect.top, rect.right, rect.bottom
def main(): process_exec = "ac_client.exe" # process handle setup rwm = pymem.ReadWriteMemory() process = rwm.get_process_by_name(process_exec) if process == None: print("Could not find: ", process_exec) return process.open() print("PID: ", process.pid, ", name: ", process.name) esp = overlay(process.handle) hwnds = esp.get_hwnds_for_pid(process.pid) print(hwnds) window_rect = wintypes.RECT() window_found = esp.get_window_rect(process.handle, window_rect) if window_found == 0: error_code = ctypes.GetLastError() print("Could not find window: ", error_code) # error_code 1400 = ERROR_INVALID_WINDOW_HANDLE print(window_rect)
def GetWindowRect(hwnd): rect = wintypes.RECT() user32.GetWindowRect(hwnd, ctypes.byref(rect)) return rect
def get_taskbar_size(): SystemParametersInfo = windll.user32.SystemParametersInfoA work_area = wintypes.RECT() if (SystemParametersInfo(SPI_GETWORKAREA, 0, byref(work_area), 0)): GetSystemMetrics = windll.user32.GetSystemMetrics return GetSystemMetrics(SM_CYSCREEN) - work_area.bottom
def thread_window_callback(hwnd, lparam): length = windll.user32.GetWindowTextLengthW(hwnd) if length > 0: buff = create_unicode_buffer(length + 1) windll.user32.GetWindowTextW(hwnd, buff, length + 1) # print("DEBUG - Inspecting {0} = {1}".format(hwnd, buff.value)) if buff.value == "Start": # Found the window # print("DEBUG sending Click message") m_res = windll.user32.SendMessageW(hwnd, BM_CLICK, 0, 0) if m_res != 0: # Don't look anymore triggered_start[0] = True return False else: try: print("<<ERROR pressing start button>>") raise WinError() except OSError: import traceback traceback.print_exc() if buff.value == "Start menu": triggered_start[0] = True if windll.user32.IsWindowVisible(hwnd): p = WINDOWPLACEMENT() p.length = c_sizeof(WINDOWPLACEMENT) val = windll.user32.GetWindowPlacement(hwnd, byref(p)) if val is not None: show_cmd = p.showCmd if (show_cmd == SW_SHOWNORMAL or show_cmd == SW_MAXIMIZE or show_cmd == SW_SHOWMAXIMIZED or show_cmd == SW_SHOWNOACTIVATE or show_cmd == SW_SHOW or show_cmd == SW_SHOWNA or show_cmd == SW_RESTORE): # Hide the window # print("DEBUG hiding the start menu {0}".format(show_cmd)) windll.user32.ShowWindow(hwnd, SW_HIDE) return False # print("DEBUG showing the start menu ({0})".format(placement.showCmd)) windll.user32.ShowWindow(hwnd, SW_SHOW) # If the task bar is hidden, then part of the start window is not shown fully. # SW_MAXIMIZE will show it fully, but the rendering becomes messed up. taskbar_rect = wintypes.RECT() windll.user32.GetClientRect(taskbar_hwnd, byref(taskbar_rect)) client_rect = wintypes.RECT() windll.user32.GetClientRect(hwnd, byref(client_rect)) overlap_rect = wintypes.RECT() if windll.user32.IntersectRect(byref(overlap_rect), byref(taskbar_rect), byref(client_rect)) != 0: # The taskbar and the start window rectangles overlap each other. # Adjust the start window to be outside of this. # Remember: the taskbar can be anywhere on the edge of the screen. new_x = client_rect.left new_y = client_rect.top if overlap_rect.left == client_rect.left and overlap_rect.right == client_rect.right: # Adjust along the y axis if overlap_rect.top <= client_rect.top <= overlap_rect.bottom: # Adjust down new_y = overlap_rect.bottom else: # Adjust up new_y = client_rect.top - (overlap_rect.bottom - overlap_rect.top) else: # Adjust along the x axis if overlap_rect.left <= client_rect.left <= overlap_rect.right: # Adjust to the right new_x = overlap_rect.right else: new_x = client_rect.left - (overlap_rect.right - overlap_rect.left) windll.user32.SetWindowPos( hwnd, None, new_x, new_y, 0, 0, SWP_NOSIZE) # Give the window the focus. This is the Microsoft Magic Focus Dance. current_hwnd = windll.user32.GetForegroundWindow() current_thread_id = windll.kernel32.GetCurrentThreadId() thread_process_id = windll.user32.GetWindowThreadProcessId(current_hwnd, None) windll.user32.AttachThreadInput(thread_process_id, current_thread_id, True) windll.user32.SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE) windll.user32.SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE) windll.user32.SetForegroundWindow(hwnd) windll.user32.AttachThreadInput(thread_process_id, current_thread_id, False) windll.user32.SetFocus(hwnd) windll.user32.SetActiveWindow(hwnd) return False # else: # print("DEBUG - Inspecting {0} => no title, ignoring".format(hwnd)) return True
def shell__open_start_menu(show_taskbar): # Find the task bar window taskbar_hwnd = windll.user32.FindWindowW("Shell_TrayWnd", None) if taskbar_hwnd is None or 0 == taskbar_hwnd: raise WinError() taskbar_size = wintypes.RECT() windll.user32.GetWindowRect(taskbar_hwnd, byref(taskbar_size)) # if show_taskbar: # if windll.user32.IsWindowVisible(taskbar_hwnd): # placement = WINDOWPLACEMENT() # placement.length = c_sizeof(WINDOWPLACEMENT) # val = windll.user32.GetWindowPlacement(taskbar_hwnd, byref(placement)) # if val is not None: # show_cmd = placement.showCmd # if (show_cmd == SW_SHOWNORMAL or show_cmd == SW_MAXIMIZE or show_cmd == SW_SHOWMAXIMIZED # or show_cmd == SW_SHOWNOACTIVATE or show_cmd == SW_SHOW or show_cmd == SW_SHOWNA # or show_cmd == SW_RESTORE): # show_taskbar = False if show_taskbar: # The task bar is auto-hide. It's always present, but not on screen. # The trick Windows uses is pushing it off-screen, so it's just barely # visible. windll.user32.GetWindowRect(taskbar_hwnd, byref(taskbar_size)) # Figure out which corner it's in. It's either top, left, right, or bottom. # We do this by finding a "0", which indicates where on the screen it's # located. However, with strange, multi-monitor setups, this isn't always # correct. But it's a good guess. # windll.user32.SetWindowPos(taskbar_hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_SHOWWINDOW) # TODO show the task bar. # print("<<Don't know how to show the task bar>>") # Find the process ID of the taskbar window. taskbar_pid = wintypes.DWORD() windll.user32.GetWindowThreadProcessId(taskbar_hwnd, byref(taskbar_pid)) # Find a thread in that process that has a "Start" button triggered_start = [False] # noinspection PyUnusedLocal def thread_window_callback(hwnd, lparam): length = windll.user32.GetWindowTextLengthW(hwnd) if length > 0: buff = create_unicode_buffer(length + 1) windll.user32.GetWindowTextW(hwnd, buff, length + 1) # print("DEBUG - Inspecting {0} = {1}".format(hwnd, buff.value)) if buff.value == "Start": # Found the window # print("DEBUG sending Click message") m_res = windll.user32.SendMessageW(hwnd, BM_CLICK, 0, 0) if m_res != 0: # Don't look anymore triggered_start[0] = True return False else: try: print("<<ERROR pressing start button>>") raise WinError() except OSError: import traceback traceback.print_exc() if buff.value == "Start menu": triggered_start[0] = True if windll.user32.IsWindowVisible(hwnd): p = WINDOWPLACEMENT() p.length = c_sizeof(WINDOWPLACEMENT) val = windll.user32.GetWindowPlacement(hwnd, byref(p)) if val is not None: show_cmd = p.showCmd if (show_cmd == SW_SHOWNORMAL or show_cmd == SW_MAXIMIZE or show_cmd == SW_SHOWMAXIMIZED or show_cmd == SW_SHOWNOACTIVATE or show_cmd == SW_SHOW or show_cmd == SW_SHOWNA or show_cmd == SW_RESTORE): # Hide the window # print("DEBUG hiding the start menu {0}".format(show_cmd)) windll.user32.ShowWindow(hwnd, SW_HIDE) return False # print("DEBUG showing the start menu ({0})".format(placement.showCmd)) windll.user32.ShowWindow(hwnd, SW_SHOW) # If the task bar is hidden, then part of the start window is not shown fully. # SW_MAXIMIZE will show it fully, but the rendering becomes messed up. taskbar_rect = wintypes.RECT() windll.user32.GetClientRect(taskbar_hwnd, byref(taskbar_rect)) client_rect = wintypes.RECT() windll.user32.GetClientRect(hwnd, byref(client_rect)) overlap_rect = wintypes.RECT() if windll.user32.IntersectRect(byref(overlap_rect), byref(taskbar_rect), byref(client_rect)) != 0: # The taskbar and the start window rectangles overlap each other. # Adjust the start window to be outside of this. # Remember: the taskbar can be anywhere on the edge of the screen. new_x = client_rect.left new_y = client_rect.top if overlap_rect.left == client_rect.left and overlap_rect.right == client_rect.right: # Adjust along the y axis if overlap_rect.top <= client_rect.top <= overlap_rect.bottom: # Adjust down new_y = overlap_rect.bottom else: # Adjust up new_y = client_rect.top - (overlap_rect.bottom - overlap_rect.top) else: # Adjust along the x axis if overlap_rect.left <= client_rect.left <= overlap_rect.right: # Adjust to the right new_x = overlap_rect.right else: new_x = client_rect.left - (overlap_rect.right - overlap_rect.left) windll.user32.SetWindowPos( hwnd, None, new_x, new_y, 0, 0, SWP_NOSIZE) # Give the window the focus. This is the Microsoft Magic Focus Dance. current_hwnd = windll.user32.GetForegroundWindow() current_thread_id = windll.kernel32.GetCurrentThreadId() thread_process_id = windll.user32.GetWindowThreadProcessId(current_hwnd, None) windll.user32.AttachThreadInput(thread_process_id, current_thread_id, True) windll.user32.SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE) windll.user32.SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE) windll.user32.SetForegroundWindow(hwnd) windll.user32.AttachThreadInput(thread_process_id, current_thread_id, False) windll.user32.SetFocus(hwnd) windll.user32.SetActiveWindow(hwnd) return False # else: # print("DEBUG - Inspecting {0} => no title, ignoring".format(hwnd)) return True # Find the threads for the process ID thread_ids = [] hsnapshot = windll.kernel32.CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, taskbar_pid) if hsnapshot == INVALID_HANDLE_VALUE: raise WinError() try: thread_entry = THREADENTRY32() thread_entry.dwSize = c_sizeof(THREADENTRY32) # print("DEBUG Getting threads for pid {0}".format(taskbar_pid)) res = windll.kernel32.Thread32First(hsnapshot, byref(thread_entry)) # print("DEBUG :: first: {0}".format(res)) if res == 0: raise WinError() while res != 0: if thread_entry.dwSize > THREADENTRY32.th32OwnerProcessID.offset: thread_ids.append(thread_entry.th32ThreadID) # else: # print("DEBUG - returned too small size {0}".format(thread_entry.dwSize)) thread_entry.dwSize = c_sizeof(THREADENTRY32) res = windll.kernel32.Thread32Next(hsnapshot, byref(thread_entry)) # print("DEBUG :: next: {0}".format(res)) finally: windll.kernel32.CloseHandle(hsnapshot) callback_type = CFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM) callback_ptr = callback_type(thread_window_callback) for thread_id in thread_ids: # print("DEBUG Iterating over windows for thread {0} in pid {1}".format(thread_id, taskbar_pid)) windll.user32.EnumThreadWindows(thread_id, callback_ptr, 0) if triggered_start[0]: return raise OSError("Could not find start button")
def get_window_rect(window): rect = wintypes.RECT() result = GETWINDOWRECT(window, byref(rect)) return result, (rect.left, rect.top, rect.right, rect.bottom)
def DwmGetWindowAttribute(hwnd: wintypes.HWND) -> wintypes.RECT: rect = wintypes.RECT() dwmapi.DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, ctypes.byref(rect), ctypes.sizeof(rect)) return rect
def getClientRect(self): clientRect = wintypes.RECT() windll.user32.GetClientRect(self.hWindow, ctypes.byref(clientRect)) return (clientRect.left, clientRect.top, clientRect.right, clientRect.bottom)
def getWindowRect(self): windowRect = wintypes.RECT() windll.user32.GetWindowRect(self.hWindow, ctypes.byref(windowRect)) return (windowRect.left, windowRect.top, windowRect.right, windowRect.bottom)