Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get HWND of each Window?

I am developing a python application and I want to get the HWND of each open windows. I need the name of the windows and the HWND to filter the list to manage some specifics windows, moving and resizing them.

I have tried to do it myself looking information around but I did not get the correct piece of code. I tried with this code but I only get the title of each windows (that is great), but I need the HWND too.

import ctypes
import win32gui
EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible

titles = []
def foreach_window(hwnd, lParam):
    if IsWindowVisible(hwnd):
        length = GetWindowTextLength(hwnd)
        buff = ctypes.create_unicode_buffer(length + 1)
        GetWindowText(hwnd, buff, length + 1)
        titles.append((hwnd, buff.value))
    return True
EnumWindows(EnumWindowsProc(foreach_window), 0)

for i in range(len(titles)):
    print(titles)[i]

win32gui.MoveWindow((titles)[5][0], 0, 0, 760, 500, True)

There is a error here:

win32gui.MoveWindow((titles)[5][0], 0, 0, 760, 500, True)
 TypeError: The object is not a PyHANDLE object
like image 761
user1618465 Avatar asked Feb 01 '13 18:02

user1618465


People also ask

How do you get window handles?

However, you can obtain the window handle by calling FindWindow() . This function retrieves a window handle based on a class name or window name. Call GetConsoleTitle() to determine the current console title. Then supply the current console title to FindWindow() .

What is a window handle HWND?

i searched the definiton of window handle on the internet (hard to find actully) A window handle (usually shortened to hWnd) is a unique identifer that Windows assigns to each window created. By window in this case we are referring to everything from command buttons and textboxes, to dialog boxes and full windows.

What can you do with a Hwnd?

about HWND (Windows)Use the HWND node to get the handle of a window with a given title (shown in the title area of the window). HWND will retrive all windows with a given title. this might be more powerful as you like - for example there are manymany__ windows with just an empty title.

What type is HWND?

HWND data types are "Handles to a Window", and are used to keep track of the various objects that appear on the screen. To communicate with a particular window, you need to have a copy of the window's handle. HWND variables are usually prefixed with the letters "hwnd", just so the programmer knows they are important.


2 Answers

You mixed up ctypes and win32gui.
The hwnd you've got is obtained via ctypes and is a LP_c_long object. That's why win32gui.MoveWindow didn't accept it. You should pass it to

ctypes.windll.user32.MoveWindow(titles[5][0], 0, 0, 760, 500, True)

If you want to use win32gui.MoveWindow, you can use python function as callback directly.
For example,

import win32gui

def enumHandler(hwnd, lParam):
    if win32gui.IsWindowVisible(hwnd):
        if 'Stack Overflow' in win32gui.GetWindowText(hwnd):
            win32gui.MoveWindow(hwnd, 0, 0, 760, 500, True)

win32gui.EnumWindows(enumHandler, None)
like image 177
nymk Avatar answered Oct 06 '22 05:10

nymk


Your problem (now that martineau has fixed your original problem of not storing the HWND values at all) is that you're trying to mix ctypes and win32gui.

You can do that if you know what you're doing—but if not, just don't do it.

If you want to get window handles you can use with win32gui, use win32gui.EnumWindows instead of calling the raw function out of the user32 DLL.

like image 41
abarnert Avatar answered Oct 06 '22 05:10

abarnert