【发布时间】:2019-08-05 21:14:44
【问题描述】:
几周前一直在阅读 Indy 的一些文档。使用 Indy,我已经能够为简单程序实现客户端和服务器程序,因此我想测试自己的 VNC 程序,比如 teamviewer,开始使用 Indy,我确实喜欢这个主题而不是使用 Raw winsock,Indy 确实帮助了我,但是我确实有一个问题想换掉。 我正在编写类似于我自己的 teamviewer 的代码,它需要客户端和服务器现在我想从服务器获取屏幕截图并发送到客户端
在客户端我做了一些看起来像这样的连接
procedure TForm1.FormCreate(Sender: TObject);
begin
IdTCPServer1:=TIdTCPServer.Create(nil);
IdTCPServer1.DefaultPort:=50000;
IdTCPServer1.OnExecute:=IdTCPServer1Execute;
IdTCPServer1.Active:=true;
end;
现在 onExecute 看起来像这样来抓取屏幕截图并发送给 Indy (Winsock)
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
mem_dc : HDC;
bmp : TBitmap;
_bm : BITMAP;
w, h : Integer;
dimensions : Integer;
begin
bmp := TBitmap.Create;
GetObject (bm, sizeof (BITMAP), @_bm);
w := _bm.bmWidth;
h := _bm.bmHeight;
bmp.Height := h;
bmp.Width := w;
mem_dc := CreateCompatibleDC (bmp.Canvas.Handle);
SelectObject (mem_dc, bm);
BitBlt (bmp.Canvas.Handle,0, 0, w, h, mem_dc, 0, 0, SRCCOPY);
Canvas.Draw (0, 0, bmp);
DeleteObject (mem_dc);
bmp.Free;
//Send Dimensions vis Indy here
dimensions := w * h / 4;
while True do
begin
AContext.Connection.IOHandler.WriteLn(dimensions);
// and do same for Pixels
end;
end.
由于我是这样的新手,我只是获取宽度和高度并发送还是我必须使用类似这样的东西单独发送它们:
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
mem_dc : HDC;
bmp : TBitmap;
_bm : BITMAP;
w, h : Integer;
begin
bmp := TBitmap.Create;
GetObject (bm, sizeof (BITMAP), @_bm);
w := _bm.bmWidth;
h := _bm.bmHeight;
bmp.Height := h;
bmp.Width := w;
mem_dc := CreateCompatibleDC (bmp.Canvas.Handle);
SelectObject (mem_dc, bm);
BitBlt (bmp.Canvas.Handle,0, 0, w, h, mem_dc, 0, 0, SRCCOPY);
Canvas.Draw (0, 0, bmp);
DeleteObject (mem_dc);
bmp.Free;
//Send Dimensions vis Indy here
dimensions := w * h;
while True do
begin
AContext.Connection.IOHandler.WriteLn(w);
AContext.Connection.IOHandler.WriteLn(h);
// Then add the same for Pixels
end;
end.
【问题讨论】: