Skip to content

Instantly share code, notes, and snippets.

@IanByun
Last active July 27, 2022 07:38
Show Gist options
  • Save IanByun/52aed8505e18d71fa5677ca0d6bbcb17 to your computer and use it in GitHub Desktop.
Save IanByun/52aed8505e18d71fa5677ca0d6bbcb17 to your computer and use it in GitHub Desktop.
# 2021-07-09 JH Byun
# Thanks to https://github.com/SD810/KaTalkEspresso/blob/master/KaTalkEspresso/AdCloser.cs
import os
os.system("start /wait cmd /c pip install pypiwin32")
import win32api
import win32process
import win32con
import win32gui
pids = win32process.EnumProcesses()
for pid in pids:
try:
proc_handle = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION, False, pid)
proc_name = win32process.GetModuleFileNameEx(proc_handle, 0)
if ("KakaoTalk.exe" in proc_name):
def callback(hwnd, hwnds):
if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
_, found_pid = win32process.GetWindowThreadProcessId(hwnd)
if found_pid == pid:
hwnds.append(hwnd)
return True
win_hwnds = []
win32gui.EnumWindows(callback, win_hwnds)
for win_hwnd in win_hwnds:
class_name = win32gui.GetClassName(win_hwnd)
window_text = win32gui.GetWindowText(win_hwnd)
window_rect = win32gui.GetWindowRect(win_hwnd)
print("win_hwnd:", win_hwnd)
print("class_name:", class_name)
print("window_text:", window_text)
print("window_rect:", window_rect)
if (window_text in ["KakaoTalk", "카카오톡", "カカオトーク"]): # only for KakaoTalk main window
print("↑Found KakaoTalk main window")
print("")
def handle_window(win_hwnd, win_hwnds):
win_hwnds.append(win_hwnd)
return True
child_hwnds = []
win32gui.EnumChildWindows(win_hwnd, handle_window, child_hwnds)
katalk_ad_win = 0
bottom_most_y = 0
for child_hwnd in child_hwnds: # bottom most window is Ad window
class_name = win32gui.GetClassName(child_hwnd)
window_text = win32gui.GetWindowText(child_hwnd)
rect = win32gui.GetWindowRect(child_hwnd)
x = rect[0]
y = rect[1]
w = rect[2] - x
h = rect[3] - y
#print(" win_hwnd:", child_hwnd)
#print(" class_name:", class_name)
#print(" window_text:", window_text)
#print(" x, y, w, h:", x, y, w, h)
#print("")
if ("BannerAdWnd" in class_name and y > bottom_most_y):
bottom_most_y = y
katalk_ad_win = child_hwnd
if (katalk_ad_win is not 0):
print(" win_hwnd:", katalk_ad_win)
print(" class_name:", win32gui.GetClassName(katalk_ad_win))
print(" window_text:", win32gui.GetWindowText(katalk_ad_win))
print(" window_rect:", win32gui.GetWindowRect(katalk_ad_win))
print(" ↑Found KakaoTalk ad window")
print("")
katalk_main_win = win_hwnd
katalk_main_rect = win32gui.GetWindowRect(katalk_main_win)
katalk_OnlineMainView = \
[child_hwnd for child_hwnd in child_hwnds
if "OnlineMainView" in win32gui.GetWindowText(child_hwnd)][0]
katalk_ContactListView = \
[child_hwnd for child_hwnd in child_hwnds
if "ContactListView" in win32gui.GetWindowText(child_hwnd)][0]
katalk_ChatRoomListView = \
[child_hwnd for child_hwnd in child_hwnds
if "ChatRoomListView" in win32gui.GetWindowText(child_hwnd)][0]
katalk_MoreView = \
[child_hwnd for child_hwnd in child_hwnds
if "MoreView" in win32gui.GetWindowText(child_hwnd)][0]
katalk_LockModeView = \
[child_hwnd for child_hwnd in child_hwnds
if "LockModeView" in win32gui.GetWindowText(child_hwnd)][0]
katalk_ad_rect = win32gui.GetWindowRect(katalk_ad_win)
win32gui.ShowWindow(katalk_ad_win, win32con.SW_HIDE)
win32gui.SetWindowPos(katalk_OnlineMainView, win32con.HWND_BOTTOM, 0, 0,
(katalk_main_rect[2] - katalk_main_rect[0]),
(katalk_main_rect[3] - katalk_main_rect[1] - 33), win32con.SWP_NOMOVE)
win32gui.SetWindowPos(katalk_LockModeView, win32con.HWND_BOTTOM, 0, 0,
(katalk_main_rect[2] - katalk_main_rect[0]),
(katalk_main_rect[3] - katalk_main_rect[1]), win32con.SWP_NOMOVE)
# win32gui.SetWindowPos(katalk_ContactListView, win32con.HWND_BOTTOM, 0, 0, (rectKatalkMain[2] - rectKatalkMain[0]), (rectKatalkMain[3] - rectKatalkMain[1]), win32con.SWP_NOMOVE)
# win32gui.SetWindowPos(katalk_ChatRoomListView, win32con.HWND_BOTTOM, 0, 0, (rectKatalkMain[2] - rectKatalkMain[0]), (rectKatalkMain[3] - rectKatalkMain[1]), win32con.SWP_NOMOVE)
# win32gui.SetWindowPos(katalk_MoreView, win32con.HWND_BOTTOM, 0, 0, (rectKatalkMain[2] - rectKatalkMain[0]), (rectKatalkMain[3] - rectKatalkMain[1]), win32con.SWP_NOMOVE)
else:
print("KakaoTalk ad window not found or already removed")
input("Press enter to exit: ")
exit()
except SystemExit:
exit()
except: # some invalid process handle error
pass
print("KakaoTalk program or window not found")
input("Press enter to exit: ")
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment