Pywin32-notepad

5066 ワード

document
Pythonとwin 32のプログラミングの例を利用して、必要に応じてボタンの精霊をカスタマイズします
SendMessageの使い方の詳細とメッセージの詳細
pywin 32でwindowsシミュレーションマウスとキーボード動作を実現
PyWin32 Documentation
SetForegroundWindowの正しい使い方
WinAPI:FindWindow、FindWindowEx-検索ウィンドウ
ctypesのwindllによりuser 32はwin 32 apiを呼び出す
pythonパッケージング方法
中国語windowsの下でpywinautoを使ってウィンドウ操作を行う(一)
notepad保存、ファイル名とファイルパスの指定
# coding=utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import os
import time
import win32gui
import win32api
import win32con

def find_idxSubHandle(pHandle, winClass, index=0):
    """              index          """
    assert type(index) == int and index >= 0
    handle = win32gui.FindWindowEx(pHandle, 0, winClass, None)
    while index > 0:
        handle = win32gui.FindWindowEx(pHandle, handle, winClass, None)
        index -= 1
    return handle

def find_subHandle(pHandle, winClassList):
    """
              
    pHandle        
    winClassList       class  ,   list-index    
    """
    assert type(winClassList) == list
    if len(winClassList) == 1:
        return find_idxSubHandle(pHandle, winClassList[0][0], winClassList[0][1])
    else:
        pHandle = find_idxSubHandle(pHandle, winClassList[0][0], winClassList[0][1])
        return find_subHandle(pHandle, winClassList[1:])

def p_sub_handle(phandle):
    """  phandle      """
    handle = -1
    while handle !=0 :
        if handle == -1:
            handle = 0
        handle = win32gui.FindWindowEx(phandle, handle, None, None)
        if handle != 0:
            print win32gui.GetClassName(handle)

hd_notepad = win32gui.FindWindow("notepad", None)
print win32gui.GetWindowText(hd_notepad)
#  notepad     
hd_menu = win32gui.GetMenu(hd_notepad)
hd_submenu = win32gui.GetSubMenu(hd_menu, 0)
#      
hd_menuid = win32gui.GetMenuItemID(hd_submenu, 2)
# 0  ,      3 
win32gui.PostMessage(hd_notepad, win32con.WM_COMMAND, hd_menuid, 0)
#win32gui.SendMessage(hd_notepad, win32con.WM_COMMAND, hd_menuid, 0)
win32api.Sleep(200)
#       ,      handle
dig_handle = win32gui.FindWindow(None, u"   ")
print win32gui.GetWindowText(dig_handle)
con_button_handle = win32gui.FindWindowEx(dig_handle, 0, "Button", None)
print win32gui.GetWindowText(con_button_handle)
#handle = find_subHandle(dig_handle, [("WorkerW", 0), ("ReBarWindow32", 0), ("Address Band Root", 0), ("msctls_progress32", 0), ("ComboBoxEx32",0), ("ComboBox", 0), ("Edit", 0)])
handle = find_subHandle(dig_handle, [("WorkerW", 0), ("ReBarWindow32", 0), ("Address Band Root", 0), ("msctls_progress32", 0)])
#    ,                

# win32gui.SendMessage(handle, win32con.WM_LBUTTONDOWN, 0, 0)
# win32api.Sleep(200)
# win32gui.SendMessage(handle, win32con.WM_LBUTTONUP, 0, 0)
#        

handle = find_subHandle(dig_handle, [("DUIViewWndClassName", 0), ("DirectUIHWND", 0), ("FloatNotifySink", 0), ("ComboBox", 0), ("Edit", 0)])
#  handle    
win32gui.SetForegroundWindow(handle)
#       
win32gui.SendMessage(handle, win32con.WM_LBUTTONDOWN, 0, 0)
win32api.Sleep(200)
win32gui.SendMessage(handle, win32con.WM_LBUTTONUP, 0, 0)
win32api.Sleep(200)
fgFilePath = "d:/1"
print win32gui.GetWindowText(handle)
print handle
print win32api.SendMessage(handle, win32con.WM_SETTEXT, 0, os.path.abspath(fgFilePath))
#        ,       ,           

#            ,         ,      ,         。
def findPicPick():
    hd_list = []

    #    
    def foo(hwnd, mouse):
        #                
        if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
            #    
            #print win32gui.GetWindowText(hwnd)
            if "PicPick" in win32gui.GetWindowText(hwnd):
                hd_list.append(hwnd)

    win32gui.EnumWindows(foo, 0)

    #hd_notepad = win32gui.FindWindow("notepad", None)   
    #hd2 = win32gui.FindWindow(None, "TfrmMDIEditor")   ,   picpcik
    #print hd_notepad
    #print hd2

    win32api.keybd_event(18,0,0,0)    # Alt
    win32api.keybd_event(18, 0, win32con.KEYEVENTF_KEYUP, 0)
    win32api.keybd_event(70,0,0,0)    # F
    win32api.keybd_event(70, 0, win32con.KEYEVENTF_KEYUP, 0)
handle = win32gui.FindWindowEx(phandle, handle, None, None) pHandleのサブコントロールを探して、handleの後の最初のサブコントロール、Noneはそれぞれクラス名とtitleです.hd_notepad = win32gui.FindWindow("notepad", None)クラス名「notepad」,title=Noneのウィンドウハンドルを見つける
win32gui.GetClassName(handle)
win32gui.GetWindowText(hd_notepad)
win32api.Sleep(200)
win32gui.SetForegroundWindow(handle)
win32gui.EnumWindows(foo, 0)
win32api.keybd_event(18,0,0,0)    # Alt
win32gui.PostMessage(hd_notepad, win32con.WM_COMMAND, hd_menuid, 0)
win32gui.SendMessage(hd_notepad, win32con.WM_COMMAND, hd_menuid, 0)

postは値を返さず、実行が完了したらすぐに戻ります.sendは実行結果を返し,プログラム実行が完了するまで戻る必要がある.