Created
October 12, 2017 13:55
-
-
Save dev001hajipro/2d8b6c8346d3da5669a624af052cb133 to your computer and use it in GitHub Desktop.
Python3.6とctypesでメモ帳を起動し、Win32APIで移動、リサイズ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| """ | |
| メモ帳起動して、移動 | |
| https://gist.github.com/miau/565417 | |
| """ | |
| import subprocess | |
| import time | |
| from ctypes import * | |
| from ctypes.wintypes import * | |
| def main(): | |
| # サブプロセス(メモ帳)の起動 | |
| child_process = subprocess.Popen(r'c:\windows\system32\notepad.exe') | |
| # プロセスチェック | |
| print(child_process.poll()) | |
| # ウェイト | |
| time.sleep(1) | |
| # メモ帳がフォアグラウンドにあるので、Win32APIのMoveWindowで(100,100)に移動し、500x500にリサイズ。 | |
| # hwnd = win32gui.GetForegroundWindow() | |
| # win32gui.MoveWindow(hwnd, 100, 100, 500, 500, True) | |
| # フォアグラウンドのウィンドウハンドル取得 | |
| hwnd = windll.user32.GetForegroundWindow() | |
| print("hwnd: ", hwnd) | |
| # ウィンドウタイトル取得 | |
| length = windll.user32.GetWindowTextLengthW(hwnd) | |
| print("title length: ", length) | |
| buff = create_unicode_buffer(length + 1) | |
| windll.user32.GetWindowTextW(hwnd, buff, length + 1) | |
| print(buff.value) | |
| # ウィンドウサイズ | |
| rect = RECT() | |
| windll.user32.GetWindowRect(hwnd, pointer(rect)) | |
| print(rect.left, rect.top, rect.right, rect.bottom) | |
| print(rect.right - rect.left, rect.bottom - rect.top) | |
| # ウィンドウの移動 | |
| windll.user32.MoveWindow(hwnd, 0, 0, 100, 100, 1) | |
| time.sleep(1) | |
| windll.user32.MoveWindow(hwnd, rect.left, rect.top, 500, 500, 1) | |
| child_process.wait() | |
| print('done.') | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment