views:

32

answers:

2

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.

+1  A: 

So far the only possible solution I see is to use SetWindowsHookEx. Pywin32 doesn't interface this, so I think I'll have to do something like this:

  • Write a C extension module. It has a function like setCallback which takes a python function to be called when the drag event happens.
  • Write a C DLL that contains the actual hook into windows. This DLL will somehow have to call the python function that is currently set.

I'm not sure how to do these, or if it's correct, though..

Claudiu
@Claudiu, please come back and post any updates to this question. I'm very curious to see what you come up with.
Mark
@Mark: check out my new answer, looks like someone did part of the work for me =)
Claudiu
+1  A: 

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()

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

import pythoncom
pythoncom.PumpMessages()
Claudiu