pythonwin, GetWindowText hangup & more

Miki Tebeka tebeka at lycosmail.com
Mon May 20 00:50:53 EDT 2002


Hello All,

I'm trying to write a small application that will kill any leftovers
from
an application crash.

I took the win32gui_taskbar.py demo and changed it a bit.
In the loop starting at line 80 there is a hangup when calling
GetWindowText. When I run this code isolated it seems to work find.

Any ideas?

And another question:
How do I get process id from hwnd? (Can't see GetWindowThreadProcessId
in win32api)

Thanks.

Miki.

The code (since I'm posting from Google groups I couldn't attach the
icon):

'''Kill Bee '''
from win32api import *
import win32api # for explicit MessageBox
from win32gui import *
from win32process import *
import win32con
import sys, os

VERSION = "1.0"

                
class MainWindow:
    
    def __init__(self):
        
        # Tray message
        self.ID_TRAYMSG = win32con.WM_USER + 20
        # Menu Messages 
        self.ID_KILL = 1023
        self.ID_ABOUT = 1024
        self.ID_QUIT = 1025
        
        message_map = {
            win32con.WM_DESTROY: self.OnDestroy,
            win32con.WM_COMMAND: self.OnCommand,
            self.ID_TRAYMSG : self.OnTaskbarNotify,
        }
        

        # Register the Window class.
        wc = WNDCLASS()
        hinst = wc.hInstance = GetModuleHandle(None)
        wc.lpszClassName = "BeeKiller"
        wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW;
        wc.hCursor = LoadCursor( 0, win32con.IDC_ARROW )
        wc.hbrBackground = win32con.COLOR_WINDOW
        wc.lpfnWndProc = message_map # could also specify a wndproc.
        classAtom = RegisterClass(wc)
        
        # Create the Window.
        style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
        self.hwnd = CreateWindow( classAtom, "BeeKiller", style, \
                    0, 0, win32con.CW_USEDEFAULT,
win32con.CW_USEDEFAULT, \
                    0, 0, hinst, None)
        UpdateWindow(self.hwnd)

        # Custom icon
        iconPathName = '%s/beekiller.ico' % os.getcwd()
        assert(os.path.isfile(iconPathName))
        icon_flags = win32con.LR_LOADFROMFILE |
win32con.LR_DEFAULTSIZE
        hicon = LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0,
0, icon_flags)

        flags = NIF_ICON | NIF_MESSAGE | NIF_TIP
        nid = (self.hwnd, 0, flags, self.ID_TRAYMSG, hicon, "Bee
Killer")
        Shell_NotifyIcon(NIM_ADD, nid)

    def OnDestroy(self, hwnd, msg, wparam, lparam):
        nid = (self.hwnd, 0)
        Shell_NotifyIcon(NIM_DELETE, nid)
        PostQuitMessage(0) # Terminate the app.

    def OnTaskbarNotify(self, hwnd, msg, wparam, lparam):
        if lparam==win32con.WM_LBUTTONUP or \
           lparam==win32con.WM_LBUTTONDBLCLK or \
           lparam==win32con.WM_RBUTTONUP:
            menu = CreatePopupMenu()
            AppendMenu( menu, win32con.MF_STRING, self.ID_KILL, "Kill
Bee")
            AppendMenu( menu, win32con.MF_STRING, self.ID_ABOUT,
"About")
            AppendMenu( menu, win32con.MF_STRING, self.ID_QUIT, "Quit
Program" )
            pos = GetCursorPos()
            TrackPopupMenu(menu, win32con.TPM_LEFTALIGN, pos[0],
pos[1], 0, self.hwnd, None)
        return 1


    def OnCommand(self, hwnd, msg, wparam, lparam):
        id = LOWORD(wparam)
        if id == self.ID_KILL:
            windows = []
            EnumWindows(lambda h, w: w.append(h), windows)
            for w in windows:
                title = GetWindowText(w)
                if not title:
                    title = 'Unknown'
                print '%s: %s' % (w, title)
        elif id == self.ID_ABOUT:
            win32api.MessageBox(0, "BeeKiller %s" % VERSION,
"BeeKiller", win32con.MB_OK | win32con.MB_TOPMOST)
        elif id == self.ID_QUIT:
            DestroyWindow(self.hwnd)
        else:
            print "Unknown command -", id

def main():
    w=MainWindow()
    PumpMessages()

if __name__=='__main__':
    main()



More information about the Python-list mailing list