unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Psock, NMMSG, NMSTRM,ShellAPI,ComCtrls;

  const 
  WM_ICONNOTIFY = WM_USER + 1; 


type
  TForm1 = class(TForm)
    NMMsg1: TNMMsg;
    NMMSGServ1: TNMMSGServ;
    Edit1: TEdit;
    Label1: TLabel;
    Memo1: TMemo;
    Button1: TButton;
    Label2: TLabel;
    Edit2: TEdit;
    Label3: TLabel;
    Edit3: TEdit;
    Memo2: TMemo;
    Label4: TLabel;
    Button2: TButton;
    Label5: TLabel;
    Button3: TButton;
    SaveDialog1: TSaveDialog;
    OpenDialog1: TOpenDialog;
    NMStrm1: TNMStrm;
    NMStrmServ1: TNMStrmServ;
    Edit4: TEdit;
    Button4: TButton;
    Button5: TButton;
    Label6: TLabel;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    procedure Button1Click(Sender: TObject);
    procedure NMMSGServ1MSG(Sender: TComponent; const sFrom, sMsg: String);
    procedure NMMsg1MessageSent(Sender: TObject);
    procedure Edit2KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure NMStrmServ1MSG(Sender: TComponent; const sFrom: String;
      strm: TStream);
    procedure FormDblClick(Sender: TObject);
    procedure WMICON(var Msg: TMessage); message WM_ICONNOTIFY;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
  private
    { Private declarations }

  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  TrayIcon: Boolean = False;
implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
NMMsg1.FromName:=edit1.Text;
NMMsg1.Host:=edit3.Text;
 NMMsg1.PostIt(Edit2.Text);
 Edit2.Text:='';
end;

procedure TForm1.NMMSGServ1MSG(Sender: TComponent; const sFrom,
  sMsg: String);
begin
form1.show;
Memo1.Lines.Add('['+sFrom+'] '+sMsg);
Memo2.Lines.Add('Получено сообщение от '+sFrom);

end;

procedure TForm1.NMMsg1MessageSent(Sender: TObject);
begin
 Memo2.Lines.Add('Отправлено сообщение от '+DateTimeToStr(Now));


end;

procedure TForm1.Edit2KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if key=vk_return then button1.Click;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if savedialog1.Execute then
memo1.Lines.SaveToFile(savedialog1.FileName);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
if savedialog1.Execute then
memo2.Lines.SaveToFile(savedialog1.FileName);
end;

procedure TForm1.Button4Click(Sender: TObject);
var
  MyStream: TMemoryStream;
begin
NMStrm1.FromName:=edit1.Text;
NMStrm1.Host:=edit3.Text;
  MyStream := TMemoryStream.Create;
  MyStream.LoadFromFile(edit4.Text);
  NMStrm1.PostIt(MyStream);
  MyStream.Free;
  Memo2.Lines.Add('Отправлен файл от '+DateTimeToStr(Now));
  NMMsg1.FromName:=edit1.Text;
NMMsg1.Host:=edit3.Text;
 NMMsg1.PostIt('Отправлен файл '+edit4.Text);
 edit4.Text:='';
 Edit2.Text:='';
end;




procedure TForm1.Button5Click(Sender: TObject);
begin
if opendialog1.Execute then
 edit4.Text:=opendialog1.FileName;
end;

procedure TForm1.NMStrmServ1MSG(Sender: TComponent; const sFrom: String;
  strm: TStream);
var
  MyStream: TMemoryStream;
begin
form1.show;
  MyStream := TMemoryStream.Create;
  MyStream.CopyFrom(strm, NMStrmServ1.BytesTotal);
   if savedialog1.Execute then
  MyStream.SaveToFile(savedialog1.FileName);
  Memo2.Lines.Add('Получен файл от '+sFrom);
end;

procedure AddTrayIcon; 
var 
  NID: TNotifyIconData; 
begin 
   with NID do begin 
     cbSize := SizeOf(TNotifyIconData); 
     Wnd := Form1.Handle; 
     uID := 1; 
     uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP; 
     uCallBackMessage := WM_ICONNOTIFY; 
     hIcon := Application.Icon.Handle; 
     szTip := 'Chat';
   end; 
   TrayIcon := Shell_NotifyIcon(NIM_ADD, Addr(NID)); 
end; 

procedure DeleteTrayIcon; 
var
  NID: TNotifyIconData; 
begin 
  with NID do begin 
    cbSize := SizeOf(TNotifyIconData); 
    Wnd := Form1.Handle; 
    uID := 1; 
  end; 
  TrayIcon := not Shell_NotifyIcon(NIM_DELETE, Addr(NID)); 
end; 

procedure TForm1.WMICON(var Msg: TMessage);
begin 
  case Msg.LParam of
    WM_RBUTTONUP:form1.Show;
    WM_MBUTTONDOWN:form1.Show;
    WM_LBUTTONDBLCLK: Form1.Show;
  end; 
end;


procedure TForm1.FormDblClick(Sender: TObject);
begin

 form1.Hide;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  AddTrayIcon;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 DeleteTrayIcon;
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
memo2.Lines.Clear;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
memo1.Lines.Clear;
end;

procedure TForm1.Button8Click(Sender: TObject);
begin
form2.show;
end;

end.

