VB.NET 怎么接收外部发来的字符串消息
时间: 2023-11-22 21:56:59 浏览: 218
在 VB.NET 中,可以使用 Windows API 函数来实现接收外部发来的字符串消息。
首先需要在代码中声明以下函数:
```vbnet
Public Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Integer
Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Boolean
```
其中 `RegisterWindowMessage` 用于注册一个自定义的消息,`PostMessage` 用于发送消息到指定的窗口。
接下来,在需要接收消息的窗口中,可以使用以下代码来接收消息:
```vbnet
Protected Overrides Sub WndProc(ByRef m As Message)
Const WM_COPYDATA As Integer = &H4A
If m.Msg = WM_COPYDATA Then
Dim data As CopyDataStruct = CType(m.GetLParam(GetType(CopyDataStruct)), CopyDataStruct)
Dim msg As String = New String(data.lpData)
' 处理接收到的消息
MessageBox.Show(msg)
End If
MyBase.WndProc(m)
End Sub
```
其中 `WM_COPYDATA` 是用来接收复制数据的消息,`CopyDataStruct` 是一个结构体,用来存储接收到的数据。
需要注意的是,接收消息的窗口需要调用 `RegisterWindowMessage` 函数来注册一个唯一的自定义消息,发送消息的程序需要使用该消息来向接收消息的窗口发送数据。
以下是一个简单的示例代码:
接收消息的窗口:
```vbnet
Imports System.Runtime.InteropServices
Public Class Form1
Public Structure CopyDataStruct
Public dwData As IntPtr
Public cbData As Integer
Public lpData As IntPtr
End Structure
Public Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Integer
Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Boolean
Private Const WM_COPYDATA As Integer = &H4A
Private msgId As Integer
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = msgId Then
Dim data As CopyDataStruct = CType(m.GetLParam(GetType(CopyDataStruct)), CopyDataStruct)
Dim msg As String = New String(data.lpData)
MessageBox.Show(msg)
End If
MyBase.WndProc(m)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
msgId = RegisterWindowMessage("MyMsg")
End Sub
End Class
```
发送消息的程序:
```vbnet
Imports System.Runtime.InteropServices
Public Class Form1
Public Structure CopyDataStruct
Public dwData As IntPtr
Public cbData As Integer
Public lpData As IntPtr
End Structure
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Public Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Integer
Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Boolean
Private Const WM_COPYDATA As Integer = &H4A
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim hWnd As IntPtr = FindWindow(Nothing, "Form1")
If hWnd <> IntPtr.Zero Then
Dim msgId As Integer = RegisterWindowMessage("MyMsg")
Dim data As New CopyDataStruct With {
.dwData = IntPtr.Zero,
.cbData = TextBox1.Text.Length,
.lpData = Marshal.StringToHGlobalAnsi(TextBox1.Text)
}
PostMessage(hWnd, WM_COPYDATA, msgId, data)
End If
End Sub
End Class
```
在上面的示例代码中,接收消息的窗口使用了 `MyMsg` 作为自定义消息的名称,发送消息的程序也使用了相同的名称。当发送消息的程序点击按钮时,会向接收消息的窗口发送一个包含文本框中的文本的消息,接收消息的窗口会弹出一个消息框显示接收到的文本。
阅读全文