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

#include <vcl.h>
#pragma hdrstop
#include "math.h"
#include <iomanip>
#include "stdio.h"
#include <vector>  //2022-2-4
#include "ctime.h"  //2022-2-4
#include <fstream>  //2022-2-4
#include "assert.h" //2022-2-4
#include "io.h"     //2022-2-4
#include <windows.h>
#include "Unit2.h"
//#include "stdafx.h"
#include <cstring>
#include <algorithm>
#include <Dialogs.hpp>
#include "string.h"
#include "iostream.h"
#include "stdlib.h"
#include "ctype.h"
#include "conio.h"
#include "Unit2.h"
#include "Unit3.h"
#include "Unit6.h"
#include "Unit5.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm5 *Form5;
float point1[3];
float point2[3];
float move_start[2];
float last_point[4];

int paint = 3;
int paint_move = 0;
int paint_sq = 0;
int switch_new = 0;

void paint_square2();
//---------------------------------------------------------------------------
__fastcall TForm5::TForm5(TComponent* Owner)
                    : TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TForm5::N2Click(TObject *Sender)
{
   ScrollBox1->Width=8*(Form1->Width);
   ScrollBox1->Left=5;
   ScrollBox1->Top=5;
   PaintBox1->Width=ScrollBox1->Width;
   ScrollBox1->Height=8*(Form1->Height);
   PaintBox1->Height=ScrollBox1->Height;
   PaintBox1->Left=ScrollBox1->Left;
   PaintBox1->Top=ScrollBox1->Top;
   FILE *fp,*fp1,*fp2;
   i=0;
 OpenDialog1->Filter="分层数据库文件（*.DBF)|*.DBF|所有文件(*.*)|*.*";
 if(OpenDialog1->Execute())
 {
     for(int I=0;I<OpenDialog1->Files->Count;I++)
     {
         fp=fopen(OpenDialog1->Files->Strings[I].c_str(),"r");
         cc=OpenDialog1->Files->Strings[I].c_str();
         if(fp)
         {
              Table1->TableName=cc;
              Table1->Open();

              Table1->Active=true;
              Table1->First();
              while(!Table1->Eof)
              {
                  if((AnsiPos("N7",Table1->FieldByName("Jh")->AsString)>0)||(AnsiPos("N8",Table1->FieldByName("Jh")->AsString)>0))
                  {
                      Jh[i]=Table1->FieldByName("Jh")->AsString;
                      Zzbx[i]=Table1->FieldByName("Zzbx")->Value;
                      Hzby[i]=Table1->FieldByName("Hzby")->Value;
                      if(AnsiPos("N7",Jh[i])>0)       //筛选南7区块的井
                      {
                          text[i]=Jh[i];
                          x[i]=Zzbx[i]-34500;
                          y[i]=Hzby[i]-9000;
                      }
                      else if(AnsiPos("N8",Jh[i])>0)     //筛选南8区块的井
                      {
                          text[i]=Jh[i];
                          x[i]=Zzbx[i]-34500;
                          y[i]=Hzby[i]-8500;
                      }

                      i++;

                  }
                  Table1->Next();
              }

         }
     }
  }
  draw(x,y,text);
  Table1->First();
 fclose(fp);

                    
}
//---------------------------------------------------------------------------
void __fastcall TForm5::draw(float x[20000],float y[20000],AnsiString text[20000])
{
   for(int i0=1;i0<i;i0++)           //坐标绘制函数，用循环遍历才能得到全部井坐标
   {
      PaintBox1->Canvas->Pen->Color=clRed;
      PaintBox1->Canvas->Pen->Width=2;
      PaintBox1->Canvas->MoveTo(x[i0],y[i0]);
      PaintBox1->Canvas->TextOut(x[i0]+5,y[i0]-5,text[i0]);
      PaintBox1->Canvas->Ellipse(x[i0]-5,y[i0]-5,x[i0]+5,y[i0]+5);
   }
 }
//---------------------------------------------------------------------------
void __fastcall TForm5::draw2(float x[20000],float y[20000],AnsiString text[20000])
{
   for(int i0=1;i0<i;i0++)           //坐标绘制函数，用循环遍历才能得到全部井坐标
   {
      PaintBox2->Canvas->Pen->Color=clRed;
      PaintBox2->Canvas->Pen->Width=2;
      PaintBox2->Canvas->MoveTo(x[i0],y[i0]);
      PaintBox2->Canvas->TextOut(x[i0]+5,y[i0]-5,text[i0]);
      PaintBox2->Canvas->Ellipse(x[i0]-5,y[i0]-5,x[i0]+5,y[i0]+5);
   }
 }
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
void paint_square2()
{
                Form5->PaintBox2->Canvas->Pen->Color = clRed;
                Form5->PaintBox2->Canvas->Pen->Width = 2;
                Form5->PaintBox2->Canvas->MoveTo(point1[1],point1[2]);
                Form5->PaintBox2->Canvas->Rectangle(point1[1],point1[2],point2[1],point2[2]);
}

//----------------------------------------------------------------------------
void __fastcall TForm5::Pb2_up(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{

        if(Button==mbLeft && paint == 0 && paint_move == 1)
        {
                point1[1] = point1[1] + X - move_start[0];
                point2[1] = point2[1] + X - move_start[0];
                point1[2] = point1[2] + Y - move_start[1];
                point2[2] = point2[2] + Y - move_start[1];
                paint_move = 0;
        }
        if(Button==mbLeft && paint != 0 && point1[0] == 1)
        {
                point2[1] = X;
                point2[2] = Y;
                Form5->PaintBox2->Canvas->Pen->Color = clRed;
                Form5->PaintBox2->Canvas->Pen->Width = 2;
                Form5->PaintBox2->Canvas->MoveTo(X,Y);
                Form5->PaintBox2->Canvas->Rectangle(X - 1,Y - 1,X + 1,Y + 1);
                point1[0] = 1;
                paint = 1;
                paint_sq = 1;
        }
        if(Button==mbLeft)
        {
                ox1=X;
                oy1=Y;
                xx2=ox1;  //xx2和yy2是为了在擦除矩形框时调用的，也可以用ox1和oy1
                yy2=oy1;  //因为再次重绘矩形框时只按鼠标键并没有松开
                A=1;  // 表示已经绘制完成一个矩形框，为Down里擦除这个矩形框if语句提供条件
                ox = point1[1];
                ox1 = point2[1];
                oy = point1[2];
                oy1 = point2[2];

                PaintBox1->Canvas->Pen->Color=clBlue;

                PaintBox1->Canvas->Brush->Style=bsClear;
                PaintBox1->Canvas->MoveTo(ox,oy);
                PaintBox1->Canvas->Pen->Width=2;          //在画矩形框时这里同样要设置Mode，才能擦除矩形框
                PaintBox1->Canvas->Pen->Mode=pmNotXor;      // 并且画笔的宽度要一致

                //  PaintBox1->Canvas->Rectangle(ox,oy,ox1,oy1);
                for(int i0=0;i0<i;i0++)
                {
                if((ox<x[i0])&&(x[i0]<ox1)&&(oy<y[i0])&&(y[i0]<oy1)) //逻辑运算要拆开再用()合并，否则可能不触发
                {                                                // 鼠标从左上角向右下角画矩形进行筛选
                   x1[i0]=x[i0];
                   y1[i0]=y[i0];
                   Jh1[i0]=text[i0];
                   ic[inum]=i0;
                   inum=inum+1;
                   PaintBox1->Canvas->Pen->Width=3 ;          //圈选井变色
                   PaintBox1->Canvas->Pen->Mode=pmNotXor;
                   PaintBox1->Canvas->Ellipse(x[i0]-5,y[i0]-5,x[i0]+5,y[i0]+5);
                }
                else if((ox>x[i0])&&(x[i0]>ox1)&&(oy>y[i0])&&(y[i0]>oy1)) // 鼠标从右下角向左上角画矩形进行筛选
                {
                   x1[i0]=x[i0];
                   y1[i0]=y[i0];
                   Jh1[i0]=text[i0];
                   ic[inum]=i0;
                   inum=inum+1;
                   PaintBox1->Canvas->Pen->Width=3 ;          //圈选井变色
                   PaintBox1->Canvas->Pen->Mode=pmNotXor;
                   PaintBox1->Canvas->Ellipse(x[i0]-5,y[i0]-5,x[i0]+5,y[i0]+5);
                }
                else if((ox>x[i0])&&(x[i0]>ox1)&&(oy<y[i0])&&(y[i0]<oy1)) // 鼠标从右上角向左下角画矩形进行筛选
                {
                   x1[i0]=x[i0];
                   y1[i0]=y[i0];
                   Jh1[i0]=text[i0];
                   ic[inum]=i0;
                   inum=inum+1;
                   PaintBox1->Canvas->Pen->Width=3 ;          //圈选井变色
                   PaintBox1->Canvas->Pen->Mode=pmNotXor;
                   PaintBox1->Canvas->Ellipse(x[i0]-5,y[i0]-5,x[i0]+5,y[i0]+5);
                }
                else if((ox<x[i0])&&(x[i0]<ox1)&&(oy>y[i0])&&(y[i0]>oy1)) // 鼠标从左下角向右下角画矩形进行筛选
                {
                   x1[i0]=x[i0];
                   y1[i0]=y[i0];
                   Jh1[i0]=text[i0];
                   ic[inum]=i0;
                   inum=inum+1;
                   PaintBox1->Canvas->Pen->Width=3 ;          //圈选井变色
                   PaintBox1->Canvas->Pen->Mode=pmNotXor;
                   PaintBox1->Canvas->Ellipse(x[i0]-5,y[i0]-5,x[i0]+5,y[i0]+5);
                }
                }
        }
        if(Button==mbRight)
        {
        //  KG=false;
                ox3=X;
                oy3=Y;

                for(int i0=0;i0<i;i0++)
                {
                        if((x[i0]-5<ox2)&&(ox2<x[i0]+5)&&(oy2>y[i0]-5)&&(oy2<y[i0]+5))
                        {
                                x2[i0]=x[i0];
                                y2[i0]=y[i0];
                                Jh2[i0]=text[i0];
                                ic[inum+inum1]=i0;
                                inum1=inum1+1;
                                PaintBox1->Canvas->Pen->Width=3 ;          //圈选井变色
                                PaintBox1->Canvas->Pen->Mode=pmNotXor;
                                PaintBox1->Canvas->Ellipse(x[i0]-5,y[i0]-5,x[i0]+5,y[i0]+5);
                        }
                }
        }

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

void __fastcall TForm5::FormCreate(TObject *Sender)
{
    Form5->WindowState=wsMaximized;
    ScrollBox1->Width=8*(Form1->Width);
    ScrollBox1->Left=5;
    ScrollBox1->Top=5;
    PaintBox1->Width=ScrollBox1->Width;
    ScrollBox1->Height=8*(Form1->Height);
    PaintBox1->Height=ScrollBox1->Height;
    PaintBox1->Left=ScrollBox1->Left;
    PaintBox1->Top=ScrollBox1->Top;
    inum=0;inum1=0;
    PaintBox2->Width=ScrollBox1->Width;
    PaintBox2->Height=ScrollBox1->Height;
    PaintBox2->Left=ScrollBox1->Left;
    PaintBox2->Top=ScrollBox1->Top;
}
//---------------------------------------------------------------------------

void __fastcall TForm5::Pb2_move(TObject *Sender,
      TShiftState Shift, int X, int Y)
{
        if(paint != 0 && point1[0] == 1 && paint_sq == 0)
        {
                Form5->PaintBox2->Canvas->Pen->Width = 2;
                Form5->PaintBox2->Canvas->Pen->Color = Form1->PaintBox2->Color;
                Form5->PaintBox2->Canvas->Rectangle(last_point[0],last_point[1],last_point[2],last_point[3]);
                Form5->PaintBox2->Canvas->Pen->Color = clRed;
                Form5->PaintBox1->Canvas->Rectangle(point1[1],point1[2],X,Y);
                draw2(x,y,text);
                last_point[0] = point1[1];
                last_point[1] = point1[2];
                last_point[2] = X;
                last_point[3] = Y;
        }
        if(paint_move == 1)
        {
                Form5->PaintBox2->Canvas->Pen->Color = Form1->PaintBox2->Color;
                Form5->PaintBox2->Canvas->Rectangle(last_point[0],last_point[1],last_point[2],last_point[3]);
                Form5->PaintBox2->Canvas->Pen->Color = clRed;
                Form5->PaintBox2->Canvas->Rectangle(point1[1] + X - move_start[0],point1[2] + Y - move_start[1],point2[1] + X - move_start[0],point2[2] + Y - move_start[1]);
                last_point[0] = point1[1] + X - move_start[0];
                last_point[1] = point1[2] + Y - move_start[1];
                last_point[2] = point2[1] + X - move_start[0];
                last_point[3] = point2[2] + Y - move_start[1];
                draw2(x,y,text);
        }
                    
}
//---------------------------------------------------------------------------

void __fastcall TForm5::Pb2_down(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{

        if(Button==mbLeft)
        {
           Form5->PaintBox2->Canvas->Pen->Color = clRed;
           Form5->PaintBox2->Canvas->Pen->Width = 2;
           inum=0;
        if(paint == 0 || paint == 1)
        {
                float x0 = 0,x1 = 0,y0 = 0,y1 = 0;
                if(point1[1] <= point2[1])
                {
                        x0 = point1[1];
                        x1 = point2[1];
                }
                else
                {
                        x1 = point1[1];
                        x0 = point2[1];
                }
                if(point1[2] <= point2[2])
                {
                        y0 = point1[2];
                        y1 = point2[2];
                }
                else
                {
                        y1 = point1[2];
                        y0 = point2[2];
                }

                if(Button == mbLeft && X >= x0 && X < x1 && Y >= y0 && Y <= y1)
                {
                        paint_move = 1;
                        move_start[0] = X;
                        move_start[1] = Y;
                }
                else
                {
                        Form5->PaintBox2->Refresh();
                        paint = 3;
                        point1[0] = 0;
                        point2[0] = 0;
                        paint_move = 0;
                        paint_sq = 0;
                        switch_new = 0;
                }
            }
        }
        if(Button == mbLeft && paint != 0 && point1[0] != 1)
        {
                point1[1] = X;
                point1[2] = Y;
                Form1->PaintBox2->Canvas->Pen->Color = clRed;
                point1[0] = 1;

        }
        if(Button == mbLeft && paint == 1)
        {
                paint_square2();
                point1[0] = 0;
                point2[0] = 0;
                paint = 0;
                switch_new = 1;
        }

        if(Button == mbLeft && paint == 0)
        {
                float x0 = 0,x1 = 0,y0 = 0,y1 = 0;
                if(point1[1] <= point2[1])
                {
                        x0 = point1[1];
                        x1 = point2[1];
                }
                else
                {
                        x1 = point1[1];
                        x0 = point2[1];
                }
                if(point1[2] <= point2[2])
                {
                        y0 = point1[2];
                        y1 = point2[2];
                }
                else
                {
                        y1 = point1[2];
                        y0 = point2[2];
                }

                if(Button == mbLeft && X >= x0 && X < x1 && Y >= y0 && Y <= y1)
                {
                        paint_move = 1;
                        move_start[0] = X;
                        move_start[1] = Y;
                }
        }

        if(Button==mbRight)
        {
                ox2=X;
                oy2=Y;
        }

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

void __fastcall TForm5::PaintBox1Paint(TObject *Sender)
{
   draw(x,y,text); //绘图函数触发Onpaint事件才能使画布上的内容不会随着拉动滑竿消失                 
}
//---------------------------------------------------------------------------

void __fastcall TForm5::N4Click(TObject *Sender)
{
        for(int i1=0;i1<inum;i1++)
        {
           if((Jh1[ic[i1]]!=NULL)||(Jh1[ic[i1]]!=""))
           {
               Jh1[ic[i1]]=Jh1[ic[i1]];

           }
        }
        for(int i1=inum;i1<inum+inum1;i1++)
        {
           if(((Jh2[ic[i1]]!=NULL)||(Jh2[ic[i1]]!=""))&&((Jh1[ic[i1]]==NULL)||(Jh1[ic[i1]]=="")))
           {
               Jh1[ic[i1]]=Jh2[ic[i1]];

           }
        }
        inum=inum+inum1; 
   if(Form6->turn==1)
   {
      Form6->Show();
      Form6->N2Click(Sender);
   }
   else
   {
      Form3->Show();
      Form3->N8Click(Sender);
   }
   Form5->Close();
   inum=0; inum1=0;Form6->turn=0;
}
//---------------------------------------------------------------------------

