1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

指定のウィンドウをアクティブ・解除するスクリプトをしたい

Last updated at Posted at 2024-03-10

コピペ大好きな自分にとってコピペ先のウィンドウを常に最前面にする必要があったので作成した。

選択したウィンドウが常に最前面かどうか取得してActiveなら解除にしたかったが見つからなかった。

Python
import win32gui
import win32con
import time


def activewindow(wm=0):
    modify = ""
    try:
        # アクティブなウィンドウのハンドルを取得
        hwnd = win32gui.GetForegroundWindow()
        if hwnd == 0:
            raise Exception("アクティブなウィンドウが見つかりません。")

        # アクティブなウィンドウのタイトルを取得
        active_hwnd = win32gui.GetWindowText(hwnd)

        # ウィンドウの状態を変更
        if wm == 0:
            win32gui.SetWindowPos(hwnd, win32con.HWND_NOTOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE)
            modify = f"ActiveOFF: {active_hwnd}"
        elif wm == 1:
            win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE)
            modify = f"ActiveON: {active_hwnd}"
        else:
            raise ValueError("無効な入力値です。0 または 1 を指定してください。")

        print(modify)
    except Exception as e:
        print(f"エラー: {e}")


if __name__ == "__main__":
    try:
        x = 1
        print(3)
        for i in range(1,4):
            print(i)
            time.sleep(1)
            
        activewindow(x)
    except ValueError:
        print("無効な入力です。数値を入力してください。")

pykit_tool.send_notificationはwindowsの通知するための機能

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?