//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "trayicon"
#pragma resource "*.dfm"
TForm1 *Form1;

TCanvas &PulpitCanvas = *new TCanvas();
Graphics::TBitmap *Bmp = new Graphics::TBitmap;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{

if(Button1->Caption == "Start"){


if(StrToInt(Edit3->Text) >= 5000){

        ClientSocket1->Address = Edit1->Text;
        ClientSocket1->Port = StrToInt(Edit2->Text);
        ClientSocket1->Active = true;

        Timer1->Interval = (StrToInt(Edit3->Text));
        Button1->Caption = "Stop";

} else {

        ShowMessage("Zbyt mały interwał!");
}

} else {

        Timer1->Enabled = false;
        ClientSocket1->Active = false;
        Button1->Caption = "Start";
}

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
        this->Close();        
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
      TCustomWinSocket *Socket)
{

 Button1->Enabled = true;
 ShowMessage("Połączyłes się z serwerem!");
 Timer1->Enabled = true;

}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Disconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
        ShowMessage("Rozłączyłes się!");

}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Error(TObject *Sender,
      TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
        ShowMessage("Nastąpił błąd komunikacja nie jest możliwa!");
        this->Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Connecting(TObject *Sender,
      TCustomWinSocket *Socket)
{
        Button1->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
    PulpitCanvas.Handle = GetDC(0);
    Bmp->Width = Screen->Width;
    Bmp->Height = Screen->Height;
    Bmp->Canvas->CopyRect(Rect(0, 0, Bmp->Width, Bmp->Height), &PulpitCanvas,
                          Rect(0, 0, Bmp->Width, Bmp->Height));

    Bmp->SaveToFile("skrinn.bmp");
    ClientSocket1->Socket->SendText('s');

}
//---------------------------------------------------------------------------


void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
      TCustomWinSocket *Socket)
{

int messageSize = Socket->ReceiveLength();

if(messageSize == sizeof(char)){

AnsiString messageText = Socket->ReceiveText();

if (messageText == "g"){

        TFileStream *fs;
        fs = new TFileStream("skrinn.bmp", fmOpenRead | fmShareDenyNone);

        ClientSocket1->Socket->SendText( IntToStr(fs->Size) );
        fs->Free();


} else if( messageText == "k"){

 ClientSocket1->Socket->SendText('f');


} else if (messageText == "r"){

        TFileStream *fs;
        char *buffer;
        fs = new TFileStream("skrinn.bmp", fmOpenRead | fmShareDenyNone);

        buffer = new char[fs->Size];
        fs->Read(buffer,fs->Size);

        if( ClientSocket1->Socket->Connected){

                ClientSocket1->Socket->SendBuf(buffer, fs->Size);
                fs->Free();
                delete buffer;


        } else {

                ShowMessage("Połączenie utracone!");
        }

}

}

}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
        delete &PulpitCanvas;
        delete Bmp;
}
//---------------------------------------------------------------------------

