python+win32:检测窗口拖动 [英] python+win32: detect window drag

查看:29
本文介绍了python+win32:检测窗口拖动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检测何时使用 python/pywin32 在窗口中拖动不属于我的应用程序的窗口?我想设置它,以便当我拖动一个标题与桌面边缘附近的模式匹配的窗口时,当松开鼠标时它会捕捉到边缘.我可以编写代码,在松开鼠标时将所有具有该标题的窗口捕捉到桌面,但我只想移动被拖动的特定窗口.

Is there a way to detect when a window that doesn't belong to my application is being dragged in windows using python/pywin32? I want to set it up so that when I drag a window whose title matches a pattern near the desktop edge, it snaps to the edge when the mouse is let go. I could write code to snap all windows with that title to the desktop whenever the mouse is released, but I want to only move the particular window that was being dragged.

推荐答案

pyHook 似乎已经完成了一些必要的工作,因为它挂钩了键盘和鼠标事件.我可能会做的是不断记录我关心的所有窗口,以及它们的位置.然后,在鼠标抬起时,我会检测是否有任何窗口移动,如果是,并且它靠近鼠标松开的位置,在标题栏上,我会假设它被拖到那里并捕捉它.挂钩代码如下.

pyHook seems to have done some of the work necessary, as it's hooked keyboard and mouse events. What I will probably do is keep a constant record of all the windows I care about, along with their positions. Then, on mouse up, I'll detect if any of the windows moved, and if so, and it's near where the mouse was let go, on the title-bar, I'll assume it was dragged there and snap it. Code to hook follows.

import pyHook

def mouseUp(event):
    if event.Injected: return True

    print "Mouse went up"
    return True

hookManager = pyHook.HookManager()
hookManager.MouseLeftUp = mouseUp
hookManager.HookMouse()

您还需要一个主循环,因为我已经在使用 gtk,或者您可以这样做:

You also need a main loop, which I have since I'm using gtk already, or you can do:

import pythoncom
pythoncom.PumpMessages()

这篇关于python+win32:检测窗口拖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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