请问如何把一个程序的一个结构数组复制到剪切板,在另一个应该程序再还原为结构数组?
请问如何把一个程序的一个结构数组复制到剪切板,在另一个应该程序再还原为结构数组?这两个程序都是我做的,而且由于其它方面的原因只能用剪切板来实现复制粘贴的功能。
假设结构如下:
Private Type student
age As Integer
name As String
End Type
google了很多资料,用到的API大概有下面几个,代码也都不是VB6.0的,没法改
Public Declare Function RegisterClipboardFormat Lib "user32" Alias "RegisterClipboardFormatA" (ByVal lpString As String) As Long
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function EmptyClipboard Lib "user32" () As Long
Public Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Public Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Public Declare Function CloseClipboard Lib "user32" () As Long
Public Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Public Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Public Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Public Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
如果结构数组不行,给一个只传递一个单一的结构对像的示例也可以,谢谢!!
我认为还是不通过剪切板的好。可以通过两个进程间的通信来实现。比如:
1、通过API函数:PostMessage或SendMessage来实现两个进程之间的消息互通。
2、实用DDE。
3、串口通信,这个方法比较偏。需要你的计算机有两个串口,将两个串口的2、3交叉连线。一个进程打开一个端口。实现通信。 '程序A
Private Type student
age As Integer
name As String
End Type
Private Sub mnuCopy_Click()
Dim s As student
Dim c As String
s.age = 1
s.name = "Your name"
c = "yourfeature" & CStr(s.age) & ";" & s.name '加入yourfeature作为你拷贝的数据的特征,按age,name的顺序组织拷贝的数据
Clipboard.SetText c
End Sub
’程序B
Private Type student
age As Integer
name As String
End Type
Private Sub mnuPaste_Click()
Dim s As student
Dim c As String
Dim x() As String
c = Clipboard.GetText
x = Split(c)
If x(0) = "yourfeature" Then '检查裁剪版的数据是否包含指定的特征
s.age = CInt(x(1))
s.name = x(2)
End If
End Sub
x = Split(c)
==>
x = Split(c,";")
c = "yourfeature" & CStr(s.age) & ";" & s.name
==>
c = "yourfeature;" & CStr(s.age) & ";" & s.name 非要用剪切板话,可以先把结构数据组的数据组织成文本,然后用vb6的Clipboard 对象,把文本装入剪切板,再利用Clipboard 对象取出还原,可以不需要使用API。
Private Type student
age As Integer
name As String
End Type
不要用string,用定长字符串。
直接COPYMEMORY不行啊? 要用 SetClipboardData 与 GetClipboardData 这一对函数解决你的问题。
补充:VB , API