niedziela, 25 września 2022

Reading/opening files with Windows API (C++ Builder)







 private: // User declarations

         DWORD temp_Size;

         HANDLE temp_Handle;

         bool __fastcall handle_True(HANDLE value);


TForm1 *Form1;


SECURITY_ATTRIBUTES sec_Atr={sizeof(SECURITY_ATTRIBUTES),NULL,TRUE};

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

        : TForm(Owner)

{


}

//---------------------------------------------------------------------------


bool __fastcall TForm1::handle_True(HANDLE value)

{

 if(value!=INVALID_HANDLE_VALUE)

  return true;

 else

  return false;

}


void __fastcall TForm1::FormCreate(TObject *Sender)

{

 DirectoryListBox1->FileList=FileListBox1;

 DriveComboBox1->DirList=DirectoryListBox1;

 FileListBox1->FileEdit=Edit1;

 Label1->Caption=DateToStr(Date())+"   "+TimeToStr(Time());


}

//---------------------------------------------------------------------------

void __fastcall TForm1::FileListBox1Change(TObject *Sender)

{

 int a;

  char buff[64*64];

 memset(buff,0,sizeof(buff)-1);

 AnsiString temp_Text=FileListBox1->FileName;

 temp_Handle=CreateFile(temp_Text.c_str(),GENERIC_READ,FILE_SHARE_READ,&sec_Atr,

 OPEN_EXISTING,0,NULL);

 if(handle_True(temp_Handle))

  ReadFile(temp_Handle,&buff,sizeof(buff),&temp_Size,NULL);

 Memo1->Text=buff;

 CloseHandle(temp_Handle);


}

poniedziałek, 19 września 2022

Connecting to Word and Excel (C++ Builder)


 

private: // User declarations


        Variant m_Excel;

        Variant m_Word;


TForm1 *Form1;


PropertySet to_Word("Visible");

PropertySet to_Excel("Visible");

Procedure close_Word("Quit");

Procedure close_Excel("Quit");


//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

        : TForm(Owner)

{

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)

{

 try

 {

  m_Excel=Variant::CreateObject("Excel.Application");

  to_Excel<<true;

  m_Excel.Exec(to_Excel);

 }

 catch(...)

 {

  ShowMessage("i can't open. Excel is not installed!");

 }

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)

{

 try

 {

  m_Word=Variant::CreateObject("Word.Application");

  to_Word<<true;

  m_Word.Exec(to_Word);

 }

 catch(...)

 {

  ShowMessage("I can't open. Word is not installed!");

 }

}


//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)

{

 try

 {

  close_Excel<<false;

  m_Excel.Exec(close_Excel);

  m_Excel=Unassigned;

 }

 catch(...)

 {

  ShowMessage("Excel is not installed!");

 }

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)

{

 try

 {

  close_Word<<false;

  m_Word.Exec(close_Word);

  m_Word=Unassigned;


 }

 catch(...)

 {

  ShowMessage("Word is not installed!");

 }

}

niedziela, 11 września 2022

Voronoi shapes (C++ Builder and Lazarus)




C++ Builder 

private: // User declarations

       double __fastcall write_Distance(int a, int b, int c, int d);
       int r,g,b;
       int date_X[10], date_Y[10];

__fastcall TForm1::TForm1(TComponent* Owner)

        : TForm(Owner)

{

 srand(time(NULL));

}

double __fastcall TForm1::write_Distance(int a, int b, int c, int d)

{

 double result;

 result=sqrt((a-c)*(a-c)+(b-d)*(b-d));

 return result;

}

//---------------------------------------------------------------------------


void __fastcall TForm1::Button1Click(TObject *Sender)

{

 TColor base_Color[10];

 int i,j,k,l,w,h;

 double d1,d2;

 j=20000+rand()%20000;

 d1=(1.00*j);

 w=PaintBox1->Width;

 h=PaintBox1->Height;

 for(i=0;i<10;i++)

 {

  r=rand()%255;

  g=rand()%255;

  b=rand()%255;

  base_Color[i]=RGB(r,g,b);

  date_X[i]=rand()%w;

  date_Y[i]=rand()%h;

 }

 for(i=0;i<w;i++)

 {

  for(j=0;j<h;j++)

  {

   d2=d1;

   for(k=0;k<10;k++)

   {

    if(write_Distance(i,j,date_X[k],date_Y[k])<d2)

    {

     l=k;

     d2=write_Distance(i,j,date_X[k],date_Y[k]);

    }

   }

   PaintBox1->Canvas->Pixels[i][j]=base_Color[l];

  }

 }

}


Lazarus


function distance_P(a,b,c,d:Integer):Real;

var

dist: real;

begin

  dist:=Sqrt((a-c)*(a-c)+(b-d)*(b-d));

  distance_P:=dist;

end;   


procedure TForm1.FormCreate(Sender: TObject);

begin

    Randomize;

end;


procedure TForm1.Button1Click(Sender: TObject);

var

  i,j,k,l,x_Width,y_Height: Integer;

  base_Colors : array[0..10] of TColor;

  x_Date : array[0..10] of Integer;

  y_Date : array[0..10] of Integer;

  temp_Distance1, temp_Distance2 : Real;

begin

  i:=20000+random(20000);

  temp_Distance1:=(1.00*i);

  base_Colors[0]:=clLime;

  base_Colors[1]:=clYellow;

  base_Colors[2]:=clRed;

  base_Colors[3]:=clGreen;

  base_Colors[4]:=clBlue;

  base_Colors[5]:=clBlack;

  base_Colors[6]:=clWhite;

  base_Colors[7]:=clAqua;

  base_Colors[8]:=clGray;

  base_Colors[9]:=clPurple;

  base_Colors[10]:=clOlive;

  x_Width:=PaintBox1.Width;

  y_Height:=PaintBox1.Height;

  for i:=0 to 10 do

  begin

   x_Date[i]:=random(x_Width);

   y_Date[i]:=random(y_Height);

  end;

  for i:=0 to x_Width do

  begin

   for j:=0 to y_Height do

   begin

       temp_Distance2:=temp_Distance1;

       for k:=0 to 10 do

       begin

           if distance_P(i,j,x_Date[k],y_Date[k])<temp_Distance2 then

           begin

             l:=k;

             temp_Distance2:=distance_P(i,j,x_Date[k],y_Date[k]);


           end;

       end;

       PaintBox1.Canvas.Pixels[i,j]:=base_Colors[l];

   end;

  end;


end;


end.