python 获取句柄,关闭进程
import win32api
import win32con
import win32gui
import win32ui
import win32console
import win32process
import os
import signal
MaiN_HWND = 0
def is_win_ok(hwnd, starttext):
s = win32gui.GetWindowText(hwnd)
if starttext in s:
print (s)
global MAIN_HWND
MAIN_HWND = hwnd
return None
return 1
def find_main_window(starttxt):
global MAIN_HWND
win32gui.EnumChildwindows(0, is_win_ok, starttxt)
return MAIN_HWND
def winfun(hwnd, lparam):
s = win32gui.GetWindowText(hwnd)
if len(s) > 3:
print(“winfun, child_hwnd: %d txt: %s” % (hwnd, s))
return 1
def main():
main_app = ‘Sublime’
main_app = ‘test.txt - 记事本’
#main_app = ‘F:\test\test.Go - Sublime Text ’
hwnd1 = win32ui.FindWindow(None,main_app)
# win32gui.CloseWindow(hwnd1)
print(hwnd1)
hwnd = win32gui.FindWindow(None, main_app)
print(win32gui.GetWindowText(hwnd))
print(type(hwnd))
print (hwnd)
thread,processId =win32process.GetWindowThreadProcessId(hwnd)
print(thread)
print(processId,signal.CTRL_BREAK_EVENT)
os.kill(processId,signal.CTRL_C_EVENT)
os.kill(processId,signal.CTRL_BREAK_EVENT)
win32gui.CloseWindow(hwnd)
if hwnd < 1:
hwnd = find_main_window(main_app)
print(type(hwnd))
print (hwnd)
# Shows or hides a window and changes its state
win32gui.ShowWindow(hwnd, 0)
# win32gui.HideCaret(hwnd)
#win32api.OpenProces
main()
相关文章