def isSC2onForeground():
"""Detect if SC2-Client is the foreground window (only Windows)."""
try:
fg_window_name = GetWindowText(GetForegroundWindow()).lower()
sc2 = "StarCraft II".lower()
return fg_window_name == sc2
except Exception as e:
module_logger.exception("message")
return False
python类GetForegroundWindow()的实例源码
def funWin():
print(win32gui.GetForegroundWindow())
def get_active_window_handle():
hwnd = win32gui.GetForegroundWindow()
return hwnd
def get_foreground_window():
"""
Returns the currently focused window's hwnd.
Returns:
(int): The focused window's window handler.
"""
return wg.GetForegroundWindow()
def set_window_top():
hwnd = win32gui.GetForegroundWindow()
(left, top, right, bottom) = win32gui.GetWindowRect(hwnd)
win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, left, top, right - left, bottom - top, 0)
def GetFocus(cls):
curtid = win32api.GetCurrentThreadId()
whd = win32gui.GetForegroundWindow()
(tid, pid) = win32process.GetWindowThreadProcessId(whd)
win32process.AttachThreadInput(curtid, tid,True)
focus_whd = win32gui.GetFocus()
win32process.AttachThreadInput(curtid, tid, False)
return focus_whd
def _click(x, y):
handle = win32gui.GetForegroundWindow()
x_old, y_old = win32api.GetCursorPos()
# see https://github.com/asweigart/pyautogui/issues/23
try:
pyautogui.click(x, y, 1)
except FileNotFoundError:
pass
win32api.SetCursorPos((x_old, y_old))
win32gui.SetForegroundWindow(handle)
def run(self):
if isWindows:
global windowsWindow
global isOnTop
wasOnTop = False
while True:
if windowsWindow:
focused = False
fg = win32gui.GetForegroundWindow()
if windowsWindow == fg:
focused = True
else:
for win in sublime.windows():
if win.hwnd() == fg:
focused = True
# print("focused....")
# print(focused)
if focused == False:
if isOnTop != False:
wasOnTop = True
set_always_on_top(False)
# print("moving to bottom")
if focused == True:
if isOnTop != True:
if wasOnTop == True:
wasOnTop = False
try:
win32gui.SetFocus(windowsWindow)
except Exception as e:
pass
time.sleep(0.1)
set_always_on_top(True)
# print("moving to top")
time.sleep(0.2)
else:
time.sleep(3)
def is_desktop_on_foreground(self): # pylint: disable=no-self-use
"""Detect "show desktop" or something close enough."""
foreground = win32gui.GetForegroundWindow()
return bool(
foreground and win32gui.GetClassName(foreground) == "WorkerW")
def captureAlbumArt(self):
while(self.windowHandle != GetForegroundWindow()):
SetForegroundWindow(self.windowHandle)
time.sleep(WAIT_TIME) ## Give Spotify a moment to come to the foreground
ShowWindow(self.windowHandle, SW_SHOWMAXIMIZED)
time.sleep(WAIT_TIME) ## Give Spotify a second to become maximized
## Get the edges of the window
left, top, right, bottom = GetWindowRect(self.windowHandle)
left += self.artOffsets[0]
bottom -= self.artOffsets[1]
## Get the album art's location from those edges and user specified offsets.
region = (left, bottom - self.artSideLength, left + self.artSideLength, bottom)
return pyscreenshot.grab(bbox=region, childprocess=False)
def funWin():
print win32gui.GetForegroundWindow()
#create pyhk class instance
def FX_GetForegroundWindow():
window_info = FX_WINDOW_INFO()
window_info.Win32Window = win32gui.GetForegroundWindow()
thread_process = win32process.GetWindowThreadProcessId(window_info.Win32Window)
window_info.ProcessID = thread_process[1]
window_info.ThreadID = thread_process[0]
window_info.Title = win32gui.GetWindowText(window_info.Win32Window)
return FX_Window(window_info)
def GetForegroundWindow(self):
win_info = FX_WINDOW_INFO()
win_info.Window = win32gui.GetForegroundWindow()
thread_process = win32process.GetWindowThreadProcessId(win_info.Window)
self.ProcessID = thread_process[1]
self.ThreadID = thread_process[0]
win_info.Title = win32gui.GetWindowText(win_info.Win32Window)
return FX_Window(win_info)
def on_message(ws, message):
print(message)
# class FocusWatcher(sublime_plugin.EventListener):
# pending_toggle = False
# prev_is_on_top = False
# already_in_progress = False
# def on_deactivated_async(self, view):
# if self.already_in_progress:
# return
# self.already_in_progress = True
# focused = False
# if isWindows and self.pending_toggle == False:
# global windowsWindow
# if windowsWindow:
# fg = win32gui.GetForegroundWindow()
# if windowsWindow == fg:
# focused = True
# else:
# for win in sublime.windows():
# if win.hwnd() == fg:
# focused = True
# if focused == False:
# global isOnTop
# self.prev_is_on_top = isOnTop
# set_always_on_top(False)
# self.pending_toggle = True
# print("on deactived, focused:")
# print(focused)
# self.already_in_progress = False
# def on_activated_async(self, view):
# if self.already_in_progress:
# return
# self.already_in_progress = True
# print(1)
# if isWindows and self.pending_toggle:
# print(2)
# global windowsWindow
# if windowsWindow:
# self.pending_toggle = False
# print(3)
# print(self.prev_is_on_top)
# try:
# if self.prev_is_on_top:
# # win32gui.SetForegroundWindow(windowsWindow)
# # time.sleep(0.05)
# win32gui.SetFocus(windowsWindow)
# set_always_on_top(True)
# # time.sleep(0.05)
# # win32gui.SetForegroundWindow(view.window().hwnd())
# except Exception as e:
# pass
# print(4)
# self.already_in_progress = False