CltMain.pas
资源名称:P2P.rar [点击查看]
上传用户:zhanghw123
上传日期:2021-11-15
资源大小:312k
文件大小:3k
源码类别:
P2P编程
开发平台:
Delphi
- Unit CltMain;
- Interface
- Uses
- Windows, SysUtils, Messages, Classes, Controls, Forms,
- Buttons, ComCtrls, ExtCtrls, StdCtrls, Graphics, Dialogs, ToolWin,
- P2pClient, P2pCommon, P2pConst, ImgList;
- Type
- TFrmMain = Class(TForm)
- UserList: TListView;
- spl1: TSplitter;
- pnl1: TPanel;
- pnl2: TPanel;
- spl2: TSplitter;
- pnl3: TPanel;
- TxtSendMsg: TRichEdit;
- CmdSend: TSpeedButton;
- Timer: TTimer;
- FD: TFontDialog;
- ImageList: TImageList;
- Pnl4: TPanel;
- ToolBar: TToolBar;
- CmdFont: TToolButton;
- Pnl5: TPanel;
- TxtLog: TRichEdit;
- Splitter1: TSplitter;
- TxtMsg: TRichEdit;
- Procedure FormCreate(Sender: TObject);
- Procedure FormDestroy(Sender: TObject);
- Procedure UserListClick(Sender: TObject);
- Procedure TimerTimer(Sender: TObject);
- Procedure FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
- Procedure CmdSendClick(Sender: TObject);
- Procedure TxtSendMsgKeyDown(Sender: TObject; Var Key: Word;
- Shift: TShiftState);
- Procedure CmdFontClick(Sender: TObject);
- Private
- P2p: TP2pClient;
- CurSelUser: TP2pUser;
- Public
- { Public declarations }
- End;
- Var
- FrmMain: TFrmMain;
- Implementation
- {$R *.dfm}
- Procedure TFrmMain.FormCreate(Sender: TObject);
- Begin
- P2p := TP2pClient.Create(TxtLog);
- P2p.Socket.RemoteHost := '127.0.0.1';
- P2p.Socket.RemotePort := 9890;
- P2p.Socket.LocalPort := 9089;
- P2p.LoginServer('测试用户');
- CurSelUser := Nil;
- End;
- Procedure TFrmMain.FormDestroy(Sender: TObject);
- Begin
- P2p.Free;
- End;
- Procedure TFrmMain.UserListClick(Sender: TObject);
- Begin
- If UserList.Selected = Nil Then
- CurSelUser := Nil Else
- CurSelUser := TP2pUser(UserList.Selected.Data);
- End;
- Procedure TFrmMain.TimerTimer(Sender: TObject);
- Begin
- P2p.Living;
- End;
- Procedure TFrmMain.FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
- Begin
- P2p.LoginOutServer;
- End;
- Procedure TFrmMain.CmdSendClick(Sender: TObject);
- Begin
- If (TxtSendMsg.Text <> '') And (CurSelUser <> Nil) Then
- Begin
- If Length(TxtSendMsg.Text) > 1024 Then
- Begin
- MessageBox(Handle, '输入的内容太长了。', '错误', MB_ICONHAND);
- TxtSendMsg.SetFocus;
- Exit;
- End;
- P2p.SendChat(CurSelUser, TxtSendMsg);
- TxtMsg.SelAttributes.Color := clGreen;
- TxtMsg.Lines.Add(Format('[%s]你对[%s]说:', [FormatDateTime('YYYY-MM-DD HH:NN:SS', Now), CurSelUser.Address]));
- TxtMsg.SelAttributes := TxtSendMsg.SelAttributes;
- TxtMsg.Lines.Add(Format('%s' + #13#10, [TxtSendMsg.Text]));
- SendMessage(TxtMsg.Handle, EM_SCROLLCARET, 0, 0);
- TxtSendMsg.Clear;
- End Else MessageBox(Handle, '请选择一个聊天用户。', '错误', MB_ICONHAND);
- End;
- Procedure TFrmMain.TxtSendMsgKeyDown(Sender: TObject; Var Key: Word;
- Shift: TShiftState);
- Begin
- If (ssCtrl In Shift) And (Key = VK_RETURN) Then
- Begin
- CmdSend.Click;
- Key := 0;
- End;
- End;
- Procedure TFrmMain.CmdFontClick(Sender: TObject);
- Begin
- If Not FD.Execute Then Exit;
- SetRichEditFont(TxtSendMsg, FD.Font);
- TxtSendMsg.SetFocus;
- End;
- End.