wtorek, 26 września 2017

Dynamically created control (class listBox) with its own function looking for files in the directory (Visual C++)

File Test_Cl.h :


using namespace System::IO;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

ref class files_ListBox : public System::Windows::Forms::ListBox
{
public: System::Void find_Files(System::String^ directory,System::String^ files)
{
System::Int64 i;

DirectoryInfo^ my_directory=gcnew DirectoryInfo(directory);
array<FileInfo^>^ my_files=my_directory->GetFiles(files);
for(i=0;i<my_files->Length;i++)
{
this->Items->Add(my_files[i]->Name+" "+my_files[i]->Length+" "+my_files[i]->CreationTime);
}

}

};

And a fragment of the file - Form1.h : 

#pragma once

#include "test_Cl.h"

private:
/// <summary>
/// Required designer variable.
/// </summary>
files_ListBox test_FLB;
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->test_FLB=(gcnew files_ListBox());
this->SuspendLayout();
this->test_FLB->Name=L"test_FLB";
this->test_FLB->Location=System::Drawing::Point(Form1::Width/2,Form1::Height/2);
this->test_FLB->Size=System::Drawing::Size(200,200);



#pragma endregion
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
test_FLB->find_Files("c:\\Windows","*.txt*");
}
};
}


niedziela, 24 września 2017

Collection of ASCII (class in Visual C++)

class collection_of_ASCII
{
private:
System::Char set_Ascii[256];
public: 
collection_of_ASCII();
System::Boolean is_in_Colection(System::Char value);
collection_of_ASCII& Connect(collection_of_ASCII temp_Collection);
collection_of_ASCII& operator -(System::Char value);
collection_of_ASCII& operator +(System::Char value);
};

collection_of_ASCII::collection_of_ASCII()
{
System::Int64 i;
for(i=0;i<256;i++)
set_Ascii[i]=0;
}
System::Boolean collection_of_ASCII::is_in_Colection(System::Char value)
{
if(set_Ascii[value]==-1)
return true;
else
return false;
}
collection_of_ASCII& collection_of_ASCII::Connect(collection_of_ASCII temp_Collection)
{
System::Int64 i;
for(i=0;i<256;i++)
if(temp_Collection.is_in_Colection(i))
set_Ascii[i]=-1;
return *this;
}
collection_of_ASCII& collection_of_ASCII::operator -(System::Char value)
{
 set_Ascii[value]=0;
 return *this;
}
collection_of_ASCII& collection_of_ASCII::operator +(System::Char value)
{
set_Ascii[value]=-1;
return *this;
}

sobota, 23 września 2017

Example class Thread (C++ Builder)

#include <vcl.h>
#include <math.h>
using namespace std;


bool is_First(int x)
{
int i,sum;
sum=0;
for(i=1;i<x+1;i++)
if(x%i==0)
 ++sum;
if(sum==2)
return true;
else
return false;
}

void write_ListBox(TListBox *LB, int value)
{
LB->Items->Add(IntToStr(value));
}

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



class example_Thread
{
private:
TListBox *listB;
LONG run_T;


HANDLE my_Event;
public:
example_Thread(TListBox *tempBox);
~example_Thread();
UINT my_ID;
int  next_First(LPVOID values);
void start_Thread();
void suspend_Thread();
void resume_Thread();
};

example_Thread::example_Thread(TListBox *tempBox)
{
run_T=0;
listB=tempBox;

}
example_Thread::~example_Thread()
{
CloseHandle((LPVOID)run_T);
}
int example_Thread::next_First(LPVOID values)
{
int x;
x=1;
for(;;)
{
  if(is_First(x))
write_ListBox(listB,x);
  WaitForSingleObject((LPVOID)run_T,500);
  ++x;
}
}
void example_Thread::start_Thread()
{

run_T=BeginThread(&this_attributes,4096,next_First,this,CREATE_SUSPENDED,my_ID);
if(run_T==(int)INVALID_HANDLE_VALUE)
{
ShowMessage("ERROR THREADS!!!");
}
else
{
ResumeThread((LPVOID)run_T);
my_Event=CreateEvent(NULL,FALSE,FALSE,NULL);
if(my_Event)
{
WaitForSingleObject(my_Event,500);
CloseHandle(my_Event);
}
}
}
void example_Thread::suspend_Thread()
{
SuspendThread((LPVOID)run_T);
}
void example_Thread::resume_Thread()
{
ResumeThread((LPVOID)run_T);
}

piątek, 22 września 2017

Class to draw a tree (C++ Builder)

file - m_tree.h



#ifndef m_treeH
#define m_treeH
//---------------------------------------------------------------------------
#include <stdlib.h>
#include <time.h>
#include <vcl.h>
class draw_Tree
{
  private:
   float x1[5],x2[5],x3[5],x4[5],x5[6],x6[6],x7[6],x8[6],x9[6],
   x10[6],x11[6],x12[6];
   TForm *tf;


  public:
   draw_Tree();
   void init_Date();
   void Draw(TForm *form);
} ;


#endif


file - m_tree.cpp

#include <stdlib.h>
#include <time.h>
#include <vcl.h>
#pragma hdrstop

#include "m_tree.h"

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

#pragma package(smart_init)

#define my_MAX 65535
draw_Tree::draw_Tree()
{
  srand(time(NULL));

}

void draw_Tree::init_Date()
{
 int i,value,temp;
 for(i=0;i<5;i++)
 {
   value=rand()%100+1;
   temp=rand()%7;
   x1[i]=(1.0*value/100.);
   if(temp<4)
    x1[i]*=-1.;
   x2[i]=(1.0*value/100.);
   if(temp<4)
    x2[i]*=-1.;
   x3[i]=(1.0*value/100.);
   if(temp<4)
    x3[i]*=-1.;
   x4[i]=(1.0*value/100.);
   if(temp<4)
   x4[i]*=-1.;
   x5[i]=(1.0*value/100.);
   if(temp<4)
   x5[i]*=-1.;
   x6[i]=(1.0*value/100.);
   if(temp<4)
    x6[i]*=-1.;
   x7[i]=(1.0*value/100.);
   if(temp<4)
   x7[i]*=-1.;
   x8[i]=(1.0*value/100.);
   if(temp<4)
    x8[i]*=-1.;
   x9[i]=(1.0*value/100.);
   if(temp<4)
   x9[i]*=-1.;
   x10[i]=(1.0*value/100.);
   if(temp<4)
    x10[i]*=-1.;
   x11[i]=(1.0*value/100.);
   if(temp<4)
   x11[i]*=-1.;
   x12[i]=(1.0*value/100.);
   if(temp<4)
    x12[i]*=-1.;

 }
}
void draw_Tree::Draw(TForm *form)
{
  int i,j,k;
  float vx_value,vy_value;

  tf=form;
  vx_value=rand()%my_MAX+1;
  vy_value=rand()%my_MAX+1;
  i=0;
  do
  {
   j=rand()%8;
   switch(j)
   {
    case 0:
    vx_value=vx_value*x1[0]+vy_value*x2[0]+x3[0];
    vy_value=vx_value*x4[0]+vy_value*x5[0]+x6[0];
    break;
    case 1:
    vx_value=vx_value*x1[1]+vy_value*x2[1]+x3[1];
    vy_value=vx_value*x4[1]+vy_value*x5[1]+x6[1];
    break;
    case 2:
    vx_value=vx_value*x1[2]+vy_value*x2[2]+x3[2];
    vy_value=vx_value*x4[2]+vy_value*x5[2]+x6[2];
    break;
    case 3:
    vx_value=vx_value*x1[3]+vy_value*x2[3]+x3[3];
    vy_value=vx_value*x4[3]+vy_value*x5[3]+x6[3];
    break;
    case 4:
    vx_value=vx_value*x7[0]+vy_value*x8[0]+x9[0];
    vy_value=vx_value*x10[0]+vy_value*x11[0]+x12[0];
    break;
    case 5:
    vx_value=vx_value*x7[1]+vy_value*x8[1]+x9[1];
    vy_value=vx_value*x10[1]+vy_value*x11[1]+x12[1];
    break;
    case 6:
    vx_value=vx_value*x7[2]+vy_value*x8[2]+x9[2];
    vy_value=vx_value*x10[2]+vy_value*x11[2]+x12[2];
    break;
    case 7:
    vx_value=vx_value*x7[3]+vy_value*x8[3]+x9[3];
    vy_value=vx_value*x10[3]+vy_value*x11[3]+x12[3];
    break;


   }
   tf->Canvas->Pixels[0.5*tf->Width+Floor(5*vx_value)]
   [0.5*tf->Height-Floor(5*vy_value)]=clGreen;
   ++i;
  }while(i<(5.*my_MAX));
}

Use class in the programm : 

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 draw_Tree DRAW;
 DRAW.init_Date();
 DRAW.Draw(Form1);

}


środa, 13 września 2017

Sending graphics from the picurBox to the server FTP (Visual C++)




private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
;
System::Double PI=3.1415926535;
System::Int64 Width=pictureBox1->Width/2;
 
System::Double x,y,z,v,i;
Graphics^ g=pictureBox1->CreateGraphics();
System::Drawing::Color my_color;
Random^ random_value=gcnew Random();
for(i=(-2.*PI);i<(2.*PI);i+=0.03)
{
x=Width*sin(i);
y=Width*cos(i);
v=Width*1.0;
z=(-1.0*Width*sin(i));
my_color=System::Drawing::Color::FromArgb(random_value->Next(255),random_value->Next(255),
random_value->Next(255));
Pen^ my_pen=gcnew Pen(my_color);
g->DrawLine(my_pen,int((1.0*Width)+x),int((1.0*Width)+y),int((1.0*Width)+z),int((1.0*Width)+v));
}


}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
Close();
}
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
System::String^ file_Name=textBox1->Text;
System::String^ FTP_Name=textBox2->Text;
pictureBox1->Image->Save(file_Name);
Uri^ uri_adress=gcnew Uri("ftp://"+FTP_Name+"/"+Path::GetFileName(file_Name));
FtpWebRequest^ my_req=dynamic_cast<FtpWebRequest^>(WebRequest::Create(uri_adress));
my_req->Credentials=gcnew
NetworkCredential("anonymus","my_name@server.com");
my_req->Method=WebRequestMethods::Ftp::UploadFile;
FileStream^ send_file=gcnew FileStream(file_Name,FileMode::Open);
Stream^ my_stream=my_req->GetRequestStream();
System::Int64 bytes;
array<Byte> ^ table=gcnew array<Byte>(1024);
do
{
bytes=send_file->Read(table,0,1024);
my_stream->Write(table,0,bytes);
}while(bytes!=0);
my_stream->Close();
 



}
};
}

czwartek, 7 września 2017

Effective graphical transformations - Visual C++





I use a Polsih book - "C++ Builder 20 efektownych programów" - Andrzej Stasiewicz. I convert C++ Builder code to Visual C++. Sometimes is not easy. Two environments, a completely different philosophy.