Skip to content

Instantly share code, notes, and snippets.

@netalkGB
Created February 22, 2023 17:36
Show Gist options
  • Select an option

  • Save netalkGB/e00de56dbbfd19aa59e193faad928ca8 to your computer and use it in GitHub Desktop.

Select an option

Save netalkGB/e00de56dbbfd19aa59e193faad928ca8 to your computer and use it in GitHub Desktop.
Windowsの開いてるウインドウたちのタイトルを取得する
import ctypes
user32 = ctypes.windll.user32
def get_titles():
titles = []
def foreach_window(hwnd, lparam):
length = user32.GetWindowTextLengthW(hwnd)
buf = ctypes.create_unicode_buffer(length + 1)
user32.GetWindowTextW(hwnd, buf, length + 1)
titles.append(buf.value)
user32.EnumWindows(ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))(foreach_window), 0)
return titles
print([title for title in get_titles() if title.find("TMIDI Player") >= 0][0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment