您的位置:首页 > 编程语言 > Delphi

电子白板简单实现delphi

2009-12-28 11:10 253 查看
电子白板简单实现

将画图存储成一个一个的点,点表示成记录,用文件流的方法存储到文本里,将文本读进文件流,用socket发送,socket接收后再存储成文本,然后再将文本用文件流读出,画到image上

1、在image上画线
image1.Canvas.Pen.Color:=clblue;
在image的mousedown事件中写
image1.Canvas.MoveTo(X,y);
在image的mousemove事件中写
image1.Canvas.lineTo(X,y);
2、在image上实现橡皮功能
with image1.Canvas do
begin
Pen.Mode := pmCopy;
Brush.Style:= bsClear;
Brush.Color:=clWhite;
FillRect(rect((x-4),(y-4),(x+4),(y+4)));//橡皮
end;
3、如果想实现电子白板的简单传送,我用的办法是将画的点按记录的形式
存入文本,记录为
type
TRec = record {定义一个记录}
sign:word;//标记,mousedown画=1,mousemove画=2,mousedown擦=3,mousemove擦=4,
x: word; //x轴标点
y: word; //y轴标点
end;

定义记录:
var rec: TRec;
记录调用:
rec.sign:=1;
rec.x:=x;
rec.y:=y;
msw.Write(rec, SizeOf(rec));//msw,msr:tmemorystream; //内存流用于读、写画板
msw.SaveToFile('c:/temp/path.txt');//文件流存储文件
4、存储成文本后用tclientsocket/tserversocket以文件流的形式传送
(1)、发送的时候先发送文件大小
Msw:=tmemorystream.Create;//文件流
MyStreamw:=tmemorystream.Create;//文件流
MyStreamw.LoadFromFile('c:/temp/path.txt');//文件流调用文件
MyStreamw.Position := 0;{注意:必须添加此句}
s1 :='size'+inttostr(MyStreamw.size);{流的大小}
if ServerSocket1.Active then
begin
if listbox1.Count<=0 then
//.ItemIndex=-1 then
begin
showmessage('没有发送方');
exit;
end;
for i:=0 to ServerSocket1.Socket.activeconnections-1 do
begin
// if ServerSocket1.Socket.Connections[i].RemoteAddress=trim(listbox1.Items[listbox1.itemindex]) then
ServerSocket1.Socket.Connections[i].SendText(s1);//发送文件流大小
end;
end;
(2)、然后接收方获取文件大小并向发送方发送ready,准备好接收,发送方接到ready,
然后发送文件流给接收方
MyStreamw.Position := 0;
if ServerSocket1.Socket.Connections[i].SendStream(MyStreamw)=false then
ServerSocket1.Socket.Connections[i].SendStream(MyStreamw); {将流发送出去}
(3)、接收方接收到数据后根据数据流的大小接收,然后存储到另一个路径的文本里,
ServerSocket每次只能接收8k的数据,如果大于8k,ServerSocket会多次触发read事件,每次接收的
最多只有8k的数据,如果数据小于8k,则直接接收并存储到文本
MyReceviceLength := socket.ReceiveLength; {读出包长度}
remainsize:=MySize-MyReceviceLength;
StatusBar1.SimpleText := '正在接收数据,数据大小为:' + inttostr(MySize);
Socket.ReceiveBuf(MyBuffer, MyReceviceLength); {接收数据包并读入缓冲区内}//MyBuffer:array[0..100000] of byte;
MyStreamr.Write(MyBuffer, MyReceviceLength); {将数据写入流中}
if MyStreamr.Size >= MySize then {如果流长度大于需接收的字节数,则接收完毕}
begin
ServerSocket1.Socket.Connections[0].SendText('ok');
MyStreamr.Position := 0;
try
MyStreamr.SaveToFile('d:/path.txt');
finally {以下为清除工作 }
MySize := 0;
//button4.Click;
paintline.create(image1);//调用线程画图,线程为取D盘文本,然后画出来

end;
end;
如果大于8k,则接收之前,先用文件流取得D盘文件的数据,然后移动到文件流的末尾,再继续接收数据
MyStreamr.LoadFromFile('d:/path.txt');
MyStreamr.Seek(0,sofromend);
StatusBar1.SimpleText := '正在接收数据,数据大小为:' + inttostr(MySize);
Socket.ReceiveBuf(MyBuffer, MySize); {接收数据包并读入缓冲区内}
MyStreamr.Write(MyBuffer, MySize); {将数据写入流中}
剩余大小为每次减去这次发送的大小。
接收完毕后向发送方发送‘ok’
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: