在C++ Builder中编写发送图象文件措施
副标题#e#
ICQ可以或许实现点到点的数据传送,但假如想把一个多媒体图像文件送给另一方,是较量难的。但可以操作C++Builder 5.0 的控件来编写一个用于发送图像的应用措施。
打开C++Builder 5.0 ,新建一个工程,在Form1中添加NMStrm,NMStrmServ,OpenPictureDialog控件,再添;加Button控件3个,Label控件1个,Edit控件 2 个,StatusBar控件1个。
首先我们来认识一下NMStrm控件,NMStrmServ控件和OpenPictureDialog控件。NMStrm控件是一个基于TCP/IP的控件,它可以吸收数据流,然后通过网络将其发送给处事器。NMStrmServ控件是可以吸收从客户端发送来的数据流,处事器只监听TCP/IP 端口,不认真监听UDP端口。OpenPictureDialog控件是用于打开图像文件举办欣赏或编辑。
各类控件的属性设计如下:
控件名 属性 值
Form Caption 发送图像
Image Name Image1
Button name=Button1,Caption=发送
Button name=Button2,Caption=显示
Button name=Button3,Caption=打开
Label name=Label1,Caption=长途主机:
Edit name Edit1
Edit name Edit2
NMStrm name NMStrm1
NMStrmServ name NMStrmServ1
OpenPictureDialog name OpenPictureDialog1
StatusBar name StatusBar1
#p#副标题#e#
源措施代码如下:
//“Unit1.h"的源代码
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//----------------------------------------- -------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <Dialogs.hpp>
#include <ExtCtrls.hpp>
#include <ExtDlgs.hpp>
#include <NMSTRM.hpp>
#include <Psock.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TNMStrm *NMStrm1;
TNMStrmServ *NMStrmServ1;
TOpenPictureDialog *OpenPictureDialog1;
TImage *Image1;
TLabel *Label1;
TEdit *Edit1;
TButton *Button3;
TEdit *Edit2;
TStatusBar *StatusBar1;
void __fastcall Button3Click(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall NMStrm1Connect(TObject *Sender);
void __fastcall NMStrm1ConnectionFailed(TObject *Sender);
void __fastcall NMStrm1Disconnect(TObject *Sender);
void __fastcall NMStrm1HostResolved(TComponent *Sender);
void __fastcall NMStrm1MessageSent(TObject *Sender);
void __fastcall NMStrm1PacketSent(TObject *Sender);
void __fastcall NMStrm1Status(TComponent *Sender, AnsiString Status);
void __fastcall NMStrm1InvalidHost(bool &Handled);
void __fastcall NMStrmServ1ClientContact(TObject *Sender);
void __fastcall NMStrmServ1MSG(TComponent *Sender,
const AnsiString sFrom, TStream *strm);
void __fastcall NMStrmServ1Status(TComponent *Sender,
AnsiString Status);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//“Unit1.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::Button3Click(TObject *Sender)
{
if (OpenPictureDialog1->Execute())
Edit2->Text =OpenPictureDialog1->FileName ;
}
//---------------------------------------------------------------------------
//发送图像文件
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TFileStream *MyFStream;
MyFStream = new TFileStream(OpenPictureDialog1->FileName, fmOpenRead);
try
{ NMStrm1->Host=Edit1->Text; //指定主机名
NMStrm1->PostIt(MyFStream); //发送的文件
}
catch(...){}
}
//---------------------------------------------------------------------------
//显示图像文件
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Image1->Picture->LoadFromFile(".\\tmp.bmp");
}
//---------------------------------------------------------------------------
//毗连到长途主机
void __fastcall TForm1::NMStrm1Connect(TObject *Sender)
{
StatusBar1->SimpleText = "Connected";
}
//---------------------------------------------------------------------------
//没有毗连到主机时发生此事件
void __fastcall TForm1::NMStrm1ConnectionFailed(TObject *Sender)
{
ShowMessage("无法毗连到对方");
}
//---------------------------------------------------------------------------
//在客户端文件发送到时发生此事件
void __fastcall TForm1::NMStrm1Disconnect(TObject *Sender)
{
if (StatusBar1 != 0)
StatusBar1->SimpleText = "文件发送到了";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMStrm1HostResolved(TComponent *Sender)
{
StatusBar1->SimpleText = "Host Resolved";
}
//---------------------------------------------------------------------------
//当文件发送到对方时发生此事件
void __fastcall TForm1::NMStrm1MessageSent(TObject *Sender)
{
ShowMessage("有文件发送到对方");
}
//---------------------------------------------------------------------------
//数据发送到对方时发生此事件
void __fastcall TForm1::NMStrm1PacketSent(TObject *Sender)
{
StatusBar1->SimpleText = IntToStr(NMStrm1->BytesRecvd)+" bytes of "
+NMStrm1->BytesTotal+" sent";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMStrm1Status(TComponent *Sender,AnsiString Status)
{
if (StatusBar1 != 0)
StatusBar1->SimpleText = Status;
}
//---------------------------------------------------------------------------
//没有主机名时发生此事件
void __fastcall TForm1::NMStrm1InvalidHost(bool &Handled)
{
AnsiString NewHost;
if (InputQuery("没有主机", "请输入要发送的主机名:", NewHost))
{
NMStrm1->Host = NewHost;
Edit1->Text=NewHost;
}
}
//---------------------------------------------------------------------------
//暗示客户机已毗连随处事器了
void __fastcall TForm1::NMStrmServ1ClientContact(TObject *Sender)
{
StatusBar1->SimpleText = "Client Contacted";
}
//---------------------------------------------------------------------------
//吸收客户端发送来的文件
void __fastcall TForm1::NMStrmServ1MSG(TComponent *Sender,
const AnsiString sFrom, TStream *strm)
{
if (FileExists(".\\tmp.bmp"))
DeleteFile(".\\tmp.bmp");
TFileStream *MyFStream;
MyFStream = new TFileStream(".\\tmp.bmp", fmCreate);
try
{
MyFStream->CopyFrom(strm, strm->Size);
}
catch(...)
{
}
MyFStream->Free();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMStrmServ1Status(TComponent *Sender,
AnsiString Status)
{
if (StatusBar1 != 0)
StatusBar1->SimpleText = Status;
}
//---------------------------------------------------------------------------
#p#分页标题#e#
编好代码今后,编译源措施,在两台计较机上运行它就行了,每一方都可以做处事器或事情站,只要对方运行了该措施,都可以向其发送图象文件,也可以在单机上操纵,快来试试吧!