簡體   English   中英

我的SendMessage / PostMessage部分代碼有什么問題?

[英]What is wrong with my SendMessage/PostMessage part of code?

我創建了一個簡單的腳本,嘗試通過在此處輸入鏈接描述來計算權益:

import time
import win32api
import win32con

from pywinauto import application

def getEquity(ps_pid, hand1, hand2):
    def set_hand(handle, hand, kf=0):
        win32api.SendMessage(handle, win32con.WM_SETFOCUS, 0, 0) # f: losefocus
        #win32api.SendMessage(handle, win32con.WM_GETDLGCODE, 0, 0)
        time.sleep(0.05)
        len = win32api.SendMessage(handle, win32con.WM_GETTEXTLENGTH, 0, 0)
        time.sleep(0.05)
        win32api.SendMessage(handle, win32con.EM_SETSEL, 0, len)
        time.sleep(0.05)
        for c in hand:
            win32api.PostMessage(handle, win32con.WM_CHAR, ord(c), 0)
            #win32api.SendMessage(handle, win32con.WM_GETDLGCODE, 0, 0)
            time.sleep(0.05)
        win32api.SendMessage(handle, win32con.WM_KILLFOCUS, 0, 0)

    app = application.Application()
    app.connect_(process=ps_pid)

    set_hand(app.PokerStove.REdit1.handle, hand1)
    set_hand(app.PokerStove.REdit2.handle, hand2)

    app.PokerStove.Evaluate.Click()
    while app.PokerStove.EvaluateButton.WindowText() != 'Evaluate':
        time.sleep(0.1)

    return app.PokerStove.Edit12.GetLine(0)

import sys
print getEquity(int(sys.argv[1]), sys.argv[2], sys.argv[3])

我決定使用窗口消息而不是SendKey,因為在最小化PokerStove的同時也需要使其工作。

當最小化PokerStove時,此腳本可以正常工作。 但是,如果不是,就會發生奇怪的事情。 腳本正確填充了文本編輯內容,然后單擊按鈕,我得到了正確的結果。 但是之后,將他的標題更改為奇怪的東西:

撲克爐

這樣看來,PokerStove仍在計算,但結果已經准備就緒。 由於此更改,當我再次啟動腳本時,它將失敗。 但是當最小化PokerStove時,我不會遇到這個問題。

我懷疑在發送消息到編輯框時做錯了什么。 因為如果我手動填充它們並單擊按鈕,那么一切都很好。 當我使用set_hand函數填充它時,即使我手動單擊按鈕,也會得到這個奇怪的結果。

那么我的腳本怎么了?

編輯:

當我將spy ++連接到EvaluateButton時,我可以看到該按鈕仍然收到WM_SETTEXT消息,將其設置為“停止(99%完成)”。

編輯2:

它已在Windows 7上進行了測試。但是,在Windows XP上的VirtualBox中,在家中代碼工作正常。

您使用PostMessage發送字符。 此功能是異步的。 嗯,跟隨SendMessage的結果可能很奇怪。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM