Sever Side Chat Application with Borland Delphi/Indy - Handling Commands: Listnames, all, takeshot
(Page 4 of 5 )
Command: Listnames
When the server receives this command it collects all the nick names of the connected clients and sends it off to the requesting client. Here’s the procedure that handles this:
procedure TMyContext.SendNicks;
var
List: TList;
Context: TMyContext;
I: Integer;
begin
List := FContextList.LockList;
try
if List.Count > 1 then
begin
//Connection.IOHandler.WriteLn('Currently
Connected:');
for I := 0 to List.Count-1 do
begin
Context := TMyContext(List[I]);
if Context <> Self then
Connection.IOHandler.WriteLn('list@'+ Context.Nick);
end;
end else
Connection.IOHandler.WriteLn('list@No-one else is
connected');
finally
FContextList.UnlockList;
end;
end;
Command: all
When the server receives this command it sends the message to all connected clients. Here’s how:
procedure TMycontext.BroadcastMsgAll(const ANick: String; const
bmsg: String);
var
List: TList;
Context: TMyContext;
I: Integer;
begin
List := FContextList.LockList;
try
for I := 0 to List.Count-1 do
begin
Context := TMyContext(List[I]);
if Context <> Self then try
Context.Connection.IOHandler.WriteLn(ANick +
'> ' + bmsg);
except end;
end;
finally
FContextList.UnlockList;
end;
end;
Command: takeshot:
Next, I needed a procedure to convert bitmap images to jpeg format, because Delphi only works in the bitmap format. Also, when you send a picture over the Internet it is always best to use a format that does not take up a lot of space and that is Internet ‘friendly’.
Procedure BmpToJpg(const Filename: String; Quality: TJPEGQualityRange=100);
var Bmp: TBitmap;
Jpg: TJpegImage;
begin
Bmp:=TBitmap.Create;
Jpg:=TJpegImage.Create;
try
Bmp.LoadFromFile(Filename);
Jpg.CompressionQuality:=Quality;
Jpg.Assign(Bmp);
Jpg.SaveToFile(ChangeFileExt(Filename, '.jpg' ));
deletefile(filename);
finally
Jpg.Free;
Bmp.Free;
end;
end;
Here’s the procedure that actually takes the screenshot:
procedure screenshot;
var DCDesk: HDC; // hDC of Desktop
bmp: TBitmap;
begin
{Create a bitmap}
bmp := TBitmap.Create;
{Set a bitmap sizes}
bmp.Height := Screen.Height;
bmp.Width := Screen.Width;
{Get a desktop DC handle - handle of a display device context}
DCDesk := GetWindowDC(GetDesktopWindow);
{Copy to any canvas, here canvas of an image}
BitBlt(bmp.Canvas.Handle, 0, 0, Screen.Width, Screen.Height,
DCDesk, 0, 0, SRCCOPY);
{Save the bitmap}
bmp.SaveToFile('scrnshot.bmp');
BMPtoJPG('scrnshot.bmp') ;
{Release desktop DC handle}
ReleaseDC(GetDesktopWindow, DCDesk);
{Release a bitmap}
bmp.Free;
end;
As the name suggests, the procedure takes a screenshot and saves it in a file called 'scrnshot.bmp'. This file will then be converted to jpeg format, before it is sent off to the requesting client. So, it is essential that this procedure is called before the bmptojpg procedure.
Next: Handling commands: name, file >>
More Delphi-Kylix Articles
More By Jacques Noah