⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 CS型聊天室3.0版.rar
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ScktComp;

type
  TForm1 = class(TForm)
    ClientSocket1: TClientSocket;
    RichEdit1: TRichEdit;
    GroupBox1: TGroupBox;
    ListBox1: TListBox;
    Button1: TButton;
    GroupBox2: TGroupBox;
    Edit1: TEdit;
    Button2: TButton;
    StatusBar1: TStatusBar;
    procedure Edit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ClientSocket1Connect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
    if key=13 then //最好加本地IP
    begin
      if listbox1.itemindex>=0 then
       begin
       clientsocket1.Socket.SendText('交谈@'+'to'+listBox1.Items.strings[listbox1.itemindex]+'@  '+edit1.Text );
       edit1.text:='';
       end
      else
       showmessage('请选择交谈对象');

    end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  // clientsocket1.Socket.SendText('离开@');
   clientsocket1.Active :=false;
   clientsocket1.close;
end;

procedure TForm1.Button1Click(Sender: TObject);
var str1:string;
begin
    str1:=inputbox('建立连接','请输入IP','127.0.0.1');
    if trim(str1)<>'' then
    begin
       clientsocket1.Port:=5555;//server port
       clientsocket1.host:=str1;
       try
       clientsocket1.active:=true;
       str1:=inputbox('建立连接','请输入昵称','泡泡');
       clientsocket1.Socket.SendText('昵称@'+str1);
       except
       showmessage('连接失败');
       end;
    end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin

    close;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
    StatusBar1.SimpleText:='连接成功';
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);
var str1,name,str2,nei_rong:string;
    n,i:integer;
begin
    str1:=socket.ReceiveText;
    str2:=copy(str1,1,pos('@',str1)-1);
    if str2='添加用户' then
      begin
        listbox1.Clear;//清除已有名单
        str1:=copy(str1,pos('@',str1)+1,length(str1)-pos('@',str1));
        while str1<>'' do  //建立新名单
        begin
         name:=copy(str1,1,pos('@',str1)-1);//昵称^IP
         listbox1.Items.Add(copy(name,1,pos('^',name)-1));
         str1:=copy(str1,pos('@',str1)+1,length(str1)-pos('@',str1));
        end;
      RichEdit1.Lines.Add(name+'进入了');
      end;

    if str2='交谈' then   //交谈@xmj@hello
      begin
        str2:=copy(str1,pos('@',str1)+1,length(str1)-pos('@',str1));
        name:=copy(str2,1,pos('@',str2)-1);
        nei_rong:=copy(str2,pos('@',str2)+1,length(str2)-pos('@',str2));
        RichEdit1.Lines.Add(name+'对你说'+nei_rong);
      end;
    if str2='离开' then
      begin
         n:= pos('@',str1);
         name:=copy(str1,n+1,length(str1)-n);
         for i:=0 to listbox1.items.count-1 do
         begin
           if listbox1.items.strings[i]=name then
             listbox1.items.delete(i);
         end;
      end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -