LoginSignup
2
4

More than 3 years have passed since last update.

Windows10で、ウインドウを最前面に設定/解除する方法

Posted at

Pythonで、Windows10のアプリケーションを最前面に表示/解除させる方法です。

以下のパッケージが必要です。

pip install pywin32

importするのはpywin32ではなく、win32guiとなります。

import win32gui

# ウインドウのハンドルを取得する
hwnd = win32gui.FindWindow(None, title) # windowのハンドルを取得

#  最前面表示の設定
win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0,
                      0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE)
# 最前面表示の解除
win32gui.SetWindowPos(self.hwnd, win32con.HWND_NOTOPMOST, 0,
                      0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE)

titleにはウインドウタイトルを入れてることで、それをキーにウインドウハンドルが取得できます。

(参考)
https://office54.net/python/app/windows-foreground-active
https://blog.goo.ne.jp/masaki_goo_2006/e/6ddf4977ba01dd11d6be256c17cc2ccf

2
4
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
2
4