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

📄 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)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMNNTP1Connect(TObject *Sender)
{
 StatusBar1->SimpleText="已建立连接!";
 NMNNTP1->GetGroupList();
 Memo1->Lines->Assign(NMNNTP1->GroupList);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 if(Button1->Caption=="连接")
    {
     NMNNTP1->Host=Edit2->Text;
     NMNNTP1->Port=StrToInt(Edit4->Text);
     NMNNTP1->Connect();
     Button1->Caption="断开连接";
    }
 else
    {
     NMNNTP1->Disconnect();
     Button1->Caption="连接";
     Memo1->Text="";
     Memo2->Text="";
     Memo3->Text="";
     Edit1->Text="";
     Edit3->Text="";
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMNNTP1Article(TObject *Sender)
{
 Memo2->Text=NMNNTP1->Body->Text;
 Memo3->Text=NMNNTP1->Header->Text;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMNNTP1GroupSelect(TObject *Sender)
{
 Form1->Caption = "NNTP Demo - ["+NMNNTP1->SelectedGroup+" Lo:"+IntToStr(NMNNTP1->LoMessage)+" Hi:"+IntToStr(NMNNTP1->HiMessage)+"]";
  if (NMNNTP1->Posting)
    Form1->Caption = Form1->Caption + " Posting Allowed]";
  else
    Form1->Caption = Form1->Caption + " Posting Prohibited]";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
 NMNNTP1->SetGroup(Edit1->Text);
 Edit3->Text="";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 NMNNTP1->GetArticle(StrToInt(Edit3->Text));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMNNTP1InvalidArticle(TObject *Sender)
{
  ShowMessage("Article number invalid");
  
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit2KeyPress(TObject *Sender, char &Key)
{
 if(Key==13)
   {
    NMNNTP1->Host=Edit2->Text;
    NMNNTP1->Port=StrToInt(Edit4->Text);
    NMNNTP1->Connect();
    Button1->Caption="断开连接";
   }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
 if(Key==13)
   {
    NMNNTP1->SetGroup(Edit1->Text);
    Edit3->Text="";
   }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit3KeyPress(TObject *Sender, char &Key)
{
 if(Key==13)
   NMNNTP1->GetArticle(StrToInt(Edit3->Text));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMNNTP1Status(TComponent *Sender,
      AnsiString Status)
{
 if(StatusBar1!=0)
   StatusBar1->SimpleText=Status;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMNNTP1Disconnect(TObject *Sender)
{
 StatusBar1->SimpleText="已断开连接";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Memo1DblClick(TObject *Sender)
{
 try
   {
    NMNNTP1->SetGroup(Memo1->SelText);
    Edit1->Text=Memo1->SelText;
    Edit3->Text="";
   }
 catch(...)
   {
    ShowMessage("所选择的不是新闻组!请重试!");
   }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMNNTP1ConnectionFailed(TObject *Sender)
{
 ShowMessage("连接失败!");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::NMNNTP1Abort(TObject *Sender)
{
 ShowMessage("已被终止");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::NMNNTP1AuthenticationNeeded(bool &Handled)
{
 AnsiString AnID, APass;
 if(NMNNTP1->UserId == "")
   if(InputQuery("需要验证帐号", "请输入用户ID", AnID))
     {
      NMNNTP1->UserId = AnID;
      Handled = TRUE;
     }
 if(NMNNTP1->Password == "")
   if(InputQuery("需要验证帐号", "请输入口令", APass))
     {
      NMNNTP1->Password = APass;
      Handled = TRUE;
     }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::NMNNTP1ConnectionRequired(bool &Handled)
{
 AnsiString S;
 if(InputQuery("要求选择一新闻组", "请输入一新闻组名:", S))
   {
    NMNNTP1->SetGroup(S);
    Handled = TRUE;
   }

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

void __fastcall TForm1::NMNNTP1HostResolved(TComponent *Sender)
{
 StatusBar1->SimpleText="完成解析域名";        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::NMNNTP1InvalidHost(bool &Handled)
{
 AnsiString TmpStr;
 if(InputQuery("主机名不正确!", "指定一新主机名:", TmpStr))
   {
    NMNNTP1->Host = TmpStr;
    Handled = TRUE;
   }
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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