[python-win32] Attaching Menu to Any App

Tim Roberts timr at probo.com
Thu May 15 19:33:17 CEST 2008


otaeris at o2.pl wrote:
> Thanks for your answer. 
> Yes, this is exactly what I want to acomplish.
> Application i'm working on is going to be called from that menu. Plan is to attach it automatically to some apps through running service or some process. 
>   

This seems like a silly thing to do.  What menu are you going to insert 
yourself in?  As a Windows user, I do not expect to access third-party 
applications via the standard menus in other applications.  There are 
better ways to do that.  If you need an application to be easily 
available at any time, the right thing to do is to set up a keyboard 
shortcut.  For example, I wrote a "while you were out" application for 
our office that we all use for taking telephone messages.  I create the 
icon with a shortcut of Alt-Shift-W, and now I can bring it up at any 
time, no matter what application is currently running.

> I red a lot about hooking WinApi but I don't realy know C that much. Writing a simple DLL sounds really easy.
>   

Well, I wouldn't say it's "easy".  It's not a lot of code, but it is 
tricky code.   You always have to remind yourself that "this function is 
running in someone else's process."  That means, among other things, you 
can't use malloc and free, because you don't know what run-time library 
is available.

> How do you mean subclassing a window and overtwritting windowproc can be done in python ?
>   

What *I* said was that it could NOT be done in pure Python, but I was 
expecting Mark Hammond or Tim Golden to contradict me in such a 
pessimistic statement.  ;)

A group at the University of North Carolina has developed several Python 
packages for automating access to and control of other Windows 
applications.  You might take a look through here:
    http://www.cs.unc.edu/Research/assist/developer.shtml

Their pyAA package seems to allow you to intercept events from another 
window.  However, that still won't allow you to modify their menus.  For 
that, I'm still convinced you'll need a C application.

I think you could do what you need with a WH_MSGFILTER hook.  This gets 
called every time a menu event happens (among other places).  You would 
figure out whether the message was for a menu that was opening, and if 
it was, add yourself to the menu, and subclass the window.  Then, when 
the menu closes, you could undo the subclass and remove yourself from 
the menu.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list