假设我有两个用vb.NET,App1和App2编写的Windows窗体应用程序。 App1和2是两个完全分离的解决方案。
是否可以在App1中共享面板(或其他容器),以便App2可以控制此面板的内容? 通过控制内容,我的意思是添加将触发App2中事件的文本框和按钮。
答案 0 :(得分:1)
我们之前在我的企业中执行了这项任务,我们使用一些WinAPI在Windows之间进行通信:
您必须DLLImport以下user32.dll函数:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, _
ByVal lParam As IntPtr) As Integer
您可以使用App1中的SendMessage功能将消息发送到App2。
你可以监听覆盖App2应用程序中覆盖WndProc Sub的Window消息:
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_COPYDATA Then
'Read the message and perform some task,
'Create textboxes, modify controls, whatever
Else
MyBase.WndProc(m)
End If
End Sub
通过应用程序1 发送窗口消息的示例如下:
Dim hWnd As IntPtr
Dim mCopyData As COPYDATASTRUCT
hWnd = FindWindow(Nothing, App2WindowName)
Dim message As New System.Text.StringBuilder
If (hWnd <> 0) Then
message.Append(Mensaje)
Dim pCopyData As IntPtr = Marshal.AllocHGlobal(message.Length() + 40)
mCopyData.lpData = Marshal.StringToHGlobalAnsi(message.ToString)
mCopyData.cbData = message.Length
mCopyData.dwData = _messageID
Marshal.StructureToPtr(mCopyData, pCopyData, False)
SendMessage(hWnd, WM_COPYDATA, sender.Handle, pCopyData)
Marshal.FreeHGlobal(mCopyData.lpData)
Marshal.FreeHGlobal(pCopyData)
End If
您需要在代码中声明COPYDATASTRUCT以使用winapi:
<StructLayout(LayoutKind.Sequential)> _
Private Structure COPYDATASTRUCT
Public dwData As IntPtr
Public cbData As Integer
Public lpData As IntPtr
End Structure
和WM_COPYDATE消息整数:
Const WM_COPYDATA As Integer = 74