VB实现远程共享显示及声音
ImgEdit1.ClearDisplay
ImgEdit1.DisplayBlankImage Screen.Width / _
Screen.TwipsPerPixelX, Screen.Height / _
Screen.TwipsPerPixelY, , , 6
ImgEdit1.ClipboardPaste ' 从剪贴板粘贴图像
ImgEdit1.BurnInAnnotations 0, 2
ImgEdit1.SaveAs FileName, 1, 6, 6, 256 ' 另存图像。参数说明如下:
' “FileName”:文件名
' 参数“1”:TIFF 型文件;
' 第一个“6”:RGB24类型;
' 第二个“6”:JPEG压缩类型
' 参数“256”:最大压缩比
Clipboard.Clear
tcpServer.SendData "PS" ' 发送“图像文件就绪”信息
End If
Case "Get Picture"
If Dir$(FileName) <> "" Then
FileID = FreeFile
Open FileName For Binary As #FileID ' 打开文件并发送第一块数据
FileLen = LOF(FileID)
ReDim Buf(1 To BlockSize) As Byte
Get #FileID, , Buf
tcpServer.SendData Buf
Cur_Pos = BlockSize
End If
Case "Next Block"
If Cur_Pos = FileLen Then
tcpServer.SendData "EF" ' 文件传送完毕后,发送“完成”信息
Close FileID
Exit Sub
End If
j = Cur_Pos + BlockSize
If j > FileLen Then
j = FileLen - Cur_Pos
Else
j = BlockSize
End If
ReDim Buf(1 To j) As Byte ' 动态确定数组大小
Get #FileID, , Buf
tcpServer.SendData Buf ' 发送后续包
Cur_Pos = Cur_Pos + j
End Select
End Sub 2. 客户端程序(frmClient.frm)
在窗体上建六个控件:一个名为 tcpClient的 Winsock控件用于通讯;一个名为txtIP 的 TextBox控件用于填写服务器的IP地址;一个名为ImgEdit1的 ImgEdit控件用于显示服务器传来的图像;三个CommandButton控件( cmdConnect、cmdGet_Pic和cmdDisconnect) 分别用于执行连接、取回图像和断开连接(见图二)。
客户端源代码如下:
' frmClient.frm
Option Explicit
Const FileName = "C:\sys1.tmp"
Private Sub cmdConnect_Click()
If tcpClient.State <> sckClosed Then tcpClient.Close
tcpClient.RemoteHost = txtIP.Text
tcpClient.RemotePort = 1001
tcpClient.Connect ' 进行连接
End Sub
Private Sub cmdDisconnect_Click()
tcpClient.SendData "Close" ' 断开连接
cmdConnect.Enabled = True
cmdGet_Pic.Enabled = False
cmdDisconnect.Enabled = False
End Sub
Private Sub cmdGet_Pic_Click()
tcpClient.SendData "Save Picture" ' 请求图像返回
frmClient.MousePointer = 11
End Sub
Private Sub Form_Resize() ' 使 ImgEdit1 的大小随窗体的变化而变化
ImgEdit1.Height = frmClient.Height - 825
ImgEdit1.Width = frmClient.Width - 225
End Sub
Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
Static FileID As Integer, FileLen As Long
Dim Buf() As Byte
Dim j As Integer
ReDim Buf(bytesTotal) As Byte ' 根据到达数据的字节数确定接收数组的大小
tcpClient.GetData Buf
' 收到连接完成的“握手”信息
If bytesTotal = 2 And Chr(Buf(0)) = "S" And Chr(Buf(1)) = "H" Then
cmdConnect.Enabled = False
cmdGet_Pic.Enabled = True
cmdDisconnect.Enabled = True
Exit Sub
End If
' 收到图像就绪的信息
If bytesTotal = 2 And Chr(Buf(0)) = "P" And Chr(Buf(1)) = "S" Then
If Dir$(FileName) <> "" Then Kill FileName
FileID = FreeFile
Open FileName For Binary As #FileID ' 打开文件,准备存储图像
FileLen = 0
tcpClient.SendData "Get Picture"
Exit Sub
End If
' 收到图像发送完毕的信息
If bytesTotal = 2 And Chr(Buf(0)) = "E" And Chr(Buf(1)) = "F" Then
Close #FileID ' 关闭文件
j = DoEvents()
vb社区