有阿.原码如下:
unit Unt_LiaoLiao;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Psock, NMHttp, NMUDP,Inifiles;
type
TFrm_Liaoliao = class(TForm)
Panel1: TPanel;
NMUDP1: TNMUDP;
Edit1: TEdit;
Splitter1: TSplitter;
Panel3: TPanel;
ListBox1: TListBox;
Panel2: TPanel;
Memo1: TMemo;
Label1: TLabel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Memo2: TMemo;
procedure FormShow(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure Memo1KeyPress(Sender: TObject; var Key: Char);
procedure Button4Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormDestroy(Sender: TObject);
procedure ListBox1DblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Frm_Liaoliao: TFrm_Liaoliao;
ComputerName:Array[0..127]of char;
FileName:TInifile;
//该函数获取所有局域网(网上邻居)里的所有计算机
Function NetGetUsers(GroupName: string; var List: TListBox): Boolean;
//该函数获取所有局域网(网上邻居)里的所有工作组
Function GetGroupList( var List : TListBox ) : Boolean;
implementation
{$R *.dfm}
Function GetGroupList( var List : TListBox ) : Boolean;
type
TNetResourceArray = ^TNetResource;//网络类型的数组
Var
NetResource: TNetResource;
Buf: Pointer;
Count,BufSize,Res: DWORD;
lphEnum: THandle;
p: TNetResourceArray;
i,j: SmallInt;
NetworkTypeList: TList;
Begin
Result := False;
NetworkTypeList := TList.Create;
List.Clear;
//获取整个网络中的文件资源的句柄,lphEnum为返回名柄
Res := WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER, Nil,lphEnum);
if Res <> NO_ERROR then exit;//Raise Exception(Res);//执行失败
//获取整个网络中的网络类型信息
Count := $FFFFFFFF;//不限资源数目
BufSize := 8192;//缓冲区大小设置为8K
GetMem(Buf, BufSize);//申请内存,用于获取工作组信息
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
//资源列举完毕 //执行失败
if ( Res = ERROR_NO_MORE_ITEMS ) or (Res <> NO_ERROR ) then Exit;
P := TNetResourceArray(Buf);
for i := 0 to Count - 1 do//记录各个网络类型的信息
begin
NetworkTypeList.Add(p);
Inc(P);
end;
Res := WNetCloseEnum(lphEnum);//关闭一次列举
if Res <> NO_ERROR then exit;
for j := 0 to NetworkTypeList.Count-1 do //列出各个网络类型中的所有工作组名称
begin//列出一个网络类型中的所有工作组名称
NetResource := TNetResource(NetworkTypeList.Items[J]^);//网络类型信息
//获取某个网络类型的文件资源的句柄,NetResource为网络类型信息,lphEnum为返回名柄
Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);
if Res <> NO_ERROR then break;//执行失败
while true do//列举一个网络类型的所有工作组的信息
begin
Count := $FFFFFFFF;//不限资源数目
BufSize := 8192;//缓冲区大小设置为8K
GetMem(Buf, BufSize);//申请内存,用于获取工作组信息
//获取一个网络类型的文件资源信息,
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
//资源列举完毕 //执行失败
if ( Res = ERROR_NO_MORE_ITEMS ) or (Res <> NO_ERROR) then break;
P := TNetResourceArray(Buf);
for i := 0 to Count - 1 do//列举各个工作组的信息
begin
List.Items.Add( StrPAS( P^.lpRemoteName ));//取得一个工作组的名称
Inc(P);
end;
end;
Res := WNetCloseEnum(lphEnum);//关闭一次列举
if Res <> NO_ERROR then break;//执行失败
end;
Result := True;
FreeMem(Buf);
NetworkTypeList.Destroy;
End;
Function NetGetUsers(GroupName: string; var List: TListBox): Boolean;
type
TNetResourceArray = ^TNetResource;//网络类型的数组
Var
i: Integer;
Buf: Pointer;
Temp: TNetResourceArray;
lphEnum: THandle;
NetResource: TNetResource;
Count,BufSize,Res: DWord;
begin
Result := False;
List.Clear;
FillChar(NetResource, SizeOf(NetResource), 0);//初始化网络层次信息
NetResource.lpRemoteName := @GroupName[1];//指定工作组名称
NetResource.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER;//类型为服务器(工作组)
NetResource.dwUsage := RESOURCEUSAGE_CONTAINER;
NetResource.dwScope := RESOURCETYPE_DISK;//列举文件资源信息
//获取指定工作组的网络资源句柄
Res := WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);
if Res <> NO_ERROR then Exit; //执行失败
while True do//列举指定工作组的网络资源
begin
Count := $FFFFFFFF;//不限资源数目
BufSize := 8192;//缓冲区大小设置为8K
GetMem(Buf, BufSize);//申请内存,用于获取工作组信息
//获取计算机名称
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
if Res = ERROR_NO_MORE_ITEMS then break;//资源列举完毕
if (Res <> NO_ERROR) then Exit;//执行失败
Temp := TNetResourceArray(Buf);
for i := 0 to Count - 1 do//列举工作组的计算机名称
begin
//获取工作组的计算机名称,+2表示删除"//",如//wangfajun=>wangfajun
List.Items.Add(Temp^.lpRemoteName + 2);
inc(Temp);
end;
end;
Res := WNetCloseEnum(lphEnum);//关闭一次列举
if Res <> NO_ERROR then exit;//执行失败
Result := True;
FreeMem(Buf);
end;
procedure TFrm_Liaoliao.FormShow(Sender: TObject);
Var Sz
word;
begin
Sz:=Sizeof(ComputerName);
getComputerName(ComputerName,Sz);//得到本机的标识符
ListBox1.Items.Clear;
ListBox1.Items.Add('大家');//在本机中增加大家
ListBox1.Items.Add(ComputerName);//增加本机名称
ListBox1.ItemIndex:=0;
end;
procedure TFrm_Liaoliao.Button1Click(Sender: TObject);
Var
MyStream:TMemoryStream;
TemStr:String;
I:Integer;
begin
Memo1.Font.Color:=ClBlue;
if Not Memo1.Visible then
begin
Memo1.Visible:=True;;
Memo2.Visible:=False;
Button4.Caption:='聊天记录';
end;
if ListBox1.ItemIndex<0 then
begin
Application.MessageBox('请选择要发送的计算机名','发送提示',Mb_OK+MB_IconInformation);
Exit;
end;
if Trim(Edit1.Text)='' then
begin
Application.MessageBox('请输入你要发送的信息','发送提示',Mb_OK+MB_IconInformation);
Edit1.SetFocus;
Exit;
end;
NMUDP1.ReportLevel:=Status_Basic;
NMUDp1.RemotePort:=8888;//端口:8888,也可以自定义,但必须和LocalPort相一致
if ListBox1.Items[ListBox1.ItemIndex]=ComPuterName then
Edit1.Text:=ComPuterName+' 自言自语道: '
+Datetimetostr(Now)+Chr(13)+Chr(10)+Edit1.Text //如果和自己说话
else
Edit1.Text:=ComputerName+' 对 '+ListBox1.Items[ListBox1.ItemIndex]+' 说: '
+Datetimetostr(Now)+Chr(13)+Chr(10)+Edit1.Text;
{Edit1.Text:='你 对 '+ListBox1.Items[ListBox1.ItemIndex]+' 说: '
+Datetimetostr(Now)+Chr(13)+Chr(10)+Edit1.Text; }
TemStr:=Trim(Edit1.Text);
MyStream:=TMemoryStream.Create;
Try
MyStream.Write(TemStr[1],Length(Edit1.Text));
if ListBox1.ItemIndex=0 then //如果选中大家就给所以人发信息
begin
For I:=1 to ListBox1.Items.Count-1 do
begin
NMUdp1.RemoteHost:=ListBox1.Items
;//远程主机的名称或地址
NMUDP1.SendStream(MyStream);//发送信息
end;
end
else //如果私聊
begin
NMUDP1.RemoteHost:=ListBox1.Items[ListBox1.itemIndex];
NMUDP1.SendStream(MyStream); //仅对选中的主机发送信息
end;
finally
MyStream.Free;
end;
if (Trim(Listbox1.Items[ListBOx1.itemIndex])<>ComputerName) And
(ListBox1.ItemIndex<>0) then Memo1.Lines.Add(Edit1.Text);
if Trim(Edit1.Text)<>'' then Edit1.Clear;
Edit1.SetFocus;
//Memo1.Font.Color:=Clred;
end;
procedure TFrm_Liaoliao.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
Var MyStream:TMemoryStream;
TemStr:String;
begin
MyStream:=TMemoryStream.Create;
Try
NMUDP1.ReadStream(MyStream);
Setlength(TemStr,NumBerbytes);
MyStream.Read(TemStr[1],Numberbytes);
Memo1.Lines.Add(TemStr);//显示对话的内容
Finally
MyStream.Free;
end;
end;
procedure TFrm_Liaoliao.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if (key<>chr(13)) or (Trim(Edit1.Text)='') then Exit;
Button1.Click;
end;
procedure TFrm_Liaoliao.Button2Click(Sender: TObject);
VAr InputStr:String;
begin
InPutStr:=INputBox('增加人员','IP地址或计算机名称','');
if InPUtStr<>'' then LIstBox1.Items.Add(InPutStr);
ListBox1.ItemIndex:=0;
end;
procedure TFrm_Liaoliao.Button3Click(Sender: TObject);
begin
if LIstBox1.ItemIndex=0 then
Application.MessageBox('所有人不能删除','删除人员',Mb_OK+MB_IconINformation)
else
ListBox1.Items.Delete(ListBox1.ItemIndex);
end;
procedure TFrm_Liaoliao.ListBox1Click(Sender: TObject);
begin
Frm_Liaoliao.Caption:='聊天工具 '+'你 对 '+ListBox1.Items[ListBox1.itemIndex]+' 说:';
end;
procedure TFrm_Liaoliao.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
key:=Chr(0);
end;
procedure TFrm_Liaoliao.Button4Click(Sender: TObject);
VAr Str_Record:String;
begin
//查看聊天记录,只是上一天的
if Button4.Caption='聊天记录' then
begin
Str_Record:=ExtractFilePath(ParamStr(0))+'MyRecord.ini';
FileName:=TIniFile.Create(Str_Record);
if FileExists(Str_Record) then
begin
Memo1.Visible:=False;
if Not Memo2.Visible then
Memo2.Visible:=True;
Memo2.Lines.Clear;
//Memo2.Text:=FileName.ReadString('Liaorecord','MyRecord','');
Memo2.Lines.LoadFromFile(Str_Record);
end;
Button4.Caption:='返回';
end else
begin
if not Memo1.Visible then
Memo1.Visible:=True;
Memo2.Visible:=False;
Button4.Caption:='聊天记录';
end;
end;
procedure TFrm_Liaoliao.FormClose(Sender: TObject;
var Action: TCloseAction);
VAr Str_Record:String;
begin
Str_Record:=ExtractFilePath(ParamStr(0))+'MyRecord.ini';
FileName:=TIniFile.Create(Str_Record);
if FileExists(Str_Record) then
FileName.EraseSection('Liaorecord');//删除以前的记录
//写入这次聊天的记录
//FileName.WriteString('Liaorecord','MyRecord',Memo1.Lines.Text);
Memo1.Lines.SaveToFile(Str_Record);
end;
procedure TFrm_Liaoliao.FormDestroy(Sender: TObject);
begin
FileName.Free;
end;
procedure TFrm_Liaoliao.ListBox1DblClick(Sender: TObject);
Var GroudWork:String;
begin
GroudWork:=Inputbox('工作组','请输入工作组名','');
if GroudWork<>'' then
begin
if not NetGetUsers(GroudWork,ListBox1) then //调用函数
begin
Application.MessageBox('该工作组不存在','操作提示',Mb_OK+Mb_IconInformation);
Exit;
end;
ListBox1.Items.Add('大家');//在本机中增加大家
end;
end;
end.