Python win32api SendMesage [英] Python win32api SendMesage

查看:35
本文介绍了Python win32api SendMesage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图澄清 win32api.我只是做了一个简单的例子.获取记事本窗口,将鼠标移动到一个位置,单击并写入一个字符串.但它不起作用.有什么问题吗?
谁能帮我解释一下 lParam 参数是什么?
它有什么作用,它是什么类型以及它应该是什么样子?

导入win32api、win32con、win32gui、win32ui、win32service、os、时间def f_click(pycwnd):x=300y=300lParam = y <<15 |Xpycwnd.SendMessage(win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam);pycwnd.SendMessage(win32con.WM_LBUTTONUP, 0, lParam);def get_whndl():whndl = win32gui.FindWindowEx(0, 0, None, 'NB.txt - Notepad')返回 whndldef make_pycwnd(hwnd):PyCWnd = win32ui.CreateWindowFromHandle(hwnd)返回 PyCWnddef send_input_hax(pycwnd, msg):f_click(pycwnd)对于 msg 中的 c:如果 c == \n":pycwnd.SendMessage(win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)pycwnd.SendMessage(win32con.WM_KEYUP, win32con.VK_RETURN, 0)别的:pycwnd.SendMessage(win32con.WM_CHAR, ord(c), 0)pycwnd.UpdateWindow()whndl = get_whndl()pycwnd = make_pycwnd(whndl)msg = "它有效!\n";send_input_hax(pycwnd,msg)

解决方案

在记事本的主窗口中有另一个窗口,您需要向它发送消息.您可以使用 Microsoft Spy++ 工具看到这个隐藏"窗口,也可以像这样获取所有子窗口:

def 回调(hwnd, hwnds):如果 win32gui.IsWindowVisible(hwnd) 和 win32gui.IsWindowEnabled(hwnd):hwnds[win32gui.GetClassName(hwnd)] = hwnd返回真hwnds = {}win32gui.EnumChildWindows(whndl, callback, hwnds)

我们正在寻找的窗口具有编辑"类名,它是记事本唯一启用且可见的子窗口.所以你的代码会这样工作:

导入win32api、win32con、win32gui、win32ui、win32service、os、时间def f_click(pycwnd):x=300y=300lParam = y <<15 |Xpycwnd.SendMessage(win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam);pycwnd.SendMessage(win32con.WM_LBUTTONUP, 0, lParam);def get_whndl():whndl = win32gui.FindWindowEx(0, 0, None, 'NB.txt - Notepad')返回 whndldef make_pycwnd(hwnd):PyCWnd = win32ui.CreateWindowFromHandle(hwnd)返回 PyCWnddef send_input_hax(pycwnd, msg):f_click(pycwnd)对于 msg 中的 c:如果 c == "\n":pycwnd.SendMessage(win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)pycwnd.SendMessage(win32con.WM_KEYUP, win32con.VK_RETURN, 0)别的:pycwnd.SendMessage(win32con.WM_CHAR, ord(c), 0)pycwnd.UpdateWindow()whndl = get_whndl()定义回调(hwnd,hwnds):如果 win32gui.IsWindowVisible(hwnd) 和 win32gui.IsWindowEnabled(hwnd):hwnds[win32gui.GetClassName(hwnd)] = hwnd返回真hwnds = {}win32gui.EnumChildWindows(whndl, callback, hwnds)whndl = hwnds['编辑']pycwnd = make_pycwnd(whndl)msg = "它有效!\n"send_input_hax(pycwnd,msg)

lParam 是 int 并且您在这里看到的是一种技巧,它允许您通过单个参数传递多个值.假设我们需要将两位数字传递给一个只接受一个参数的函数.我们可以将它们作为两位数发送并在函数内拆分.同样的方式按位移位 (<<) 和按位或 (|) 操作在您的情况下也是可逆的:

<预><代码>>>>x = 300>>>y = 300>>>lParam = y <<15 |X>>>lParam &0x7FFF # x0:300>>>lParam >>15#年1:300

您可以在 维基百科Python 维基.

I am trying to clarify win32api. And I just made a simple example. Get the Notepad window, move the mouse to a position, click and write a string. But it does not work. What's the problem?
And could anybody clarify for me what the lParam parameter is?
What does it do, What type is it and How should it look?

import win32api, win32con, win32gui, win32ui, win32service, os, time



def f_click(pycwnd):
        x=300
        y=300
        lParam = y <<15 | x
        pycwnd.SendMessage(win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam);
        pycwnd.SendMessage(win32con.WM_LBUTTONUP, 0, lParam);

def get_whndl():
        whndl = win32gui.FindWindowEx(0, 0, None, 'NB.txt - Notepad')
        return whndl

def make_pycwnd(hwnd):       
        PyCWnd = win32ui.CreateWindowFromHandle(hwnd)
        return PyCWnd
        
def send_input_hax(pycwnd, msg):
    f_click(pycwnd)
    for c in msg:
        if c == "\n":
            pycwnd.SendMessage(win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
            pycwnd.SendMessage(win32con.WM_KEYUP, win32con.VK_RETURN, 0)
        else:
            pycwnd.SendMessage(win32con.WM_CHAR, ord(c), 0)
    pycwnd.UpdateWindow()
        
whndl = get_whndl()
pycwnd = make_pycwnd(whndl)
msg = "It works !\n"
send_input_hax(pycwnd,msg)

解决方案

There is another window inside a Notepad's main one, you need to send your messages to it. You can see this 'hidden' window with Microsoft Spy++ tool or you can get all child windows like so:

def callback(hwnd, hwnds):
    if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
        hwnds[win32gui.GetClassName(hwnd)] = hwnd
    return True

hwnds = {}
win32gui.EnumChildWindows(whndl, callback, hwnds)

Window we are looking for has 'Edit' class name and it is the only enabled and visible child window for Notepad. So your code will work this way:

import win32api, win32con, win32gui, win32ui, win32service, os, time


def f_click(pycwnd):
    x=300
    y=300
    lParam = y <<15 | x
    pycwnd.SendMessage(win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam);
    pycwnd.SendMessage(win32con.WM_LBUTTONUP, 0, lParam);

def get_whndl():
    whndl = win32gui.FindWindowEx(0, 0, None, 'NB.txt - Notepad')
    return whndl

def make_pycwnd(hwnd):       
    PyCWnd = win32ui.CreateWindowFromHandle(hwnd)
    return PyCWnd

def send_input_hax(pycwnd, msg):
    f_click(pycwnd)
    for c in msg:
        if c == "\n":
            pycwnd.SendMessage(win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
            pycwnd.SendMessage(win32con.WM_KEYUP, win32con.VK_RETURN, 0)
        else:
            pycwnd.SendMessage(win32con.WM_CHAR, ord(c), 0)
    pycwnd.UpdateWindow()

whndl = get_whndl()

def callback(hwnd, hwnds):
    if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
        hwnds[win32gui.GetClassName(hwnd)] = hwnd
    return True
hwnds = {}
win32gui.EnumChildWindows(whndl, callback, hwnds)
whndl = hwnds['Edit']

pycwnd = make_pycwnd(whndl)
msg = "It works !\n"
send_input_hax(pycwnd,msg)

lParam is int and what you see here is trick that allows you to pass more than one value through a single argument. Let's say that we need to pass two digits to a function which takes only one argument. We can send them as double digit number and split it inside function. Same way bitwise shift (<<) and bitwise or (|) operations are also reversable in your case:

>>> x = 300
>>> y = 300
>>> lParam = y << 15 | x
>>> lParam & 0x7FFF # x
0: 300
>>> lParam >> 15 # y
1: 300

You can read more about bitwise operations in Wikipedia and Python Wiki.

这篇关于Python win32api SendMesage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆