重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
两个方法
网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了西畴免费建站欢迎大家使用!
1.
要接收的窗口先使用FindWindow这个api获得句柄
调用SetForegroundWindow使其获得焦点并选中
调用.net方法:SendKeys.Send方法发送
2.使用API的SendInput
可以用动态数据交换(Dynamic Data Exchange,DDE)实现进程之间的通信。
发送端:
添加Command1,Text1控件,Text1的内容用于发送。
代码如下:
Private Sub Command1_Click()
Dim t As Long
Text1.LinkMode = 0
Text1.LinkTopic = "Child|FrmChild" '注意此处一定与接收的程序名称和连接的标题相同否则连接不成功。
Text1.LinkMode = 2
Text1.LinkExecute Text1.Text
t = Text1.LinkTimeout
Text1.LinkTimeout = 1
Text1.LinkMode = 0
Text1.LinkTimeout = t
End Sub
接收端:
添加Text1控件,Text1的内容用于接收。设置工程名称为Child(与发送的程序名对应),设置窗体的LinkMode属性为1,LinkTopic为FrmChild(对应上面发送的标题)。
Private Sub Form_LinkExecute(CmdStr As String, Cancel As Integer)
Text1 = CmdStr
Cancel = 0
End Sub
编译后,先运行接收端,然后运行发送端,输入内容,发送即可成功。
用 vb.net socket通信
Dim th As Threading.Thread
2 Dim tcpl As System.Net.Sockets.TcpListener
3
4 Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
5 th = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf MyListen))
6 th.Start()
7 End Sub
8
9 Public Sub SendMessage()Sub SendMessage(ByVal IP As String, ByVal SendMsg As String)
10 Try
11 If IP "" Then
12 Dim tcpc As New System.Net.Sockets.TcpClient(IP, 5656)
13 Dim tcpStream As Net.Sockets.NetworkStream = tcpc.GetStream
14 Dim reqStream As New IO.StreamWriter(tcpStream)
15 reqStream.Write(SendMsg)
16 reqStream.Flush()
17 tcpStream.Close()
18 tcpc.Close()
19 End If
20 Catch ex As Exception
21 MsgBox(ex.Message.ToString)
22 End Try
23 End Sub
24 Private Sub MyListen()Sub MyListen()
25 Try
26 Dim ipAddress As System.Net.IPAddress = System.Net.Dns.Resolve(System.Net.Dns.GetHostName).AddressList(0)
27 tcpl = New System.Net.Sockets.TcpListener(ipAddress, 5656)
28 tcpl.Start()
29 While True
30 Dim s As System.Net.Sockets.Socket = tcpl.AcceptSocket()
31 Dim MyBuffer(1024) As Byte
32 Dim i As Integer
33 i = s.Receive(MyBuffer)
34 If i 0 Then
35 Dim lstrRec As String
36 Dim j As Integer
37 For j = 0 To i - 1
38 TextBox1.Text += Chr(MyBuffer(j)) ","
39 Next
40 End If
41 End While
42 Catch ex As Exception
43 MsgBox(ex.Message.ToString)
44 End Try
45 End Sub
46
47 Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
48 SendMessage("192.168.0.61", TextBox2.Text)
49 End Sub