python 进程通信测试代码(程序A,程序B)
时间:2010-08-20 来源:古代
http://old.nabble.com/-CPyUG--Python-%E5%92%8C-MFC%E7%A8%8B%E5%BA%8F-%E9%80%9A%E4%BF%A1%E7%9A%84%E9%97%AE%E9%A2%98-(%E8%AF%B7%E6%95%99Windows%E6%B6%88%E6%81%AF%E6%9C%BA%E5%88%B6%E9%AB%98%E6%89%8B)-td28851695.html
发送端程序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()