发送端程序A
#encoding=gbk
import wx
import win32con
import win32api
import win32gui
import os
import sys
#win32con.WM_USER+
class MyFrame(wx.Frame):
def __init__(self, parent=None):
wx.Frame.__init__(self, parent, -1, "testFind")
wx.Panel(self)
#hWnd = win32gui.FindWindow(None, "MSGWIN")
hWnd = win32gui.FindWindow(None, "MSGWIN")
if hWnd <> 0:
print hWnd
ret=win32gui.PostMessage(hWnd, win32con.WM_USER+779, 266,0)
#ret=win32gui.SendMessage(hWnd, win32con.WM_CLOSE, 0,0)
print ret
print 'END'
self.Destroy()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = MyFrame()
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()
def f_put(f,data,mode='wb'):
try:
fx = open(f,mode)
fx.write(data)
fx.close()
return True
except Exception,e:
return False
#f_put("f:\\whois.txt",response)
"""
hWnd = win32gui.FindWindow(None, "FrameEvent")
print hWnd
if hWnd <> 0:
win32api.SendMessage(hWnd , 300 , 300, 300)
pass
point = (555, 175)
x, y = point
win32api.SetCursorPos(point)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x, y, 0, 0)
win32api.SetCursorPos(point)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)
#ctrl + A
win32api.SendMessage(hWnd, 0, win32con.VK_CONTROL, 0)
win32api.Sleep(10)
win32api.SendMessage(hWnd, 0, 65, 0)
win32api.SendMessage(hWnd, win32con.KEYEVENTF_KEYUP, 65, 0)
win32api.SendMessage(hWnd, win32con.KEYEVENTF_KEYUP, win32con.VK_CONTROL, 0)
win32api.Sleep(10)
#ctrl + C
win32api.SendMessage(hWnd, win32con.WM_KEYDOWN, win32con.VK_CONTROL, 0)
win32api.Sleep(10)
win32api.SendMessage(hWnd, win32con.WM_KEYDOWN, 67, 0)
win32api.SendMessage(hWnd, win32con.WM_KEYUP, 67, 0)
win32api.SendMessage(hWnd, win32con.WM_KEYUP, win32con.VK_CONTROL, 0)
win32api.Sleep(10)
"""
接收端,程序B
import win32gui
import win32con
import win32api
import wx
class ListenWindow:
def __init__(self,parent):
wc = win32gui.WNDCLASS()
hinst = wc.hInstance = win32gui.GetModuleHandle(None)
self.message_map={
win32con.WM_DESTROY:self.OnDestroy,
win32con.WM_USER+779:self.OnMyMessage,
}
self.parent=parent
wc.lpszClassName = "Spark"
wc.lpfnWndProc = self.message_map
classAtom = win32gui.RegisterClass(wc)
style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
self.hwnd = win32gui.CreateWindow( "Spark", "MSGWIN", style,
0, 0, win32con.CW_USEDEFAULT,win32con.CW_USEDEFAULT,
0, 0, hinst, None)
def OnDestroy(self, hwnd, msg, wparam, lparam):
print "OnDestroy"
return 0
def OnMyMessage(self, hwnd, msg, wparam, lparam):
print wparam
self.parent.OnMyMessage(wparam, lparam)
print "I get it"
return 0
class MyFrame(wx.Frame):
def __init__(self, parent=None):
wx.Frame.__init__(self, parent, -1, "Demo")
listener = ListenWindow(self)
self.listener = listener
wx.Panel(self)
self.Bind(wx.EVT_CLOSE, self.OnClose)
#win32gui.SendMessage(self.listener.hwnd, win32con.WM_USER+779, 0, 0)
wc = win32gui.WNDCLASS()
self.message_map={
win32con.WM_DESTROY:self.OnClose,
win32con.WM_USER+779:self.OnMyMessage,
}
wc.lpszClassName = "DemoWND"
wc.lpfnWndProc = self.message_map
classAtom = win32gui.RegisterClass(wc)
def OnMyMessage(self,wparam,lparam):
print 'Frame'
print wparam
print 'End'
def OnClose(self, evt):
win32gui.SendMessage(self.listener.hwnd, win32con.WM_CLOSE, 0, 0)
self.Destroy()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = MyFrame()
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()