⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 学校和学生家长用短信及时沟通学生在校情况或及时把有关通知、重要信息发布给学生家长。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
#include "registry.hpp"
#include "stdio.h"
#define RowCount 100
String CGSN="520338413260162";  //tc35 SerioNo
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Comm"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
   : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
   String fn,nn;
   int i,j,k,l,fp;
   char txt[256];

   CurDir=GetCurrentDir();
   sgClassName->Cells[0][0]="序号";
   sgClassName->Cells[1][0]="年级班级";
   //
   sgEdPhoncode->Cells[0][0]="序号";
   sgEdPhoncode->Cells[1][0]="同学姓名";
   sgEdPhoncode->Cells[2][0]="家长姓名";
   sgEdPhoncode->Cells[3][0]="手机号码";
   //
   sgMpBook->Cells[0][0]="序号";
   sgMpBook->Cells[1][0]="同学姓名";
   sgMpBook->Cells[2][0]="家长姓名";
   sgMpBook->Cells[3][0]="手机号码";
   //
   sgEdSmTxt->Cells[0][0]="序号";
   sgEdSmTxt->Cells[1][0]="常用语内容";
   //
   for(i=1;i<RowCount+1;i++){
      if(i<10)
         nn="00"+IntToStr(i);
      else if(i>9&&i<100)
         nn="0"+IntToStr(i);
      else
         nn=IntToStr(i);
      sgMpBook->Cells[0][i]=nn;
      sgClassName->Cells[0][i]=nn;
      sgEdPhoncode->Cells[0][i]=nn;
      sgEdSmTxt->Cells[0][i]=nn;
   }
   fn=CurDir+"\\MpBook.cfg";
   fp=FileOpen(fn,fmOpenRead);
   //
   FileRead(fp,&l,4);
   FileRead(fp,txt,l);
   txt[l]='\0';
   edSmsc->Text=txt;
   //
   FileRead(fp,&l,4);
   FileRead(fp,txt,l);
   txt[l]='\0';
   cbPort->Text=txt;
   //
   for(i=1;i<RowCount+1;i++){
      FileRead(fp,&l,4);
      FileRead(fp,txt,l);
      txt[l]='\0';
      sgClassName->Cells[1][i]=txt;
   }
   for(i=1;i<RowCount+1;i++){
      FileRead(fp,&l,4);
      FileRead(fp,txt,l);
      txt[l]='\0';
      sgEdSmTxt->Cells[1][i]=txt;
   }
   for(i=0;i<RowCount;i++)
      for(j=0;j<RowCount;j++)
         for(k=0;k<3;k++){
            FileRead(fp,&l,4);
            FileRead(fp,txt,l);
            txt[l]='\0';
            NameCode[i][j][k]=txt;
         }
   FileClose(fp); 
   //
   for(i=0;i<RowCount;i++)
      for(j=0;j<3;j++)
         sgEdPhoncode->Cells[j+1][i+1]=NameCode[0][i][j];
   sbSendSms->Enabled=false;
   sbConnectOff->Enabled=false;
   AddCombo();
   PageControlChange(Form1);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
   int i,j,k,l,fp;
   String ss,fn;
   char txt[256];

   fn=CurDir+"\\MpBook.cfg";
   if(FileExists(fn))
      fp=FileOpen(fn,fmOpenWrite);
   else
      fp=FileCreate(fn);
   FileSeek(fp,0,0);
   //
   strcpy(txt,edSmsc->Text.c_str());
   l=strlen(txt);
   FileWrite(fp,&l,4);
   FileWrite(fp,txt,l);
   //
   strcpy(txt,cbPort->Text.c_str());
   l=strlen(txt);
   FileWrite(fp,&l,4);
   FileWrite(fp,txt,l);
   //
   for(i=1;i<RowCount+1;i++){
      ss=sgClassName->Cells[1][i];
      l=ss.Length();
      FileWrite(fp,&l,4);
      strcpy(txt,ss.c_str());
      FileWrite(fp,txt,l);
   }
   for(i=1;i<RowCount+1;i++){
      ss=sgEdSmTxt->Cells[1][i];
      l=ss.Length();
      FileWrite(fp,&l,4);
      strcpy(txt,ss.c_str());
      FileWrite(fp,txt,l);
   }
   for(i=0;i<RowCount;i++){
      for(j=0;j<RowCount;j++){
         for(k=0;k<3;k++){
            ss=NameCode[i][j][k];
            l=ss.Length();
            FileWrite(fp,&l,4);
            strcpy(txt,ss.c_str());
            FileWrite(fp,txt,l);
         }
      }
   }
   FileClose(fp);
   Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::PageControlChange(TObject *Sender)
{
   int i,j;
   
   cbClassName->Clear();
   for(i=1;i<RowCount+1;i++)
      cbClassName->Items->Add(sgClassName->Cells[1][i]);
   cbClassName->Text=sgClassName->Cells[1][1];
   cbSmsTxt->Clear();
   for(i=1;i<RowCount+1;i++)
      cbSmsTxt->Items->Add(sgEdSmTxt->Cells[1][i]);
   cbSmsTxt->Text=sgEdSmTxt->Cells[1][1];
   for(i=0;i<RowCount;i++)
      for(j=0;j<3;j++)
         sgMpBook->Cells[j+1][i+1]=NameCode[0][i][j];
}
//---------------------------------------------------------------------------

void __fastcall TForm1::sbConnectOnClick(TObject *Sender)
{
   CommOpen();
   if(!ConnectMP()){
      ShowMessage("电脑与手机连通失败!");
      sbSendSms->Enabled=false;;
      sbConnectOn->Enabled=true;
      sbConnectOff->Enabled=false;
      StatusBar1->SimpleText="电脑与手机连接断开!";
      Comm1->PortOpen=false;
   }
   else{
      sbSendSms->Enabled=true;
      sbConnectOn->Enabled=false;
      sbConnectOff->Enabled=true;
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::CommOpen()
{
   if(cbPort->Text=="com1")
      Comm1->CommPort=pnCOM1;
   else if(cbPort->Text=="com2")
      Comm1->CommPort=pnCOM2;
   else if(cbPort->Text=="com3")
      Comm1->CommPort=pnCOM3;
   else if(cbPort->Text=="com4")
      Comm1->CommPort=pnCOM4;
   else if(cbPort->Text=="com5")
      Comm1->CommPort=pnCOM5;
   else if(cbPort->Text=="com6")
      Comm1->CommPort=pnCOM6;
   else if(cbPort->Text=="com7")
      Comm1->CommPort=pnCOM7;
   else if(cbPort->Text=="com8")
      Comm1->CommPort=pnCOM8;
   else if(cbPort->Text=="com9")
      Comm1->CommPort=pnCOM9;
   Comm1->BaudRate=br19200;
   Comm1->Parity=None;
   Comm1->StopBits=SB1;
   Comm1->DataBits=DB8;
   Comm1->PortOpen=true;
}
//---------------------------------------------------------------------------

bool __fastcall TForm1::ConnectMP()
{
   int i,p,counter;
   char AtCgmi[]="AT+CGMI\r";
   char AtCgmm[]="AT+CGMM\r";
   String ss;
   DynamicArray<byte> ByteSend;

   if(!GetCgsn()){
      ShowMessage("软件版本错误,请找软件开发商!");
      return false;
   }
   sbRegist->Enabled=false;
   StatusBar1->SimpleText="开始连接...";
   //手机厂家
   if(!Check()){
      StatusBar1->SimpleText="电脑与手机连通失败!";
      Comm1->PortOpen=false;
      return false;
   }
   SmsRcv="";
   ByteSend.Length=8;
   for(i=0;i<8;i++)
      ByteSend[i]=AtCgmi[i];
   Comm1->OutputByte(ByteSend);
   Sleep(200);
   counter=0;
   while((p=SmsRcv.Pos("\r\nSIEMENS\r\n"))==0&&counter<100){
      Application->ProcessMessages();
      counter++;
   }
   if(counter>=100){
      StatusBar1->SimpleText="电脑与手机连通失败!";
      Comm1->PortOpen=false;
      return false;
   }
   ss=SmsRcv.SubString(p+2,7);
   StatusBar1->SimpleText="                          手机厂家: "+ss;
   //手机型号
   SmsRcv="";
   ByteSend.Length=8;
   for(i=0;i<8;i++)
      ByteSend[i]=AtCgmm[i];
   Comm1->OutputByte(ByteSend);
   Sleep(200);
   counter=0;
   while((p=SmsRcv.Pos("AT+CGMM"))==0&&counter<100){
      Application->ProcessMessages();
      counter++;
   }
   if(counter>=100){
      StatusBar1->SimpleText="电脑与手机连通失败!";
      Comm1->PortOpen=false;
      return false;
   }
   SmsRcv.Delete(1,p+9);
   while((p=SmsRcv.Pos("\r\nOK\r\n"))==0){
      Application->ProcessMessages();
   }
   ss=SmsRcv.SubString(1,p-1);
   StatusBar1->SimpleText=StatusBar1->SimpleText+"      手机型号: "+ss+"    连接就绪!";
   ByteSend.Length=0;
   return true;
}
//---------------------------------------------------------------------------

bool __fastcall TForm1::Check()
{
   int i,p,counter;
   char At[]="AT\r\n";
   DynamicArray<byte> ByteSend;

   ByteSend.Length=4;
   for(i=0;i<4;i++)
      ByteSend[i]=At[i];
   counter=0;
   while(1){
      SmsRcv="";
      Comm1->OutputByte(ByteSend);
      Application->ProcessMessages();
      Sleep(200);
      p=SmsRcv.Pos("\r\nOK\r\n");
      if(p){
         ByteSend.Length=0;
         return true;
      }
      counter++;
      if(counter>20)
         return false;
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::mNameInsClick(TObject *Sender)
{
   int i,j,k,rr;

   rr=sgClassName->Row;
   for(i=RowCount+1;i>rr+1;i--)
      sgClassName->Cells[1][i]=sgClassName->Cells[1][i-1];
   sgClassName->Cells[1][rr+1]="";
   for(i=RowCount-1;i>rr;i--)
      for(j=0;j<100;j++)
         for(k=0;k<3;k++)
            NameCode[i][j][k]=NameCode[i-1][j][k];
   for(j=0;j<100;j++)
      for(k=0;k<3;k++)
         NameCode[rr][j][k]="";
   for(i=1;i<RowCount+1;i++)
      for(j=1;j<4;j++)
         sgEdPhoncode->Cells[j][i]="";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::mNameDelClick(TObject *Sender)
{
   int i,j,k,rr;

   rr=sgClassName->Row;
   for(i=rr;i<RowCount+1;i++)
      sgClassName->Cells[1][i]=sgClassName->Cells[1][i+1];
   sgClassName->Cells[1][RowCount]="";
   for(i=rr-1;i<99;i++)
      for(j=0;j<100;j++)
         for(k=0;k<3;k++)
            NameCode[i][j][k]=NameCode[i+1][j][k];
   for(j=0;j<100;j++)
      for(k=0;k<3;k++)
         NameCode[99][j][k]="";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::sgClassNameClick(TObject *Sender)
{
   int i,j,CurRow;

   CurRow=sgClassName->Row;
   for(i=0;i<RowCount;i++)
      for(j=0;j<3;j++)
         sgEdPhoncode->Cells[j+1][i+1]=NameCode[CurRow-1][i][j];
}
//---------------------------------------------------------------------------

void __fastcall TForm1::sgEdPhoncodeDblClick(TObject *Sender)
{
   int CurCls,CurRow,CurCol;

   CurRow=sgEdPhoncode->Row;
   CurCol=sgEdPhoncode->Col;
   CurCls=sgClassName->Row;
   if(CurCls<1)
      CurCls=1;
   switch(CurCol){
      case 1:
         NameCode[CurCls-1][CurRow-1][0]=InputBox("输入学生姓名:  ","输入学生姓名",sgEdPhoncode->Cells[CurCol][CurRow]);
         sgEdPhoncode->Cells[CurCol][CurRow]=NameCode[CurCls-1][CurRow-1][0];
         break;
      case 2:
         NameCode[CurCls-1][CurRow-1][1]=InputBox("输入家长姓名:  ","输入家长姓名",sgEdPhoncode->Cells[CurCol][CurRow]);
         sgEdPhoncode->Cells[CurCol][CurRow]=NameCode[CurCls-1][CurRow-1][1];
         break;
      case 3:
         NameCode[CurCls-1][CurRow-1][2]=InputBox("输入电话号码:  ","输入电话号码",sgEdPhoncode->Cells[CurCol][CurRow]);
         sgEdPhoncode->Cells[CurCol][CurRow]=NameCode[CurCls-1][CurRow-1][2];
         break;
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::mCleanAllClick(TObject *Sender)
{
   lsRecv->Items->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::mSelectAllClick(TObject *Sender)
{
   int i,j;

   i=1;
   while(sgMpBook->Cells[1][i]!=""){
      lsRecv->Items->Add(sgMpBook->Cells[1][i]+"+"+sgMpBook->Cells[2][i]+"+"+sgMpBook->Cells[3][i]);
      if(lsRecv->Count<10)
         Label4->Caption ="接收短信人数为0"+IntToStr(lsRecv->Count)+"位";
      else
         Label4->Caption ="接收短信人数为"+IntToStr(lsRecv->Count)+"位";
      i++;
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::mCodeInsClick(TObject *Sender)
{
    int i,j,rr,zz;

   rr=sgEdPhoncode->Row;
   for(i=RowCount+1;i>rr+1;i--)
      for(j=1;j<4;j++){
         sgEdPhoncode->Cells[j][i]=sgEdPhoncode->Cells[j][i-1];
      }
   for(j=1;j<4;j++){
      sgEdPhoncode->Cells[j][rr+1]="";
   }
   zz=sgClassName->Row;
   if(zz<1)
      zz=1;
   for(i=RowCount-1;i>rr;i--){
      for(j=0;j<3;j++){
         NameCode[zz-1][i][j]=NameCode[zz-1][i-1][j];
      }
   }
   for(j=0;j<3;j++)
      NameCode[zz-1][rr][j]="";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::mCodeDelClick(TObject *Sender)
{
   int i,j,rr,zz;

   rr=sgEdPhoncode->Row;
   for(i=rr;i<RowCount+1;i++){
      for(j=1;j<4;j++){
         sgEdPhoncode->Cells[j][i]=sgEdPhoncode->Cells[j][i+1];
      }
   }
   for(j=1;j<4;j++){
      sgEdPhoncode->Cells[j][RowCount]="";
   }
   //
   zz=sgClassName->Row;
   if(zz<1)
      zz=1;
   //
   for(i=rr-1;i<RowCount-1;i++)
      for(j=0;j<3;j++)
         NameCode[zz-1][i][j]=NameCode[zz-1][i+1][j];
   for(j=0;j<3;j++)
      NameCode[zz-1][RowCount-1][j]="";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::mEdSmInsClick(TObject *Sender)
{
   int i,rr;

   rr=sgEdSmTxt->Row;
   for(i=RowCount+1;i>rr+1;i--)
      sgEdSmTxt->Cells[1][i]=sgEdSmTxt->Cells[1][i-1];
   sgEdSmTxt->Cells[1][rr+1]="";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -