下面是建立服务器应用的方法: 1.创建一个新的标准EXE文件; 2.加入一个Winsock控件; 3.加入如下代码: Private Sub Form Load() tcpServer.LocalPort = 1001 tcpServer.Localhost = ″servser″ tcpServer.remotePort = 1002 tcpServer.Localhost = ″klint″ tcpServer.Listen End Sub ′连接检查 Private Sub tcpServer ConnectionRequest (ByVal requestID As Long) If tcpServer.State <> sckClosed Then tcpServer.Close tcpServer.Accept requestID End Sub ′发送数据 Private Sub frmserver monsemove(x,y) tcpServer.SendData ″x″& str(x) tcpServer.SendData ″y″& str(y) End Sub 建立客户应用的方法为: 1.创建一个新的标准EXE文件; 2.加入一个Winsock控件; 3.加入两个TEXT框—— txt x和 txt y; 4.加入如下代码: Private Sub Form Load() tcpServer.LocalPort = 1002 tcpServer.Localhost = ″klint″ tcpServer.remotePort = 1001 tcpServer.Localhost = ″servser″ tcpServer.Listen End Sub ′连接检查 Private Sub tcpklint ConnectionRequest (ByVal requestID As Long) If tcpklint.State <> sckClosed Then tcpklint.Close tcpklint.Accept requestID End Sub Private Sub tcpClient DataArrival (ByVal bytesTotal As Long) Dim strData As String tcpklint.GetData strData if left(strData,1)=″X″then txt x.Text = strData else txt y.Text = strData endif End Sub
用子类最简单,重载窗口处理函数,然后你截获你自定消息即可’子类化你可搜索以前的帖子,这方面挺多Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long hwnd 用Findwindow 先获得该句柄,最好给自己的程序起个长一些的标题(与其它区别)。
Private Const HWND_BROADCAST=&HFFFF&
TO: yefanqiu(叶帆) 在程序B中怎么接收A发出的信息
Option Explicit Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 工程一 Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Private Const MY_MSG = WM_USER + 1000 '最好+WM_USER,区别系统消息Private Sub Command1_Click() Dim hwnd As Long hwnd = FindWindow(vbNullString, "Form2") SendMessage hwnd, MY_MSG, 0, 0 '向另一工程的窗体Form2发消息,注Form2为窗体标题 End Sub‘工程二 Option ExplicitPrivate Sub Form_Load() ' 设置新的窗口信息处理进程 '窗口进程 '窗口进程地址 OldWindowProc = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf NewWindowProc)End SubPrivate Sub Form_Unload(Cancel As Integer) ' 恢复原来窗口信息处理进程. SetWindowLong Me.hwnd, GWL_WNDPROC, OldWindowProc End Sub'工程二的模块 Option Explicit Public OldWindowProc As Long '旧的窗口进程号'将消息传答窗口函数 Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long '设置窗口附加内存长型数值 Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Public Const GWL_WNDPROC = (-4) Public Const WM_USER = &H400 Public Const MY_MSG = WM_USER + 1000 '最好+WM_USER,区别系统消息 '************************************************************************* '**函 数 名:NewWindowProc '**输 入:ByVal hwnd(Long) - '** :ByVal Msg(Long) - '** :ByVal wParam(Long) - '** :ByVal lParam(Long) - '**输 出:(Long) - '**功能描述:新的窗口进程 '************************************************************************* Public Function NewWindowProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long If Msg = MY_MSG Then Form2.Text1.Text = "收到" End If '发送其余的消息到原先的窗口信息处理进程 NewWindowProc = CallWindowProc(OldWindowProc, hwnd, Msg, wParam, lParam) End Function
各个vb学习网站上都有这样的例子
windows系统中的消息概念?
还是指程序中变量的传递?前者要用api,后者就很简单的!
1.创建一个新的标准EXE文件;
2.加入一个Winsock控件;
3.加入如下代码:
Private Sub Form Load()
tcpServer.LocalPort = 1001
tcpServer.Localhost = ″servser″
tcpServer.remotePort = 1002
tcpServer.Localhost = ″klint″
tcpServer.Listen
End Sub
′连接检查
Private Sub tcpServer ConnectionRequest
(ByVal requestID As Long)
If tcpServer.State <> sckClosed Then
tcpServer.Close
tcpServer.Accept requestID
End Sub
′发送数据
Private Sub frmserver monsemove(x,y)
tcpServer.SendData ″x″& str(x)
tcpServer.SendData ″y″& str(y)
End Sub
建立客户应用的方法为:
1.创建一个新的标准EXE文件;
2.加入一个Winsock控件;
3.加入两个TEXT框—— txt x和 txt y;
4.加入如下代码:
Private Sub Form Load()
tcpServer.LocalPort = 1002
tcpServer.Localhost = ″klint″
tcpServer.remotePort = 1001
tcpServer.Localhost = ″servser″
tcpServer.Listen
End Sub
′连接检查
Private Sub tcpklint ConnectionRequest
(ByVal requestID As Long)
If tcpklint.State <> sckClosed Then
tcpklint.Close
tcpklint.Accept requestID
End Sub
Private Sub tcpClient DataArrival
(ByVal bytesTotal As Long)
Dim strData As String
tcpklint.GetData strData
if left(strData,1)=″X″then
txt x.Text = strData
else
txt y.Text = strData
endif
End Sub
1、利用的DDE方式(古老的window32办法)同一台机器上
2、利用邮槽方式(非连接方式)
CreateMailSlot
CreateFile
利用管道方式(连接方式)
CreatePipe
CreateFile
其中第2种方法可以在非同一台机器上,需要netbios的支持,需安装TCP/IP协议
3、用winsock建立客户/服务机方案
也有连接和非连接两种方案
hwnd 用Findwindow 先获得该句柄,最好给自己的程序起个长一些的标题(与其它区别)。
在程序B中怎么接收A发出的信息
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
工程一
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const MY_MSG = WM_USER + 1000 '最好+WM_USER,区别系统消息Private Sub Command1_Click()
Dim hwnd As Long
hwnd = FindWindow(vbNullString, "Form2")
SendMessage hwnd, MY_MSG, 0, 0 '向另一工程的窗体Form2发消息,注Form2为窗体标题
End Sub‘工程二
Option ExplicitPrivate Sub Form_Load()
' 设置新的窗口信息处理进程 '窗口进程 '窗口进程地址
OldWindowProc = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf NewWindowProc)End SubPrivate Sub Form_Unload(Cancel As Integer)
' 恢复原来窗口信息处理进程.
SetWindowLong Me.hwnd, GWL_WNDPROC, OldWindowProc
End Sub'工程二的模块
Option Explicit
Public OldWindowProc As Long '旧的窗口进程号'将消息传答窗口函数
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'设置窗口附加内存长型数值
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Const GWL_WNDPROC = (-4)
Public Const WM_USER = &H400
Public Const MY_MSG = WM_USER + 1000 '最好+WM_USER,区别系统消息
'*************************************************************************
'**函 数 名:NewWindowProc
'**输 入:ByVal hwnd(Long) -
'** :ByVal Msg(Long) -
'** :ByVal wParam(Long) -
'** :ByVal lParam(Long) -
'**输 出:(Long) -
'**功能描述:新的窗口进程
'*************************************************************************
Public Function NewWindowProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = MY_MSG Then
Form2.Text1.Text = "收到"
End If
'发送其余的消息到原先的窗口信息处理进程
NewWindowProc = CallWindowProc(OldWindowProc, hwnd, Msg, wParam, lParam)
End Function