只要是在电脑上运行的程序都能检测,不知道窗口名字的鼠标指着任务栏它就会显示,例如我的是QQ游戏,此代码适用与辅助开发专用,对于非开发者此代码并无意义。 import tkinter as tk from tkinter import ttk, messagebox import ctypes import threading class WindowDetector: def __init__(self, root): self.root = root self.root.title("游戏窗口检测专用工具") self.root.geometry("420x280") self.root.configure(bg="#f5f5f5")
self.create_widgets()
def create_widgets(self): # 标题 title_label = tk.Label( self.root, text="自定义搜索", font=("微软雅黑", 16, "bold"), pady=15, bg="#f5f5f5" ) title_label.pack()
# 输入框区域 input_frame = tk.Frame(self.root, bg="#f5f5f5") input_frame.pack(pady=10)
tk.Label( input_frame, text="游戏窗口标题:", font=("微软雅黑", 10), bg="#f5f5f5" ).grid(row=0, column=0, padx=5)
self.window_title = tk.StringVar() self.entry = ttk.Entry( input_frame, textvariable=self.window_title, width=32, font=("微软雅黑", 10) ) self.entry.grid(row=0, column=1, padx=5)
# 红色检测按钮 self.btn = tk.Button( self.root, text="立即检测", command=self.start_detection, font=("微软雅黑", 10, "bold"), fg="red", bg="white", activeforeground="red", relief=tk.GROOVE, bd=2, padx=20, pady=5 ) self.btn.pack(pady=15)
# 状态标签 self.status = tk.Label( self.root, text="等待检测...", font=("微软雅黑", 10), fg="gray", bg="#f5f5f5" ) self.status.pack()
def check_window(self, title): hwnd = ctypes.windll.user32.FindWindowW(None, title) return hwnd != 0
def start_detection(self): title = self.window_title.get().strip() if not title: messagebox.showwarning("警告", "请输入窗口标题") return
self.btn.config(state="disabled") self.status.config(text="检测中...", fg="orange")
threading.Thread( target=self.do_detection, args=(title,), daemon=True ).start()
def do_detection(self, title): is_running = self.check_window(title) self.root.after(0, lambda: self.show_result(is_running, title))
def show_result(self, is_running, title): if is_running: self.root.config(bg="#e6f3ff") self.status.config(text=f"找到窗口: {title}", fg="blue") else: self.root.config(bg="#ffe6e6") self.status.config(text=f"未找到窗口: {title}", fg="red")
self.btn.config(state="normal") if __name__ == "__main__": root = tk.Tk() app = WindowDetector(root) root.mainloop() |