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

📄 unit1.cpp

📁 网络编程的一些实例
💻 CPP
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    //下面这两个布尔变量用于在UUEncoding/UUDecoding代码段中决定
    //是否用户输入了有些的文件名
    ValidInputFile = false;
    ValidOutputFile = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BtURLEncodeClick(TObject *Sender)
{
    NMURL1->InputString = URLEdit->Text;
    Output->Text = NMURL1->Encode;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BtURLDecodeClick(TObject *Sender)
{
    NMURL1->InputString = URLEdit->Text;
    Output->Text = NMURL1->Decode;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BtUUEncodeClick(TObject *Sender)
{
    if((!ValidInputFile) || (!ValidOutputFile))
      {
        MessageBox(NULL, "There must be valid input for files.",
            "Invalid input recieved", MB_ICONERROR);
        return;
    }

    //编码用到的文件流
    TFileStream *InputStrm = new TFileStream(InFileEdit->Text, fmOpenRead);
    TFileStream *OutputStrm = new TFileStream(OutFileEdit->Text, fmCreate |
                fmOpenReadWrite);

    //设置控件的属性
    if(RadioGroup1->ItemIndex==0)
      //使用MIME编码
      NMUUProcessor1->Method = uuMime;
    else
      //使用CODE编码
      NMUUProcessor1->Method = uuCode;
    NMUUProcessor1->InputStream = InputStrm;
    NMUUProcessor1->OutputStream = OutputStrm;

    //把文件编码到文件流中
    NMUUProcessor1->Encode();

    //为将来的显示需要重新设置输出流
    OutputStrm->Seek(soFromBeginning, 0);

    //下面的代码段使用缓冲区转换一文件流
    {
       char *WriteBuffer = new char[OutputStrm->Size];

       OutputStrm->Read(WriteBuffer, OutputStrm->Size);
       //应该首先清空输出,但是这个分配重新设置了输出
       //因此不用首先清空它
       Output->Text = WriteBuffer;

       delete WriteBuffer;
    }

    //清空文件流
    delete OutputStrm;
    delete InputStrm;

    // 通知用户解码文件成功
    MessageBox(NULL, "解码文件成功", "解码成功", NULL);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BtUUDecodeClick(TObject *Sender)
{
    if((!ValidInputFile) || (!ValidOutputFile))
      {
       MessageBox(NULL, "There must be valid input for files.",
            "Invalid input recieved", MB_ICONERROR);
       return;
      }

    // 用于解码的文件流
    TFileStream *InputStrm = new TFileStream(InFileEdit->Text, fmOpenRead);
    TFileStream *OutputStrm = new TFileStream(OutFileEdit->Text, fmCreate |
                fmOpenReadWrite);

    // 设置控件的属性
    if(RadioGroup1->ItemIndex==0)
      //使用MIME解码
      NMUUProcessor1->Method = uuMime;
    else
      //使用CODE解码
      NMUUProcessor1->Method = uuCode;
    NMUUProcessor1->InputStream = InputStrm;
    NMUUProcessor1->OutputStream = OutputStrm;

    // 把文件解码到文件流中
    NMUUProcessor1->Decode();

    //为将来的显示需要重新设置输出流
    OutputStrm->Seek(soFromBeginning, 0);

    //下面的代码段使用缓冲区转换一文件流
    {
     char *WriteBuffer = new char[OutputStrm->Size];

     OutputStrm->Read(WriteBuffer, OutputStrm->Size);
     //应该首先清空输出,但是这个分配重新设置了输出
     //因此不用首先清空它
     Output->Text = WriteBuffer;

     delete WriteBuffer;
    }

    // 清空输出、输入文件流
    delete OutputStrm;
    delete InputStrm;

    // 通知用户完成对文件的解码
    MessageBox(NULL, "解码文件成功", "解码成功", NULL);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BtInputFileClick(TObject *Sender)
{
   if(OpenDialog1->Execute())
     {
      InFileEdit->Text = OpenDialog1->FileName;
      ValidInputFile = true;
     }
   else
     {
      InFileEdit->Text = "";
      ValidInputFile = false;
     }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BtOutputFileClick(TObject *Sender)
{
  if(OpenDialog1->Execute())
    {
     OutFileEdit->Text = OpenDialog1->FileName;
     ValidOutputFile = true;
    }
  else
    {
     OutFileEdit->Text = "";
     ValidOutputFile = false;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMUUProcessor1EndDecode(TObject *Sender)
{
 StatusBar1->SimpleText="完成解码";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMUUProcessor1BeginDecode(TObject *Sender)
{
 StatusBar1->SimpleText="开始解码";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMUUProcessor1BeginEncode(TObject *Sender)
{
 StatusBar1->SimpleText="开始编码";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMUUProcessor1EndEncode(TObject *Sender)
{
  StatusBar1->SimpleText="完成编码";
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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