//我以前用过的一个
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
NMUDP, Buttons, StdCtrls, ExtCtrls, WinSock, ComCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Memo1: TMemo;
NMUDP1: TNMUDP;
Panel1: TPanel;
Button1: TButton;
Button2: TButton;
BitBtn1: TBitBtn;
RichEdit1: TRichEdit;
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
ComputerName: array[0..127] of Char;
colornum:integer;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
var
sz: dword;
begin
sz := SizeOf(Computername);
GetComputerName(Computername,sz);//得到本机的标识
ListBox1.Items.Clear;
ListBox1.Items.Add('大家');//在网友清单中,增加”大家”和
ListBox1.Items.Add(ComputerName);//本机名称
ListBox1.ItemIndex:=0;
ListBox1.Hint:=' 好友列表'+chr(13)+'在这里选择要发送的对象';
Memo1.Hint:=' 信息窗口'+chr(13)+'这里是聊天记录';
colornum:=0;//用于记录发送聊天语句次数,从而确定该次的颜色
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
MyStream: TMemoryStream;
TmpStr: String;
i:integer;
begin
if RichEdit1.Text<>''
then //file://如果所说的内容不为空则发送。
begin
NMUDP1.ReportLevel := Status_Basic;
NMUDP1.RemotePort :=8888;//端口为:8888,可以自己定义,但必须与LocalPort相一致。
if ListBox1.Items[ListBox1.ItemIndex]=ComputerName
then RichEdit1.Text:=ComputerName+'自言自语道:'+RichEdit1.Text //file://如果和自己对话.
Else RichEdit1.Text:=ComputerName+'对'+ListBox1.Items[listbox1.itemindex]+'说:'+RichEdit1.Text;
TmpStr :=RichEdit1.text;
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1],Length(RichEdit1.Text));
if ListBox1.ItemIndex=0
then
begin
for i:=1 to ListBox1.Items.Count-1 do
begin //file://如果选择”大家”,则对所有的网友发送信息
NMUDP1.RemoteHost :=ListBox1.Items;//远程主机的名称或地址.
NMUDP1.SendStream(MyStream);//发送信息.
End;
end
else
begin //如果私聊
NMUDP1.RemoteHost :=ListBox1.Items[ListBox1.itemindex]; //file://仅对所选中的网友.
NMUDP1.SendStream(MyStream);
End;
finally
MyStream.Free;
end;
RichEdit1.Text:='';
RichEdit1.SetFocus;
end
else RichEdit1.SetFocus;
end;
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
MyStream: TMemoryStream;
TmpStr: String;
begin
MyStream := TMemoryStream.Create;
colornum:=colornum+1;
case (colornum mod 7) of
0: memo1.Font.Color:=clBlack;
1: memo1.Font.Color:=clGreen;
2: memo1.Font.Color:=clNavy;
3: memo1.Font.Color:=clTeal;
4: memo1.Font.Color:=clGray;
5: memo1.Font.Color:=clRed;
6: memo1.Font.Color:=clBlue;
7: memo1.Font.Color:=clFuchsia;
8: memo1.Font.Color:=clBackground;
9: memo1.Font.Color:=clHighlight;
end;
try
NMUDP1.ReadStream(MyStream);
SetLength(TmpStr,NumberBytes);
MyStream.Read(TmpStr[1],NumberBytes);
Memo1.Lines.Add(TmpStr);// file://显示对话的内容.
finally
MyStream.Free;
end
end;
procedure TForm1.Button1Click(Sender: TObject);
var
inputString:String;
begin
//file://增加网友,输入的可以是IP地址或计算机名称。
InputString:=InputBox('增加人员','IP地址或计算机名 ','');
if Inputstring<>'' then ListBox1.Items.Add(Inputstring);
ListBox1.ItemIndex:=0;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//file://删除当前选中的网友,但”大家”不能被删除.
if ListBox1.ItemIndex<>0 then
ListBox1.Items.Delete(ListBox1.ItemIndex);
end;
end.