分享
 
 
 

一个消息提示托盘程序的开发历程(采用socket技术,附源代码)三---客户端源代码

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

下面给出客户端程序,客户端相对复杂些,需要首先登陆,把登陆信息传给服务端,等待服务端的验证,如果验证通过,才

进入任务消息接受状态:

登陆:

Unit2.cpp

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

#include <vcl.h>

#pragma hdrstop

#include "Unit2.h"

#include "Msg.h"

#include <iniFiles.hpp>

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

#pragma package(smart_init)

#pragma resource "*.dfm"

TLoginForm *LoginForm;

String userName;

String serverIP;

String misurl;

int port;

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

__fastcall TLoginForm::TLoginForm(TComponent* Owner)

: TForm(Owner)

{

isPass=false;

}

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

void __fastcall TLoginForm::Button1Click(TObject *Sender)

{

if(EditName->Text.Trim().Length()>=30){

Application->MessageBox("用户名过长,请重新输入.",NULL,MB_OK);

}

else if(EditPwd->Text.Trim().Length()>=20){

Application->MessageBox("密码过过长,请重新输入.",NULL,MB_OK);

}

else if(inet_addr(EditServer->Text.c_str())==0xffffffff){

Application->MessageBox("IP地址设置有错,请重新设置.",NULL,MB_OK);

}

else{

ClientSocket1->Active=false;

ClientSocket1->Host=EditServer->Text;

ClientSocket1->Port=port;

ClientSocket1->Active=true;

}

}

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

void __fastcall TLoginForm::ClientSocket1Error(TObject *Sender,

TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)

{

Application->MessageBox("无法连接到服务器,请确认服务端程序是否开启.",NULL,MB_OK);

ErrorCode=0;

}

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

void __fastcall TLoginForm::ClientSocket1Connect(TObject *Sender,

TCustomWinSocket *Socket)

{

userName = EditName->Text.Trim();

serverIP = EditServer->Text.Trim();

LoginInfo *loginfo = new LoginInfo;

memcpy(loginfo->userID,userName.c_str(),userName.Length()+1);

memcpy(loginfo->pwd,EditPwd->Text.Trim().c_str(),EditPwd->Text.Trim().Length()+1);

ClientSocket1->Socket->SendBuf(loginfo,50);

}

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

void __fastcall TLoginForm::ClientSocket1Read(TObject *Sender,

TCustomWinSocket *Socket)

{

MESSAGEINFO *Msg = new MESSAGEINFO;

Socket->ReceiveBuf(Msg,244);

if(Msg->MsgType==0xA){

isPass=true;

//写如INI

char dir[MAX_PATH];

char *tfile = "/Config.ini";

//将程序现在目录所在输入此字符数组

::GetCurrentDirectory(MAX_PATH,dir);

//将现在目录加上指定文件名

strcat(dir,tfile);

TIniFile *ini = new TIniFile(dir);

try{

ini->WriteString("clientcfg","server",serverIP);

ini->WriteString("clientcfg","user",userName);

delete ini;

}

catch(...){

Application->MessageBoxA("写入配置文件config.ini失败!",NULL,MB_OK+16);

delete ini;

}

Close();

}

if(Msg->MsgType==0xB){

Application->MessageBox("用户名或密码错误!",NULL,MB_OK);

}

}

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

void __fastcall TLoginForm::FormClose(TObject *Sender,

TCloseAction &Action)

{

ClientSocket1->Active=false;

}

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

bool __fastcall TLoginForm::GetisPass()

{

//TODO: Add your source code here

return isPass;

}

void __fastcall TLoginForm::FormCreate(TObject *Sender)

{

char dir[MAX_PATH];

//----获取运行程序的目录开始

char *tfile = "/Config.ini";

//将程序现在目录所在输入此字符数组

::GetCurrentDirectory(MAX_PATH,dir);

//将现在目录加上指定文件名

strcat(dir,tfile);

//----获取运行程序的目录结束

TIniFile *ini = new TIniFile(dir);

try

{

EditName->Text = ini->ReadString("clientcfg","user","");

EditServer->Text = ini->ReadString("clientcfg","server","");

port = ini->ReadInteger("clientcfg","port",1555);

misurl = ini->ReadString("clientcfg","misurl","http://192.168.1.101:7001/webmis/Login/loginController.jpf");

delete ini;

}

catch(...)

{

MessageBox(Application->Handle,"读取配置文件config.ini失败!","信息提示!",MB_OK+MB_ICONINFORMATION+MB_SYSTEMMODAL);

delete ini;

}

}

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

void __fastcall TLoginForm::EditServerDblClick(TObject *Sender)

{

EditServer->ReadOnly = false;

EditServer->Font->Color = clBlack;

}

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

Unit2.h

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

#ifndef Unit2H

#define Unit2H

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

#include <Classes.hpp>

#include <Controls.hpp>

#include <StdCtrls.hpp>

#include <Forms.hpp>

#include <ScktComp.hpp>

#include <Mask.hpp>

#include <Buttons.hpp>

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

class TLoginForm : public TForm

{

__published: // IDE-managed Components

TButton *Button1;

TGroupBox *GroupBox1;

TLabel *Label1;

TLabel *Label2;

TEdit *EditName;

TEdit *EditPwd;

TLabel *Label3;

TEdit *EditServer;

TClientSocket *ClientSocket1;

void __fastcall Button1Click(TObject *Sender);

void __fastcall ClientSocket1Error(TObject *Sender,

TCustomWinSocket *Socket, TErrorEvent ErrorEvent,

int &ErrorCode);

void __fastcall ClientSocket1Connect(TObject *Sender,

TCustomWinSocket *Socket);

void __fastcall ClientSocket1Read(TObject *Sender,

TCustomWinSocket *Socket);

void __fastcall FormClose(TObject *Sender, TCloseAction &Action);

void __fastcall FormCreate(TObject *Sender);

void __fastcall EditServerDblClick(TObject *Sender);

private:

bool isPass;

public:

bool __fastcall GetisPass(); // User declarations

__fastcall TLoginForm(TComponent* Owner);

};

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

extern PACKAGE TLoginForm *LoginForm;

extern String userName; //用户

extern String serverIP;

extern int port;

extern String misurl;

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

#endif

登陆成功后,进入任务监听:

Unit1.cpp

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

#include <vcl.h>

#pragma hdrstop

#include "Unit1.h"

#include "Unit2.h"

#include "Unit3.h"

#include "Msg.h"

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

#pragma package(smart_init)

#pragma link "Trayicon"

#pragma resource "*.dfm"

TForm1 *Form1;

TList *MsgList = new TList;

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

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{

isCall=false;

if(serverIP.Length()>0){

Server=serverIP;

ClientSocket1->Host=Server;

ClientSocket1->Port=port;

ClientSocket1->Active=true;

btnconnect->Enabled=false;

btndisconnect->Enabled=true;

}else{

Application->Terminate();

}

StringGrid1->DefaultColWidth=50;

StringGrid1->ColWidths[2]=600;

StringGrid1->ColWidths[3]=100;

GroupBox1->Caption=userName+"的任务列表";

StringGrid1->Cells[0][0]="消息ID";

StringGrid1->Cells[1][0]="负责人";

StringGrid1->Cells[2][0]="任务名称";

StringGrid1->Cells[3][0]="时间";

}

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

void __fastcall TForm1::N2Click(TObject *Sender)

{

Application->Terminate();

}

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

void __fastcall TForm1::TrayIcon1Click(TObject *Sender)

{

if(isCall==true){

isCall=false;

TrayIcon1->IconIndex=0;

}

}

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

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

void __fastcall TForm1::Timer2Timer(TObject *Sender)

{

if((isCall==true)){

TrayIcon1->IconIndex=(GetTickCount()/500)%2;

}

}

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

void __fastcall TForm1::btnconnectClick(TObject *Sender)

{

if(InputQuery("连接到服务器","输入服务器地址:",serverIP)){

if(inet_addr(serverIP.c_str())==0xffffffff){

Application->MessageBox("IP地址设置有错,请重新设置.",NULL,MB_OK);

}

else{

ClientSocket1->Host=serverIP;

ClientSocket1->Active=true;

btnconnect->Enabled=false;

btndisconnect->Enabled=true;

}

}

}

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

void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,

TCustomWinSocket *Socket)

{

StatusBar1->SimpleText="连接到:"+Server;

TrayIcon1->IconIndex = 0;

}

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

void __fastcall TForm1::ClientSocket1Read(TObject *Sender,

TCustomWinSocket *Socket)

{

//接受服务器端传来的MESSAGE;

MESSAGEINFO *Msg = new MESSAGEINFO;

Socket->ReceiveBuf(Msg,244);

if((Msg->MsgType==0xC)&&((String)Msg->UsrCode==userName)){

//bool isAdd=true;

MsgList->Add(Msg);

/*

for(int i=1;i<=StringGrid1->RowCount;i++){

if(StringGrid1->Cells[0][i]==(String)Msg->MessageID){

isAdd=false;

}

}

if(isAdd==true){

isCall=true;

}

*/

}

if(Msg->MsgType==0xD){ //一次轮询结束

//Label1->Caption = MsgList->Count+1;

if((MsgList->Count+1)>StringGrid1->RowCount) isCall = true;

if((MsgList->Count+1)!=StringGrid1->RowCount){

StringGrid1->RowCount=0;

for (int i = 0; i < MsgList->Count; i++) {

int rowcount=StringGrid1->RowCount;

StringGrid1->Cells[0][rowcount]=((MESSAGEINFO *)MsgList->Items[i])->MessageID;

StringGrid1->Cells[1][rowcount]=((MESSAGEINFO *)MsgList->Items[i])->UsrCode;

StringGrid1->Cells[2][rowcount]=((MESSAGEINFO *)MsgList->Items[i])->TastName;

StringGrid1->Cells[3][rowcount]=((MESSAGEINFO *)MsgList->Items[i])->CreatTime;

StringGrid1->RowCount++;

}

}

MsgList=NULL;

MsgList = new TList;

Label1->Caption = "您的任务数:"+IntToStr(StringGrid1->RowCount-1);

}

}

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

void __fastcall TForm1::ClientSocket1Disconnect(TObject *Sender,

TCustomWinSocket *Socket)

{

btnconnect->Enabled=true;

btndisconnect->Enabled=false;

StatusBar1->SimpleText="无法连接到服务端!";

TrayIcon1->IconIndex = 2;

}

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

void __fastcall TForm1::btndisconnectClick(TObject *Sender)

{

ClientSocket1->Close();

btnconnect->Enabled=true;

btndisconnect->Enabled=false;

}

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

void __fastcall TForm1::ClientSocket1Error(TObject *Sender,

TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)

{

StatusBar1->SimpleText="无法连接到:"+serverIP;

ErrorCode=0;

btndisconnect->Enabled=true;

btnconnect->Enabled=false;

}

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

//判断某人的任务数

TForm1::StrCount(String substing, String SourceStr)

{

//TODO: Add your source code here

int sp = substing.Length();

int pos = SourceStr.Pos(substing);

String tmp;

int capa=0;

while( pos > 0 )

{

tmp=SourceStr.SubString(1,pos-1);

capa++;

SourceStr=SourceStr.SubString(pos+sp,SourceStr.Length()-pos-sp+1);

pos=SourceStr.Pos(substing);

}

return capa;

}

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)

{

ClientSocket1->Close();

}

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

void __fastcall TForm1::FormClick(TObject *Sender)

{

delete MsgList;

}

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

void __fastcall TForm1::N3Click(TObject *Sender)

{

N3->Enabled=false;

FormAbout->ShowModal();

N3->Enabled=true;

}

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

void __fastcall TForm1::Image1Click(TObject *Sender)

{

ShellExecute(Handle,NULL,misurl.c_str(),NULL,NULL,SW_SHOWNORMAL);

}

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

void __fastcall TForm1::FormShow(TObject *Sender)

{

if(!TrayIcon1->Visible)

TrayIcon1->Visible=true;

TrayIcon1->Minimize();

}

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

Unit1.h

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

#ifndef Unit1H

#define Unit1H

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

#include <Classes.hpp>

#include <Controls.hpp>

#include <StdCtrls.hpp>

#include <Forms.hpp>

#include "Trayicon.h"

#include <ImgList.hpp>

#include <Menus.hpp>

#include <ADODB.hpp>

#include <Db.hpp>

#include <DBGrids.hpp>

#include <ExtCtrls.hpp>

#include <Grids.hpp>

#include <ScktComp.hpp>

#include <ComCtrls.hpp>

#include <Graphics.hpp>

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

class TForm1 : public TForm

{

__published: // IDE-managed Components

TTrayIcon *TrayIcon1;

TPopupMenu *PopupMenu1;

TMenuItem *N2;

TMenuItem *N3;

TImageList *ImageList1;

TTimer *Timer2;

TGroupBox *GroupBox1;

TClientSocket *ClientSocket1;

TButton *btnconnect;

TButton *btndisconnect;

TStatusBar *StatusBar1;

TStringGrid *StringGrid1;

TImage *Image1;

TLabel *Label1;

void __fastcall N2Click(TObject *Sender);

void __fastcall TrayIcon1Click(TObject *Sender);

void __fastcall Timer2Timer(TObject *Sender);

void __fastcall btnconnectClick(TObject *Sender);

void __fastcall ClientSocket1Connect(TObject *Sender,

TCustomWinSocket *Socket);

void __fastcall ClientSocket1Read(TObject *Sender,

TCustomWinSocket *Socket);

void __fastcall ClientSocket1Disconnect(TObject *Sender,

TCustomWinSocket *Socket);

void __fastcall btndisconnectClick(TObject *Sender);

void __fastcall ClientSocket1Error(TObject *Sender,

TCustomWinSocket *Socket, TErrorEvent ErrorEvent,

int &ErrorCode);

void __fastcall FormClose(TObject *Sender, TCloseAction &Action);

void __fastcall FormClick(TObject *Sender);

void __fastcall N3Click(TObject *Sender);

void __fastcall Image1Click(TObject *Sender);

void __fastcall FormShow(TObject *Sender);

private: // User declarations

bool isCall;

int capa; //任务数

String Server;

public: // User declarations

__fastcall TForm1(TComponent* Owner);

StrCount(String substing, String SourceStr);

};

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

extern PACKAGE TForm1 *Form1;

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

#endif

然后是about界面:

Unit3.cpp

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

#include <vcl.h>

#pragma hdrstop

#include "Unit3.h"

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

#pragma package(smart_init)

#pragma resource "*.dfm"

TFormAbout *FormAbout;

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

__fastcall TFormAbout::TFormAbout(TComponent* Owner)

: TForm(Owner)

{

}

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

void __fastcall TFormAbout::Button1Click(TObject *Sender)

{

ModalResult = mrOk;

}

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

Unit3.h

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

#ifndef Unit3H

#define Unit3H

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

#include <Classes.hpp>

#include <Controls.hpp>

#include <StdCtrls.hpp>

#include <Forms.hpp>

#include <ExtCtrls.hpp>

#include <Graphics.hpp>

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

class TFormAbout : public TForm

{

__published: // IDE-managed Components

TButton *Button1;

TImage *Image1;

TLabel *Label2;

TLabel *Label3;

TLabel *Label1;

TLabel *Label4;

void __fastcall Button1Click(TObject *Sender);

private: // User declarations

public: // User declarations

__fastcall TFormAbout(TComponent* Owner);

};

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

extern PACKAGE TFormAbout *FormAbout;

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

#endif

另外,登陆界面和任务监听主界面如何出现,需要在project中做如下修改(模式对话框)

project2.cpp

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

#include <vcl.h>

#include "Unit2.h"

#pragma hdrstop

USERES("Project2.res");

USEFORM("Unit1.cpp", Form1);

USEFORM("Unit2.cpp", LoginForm);

USEFORM("Unit3.cpp", FormAbout);

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

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)

{

try

{

Application->Initialize();

TLoginForm *f = new TLoginForm(Application);

f->ShowModal();

if(!f->GetisPass()){

delete f;

return 0;

}

Application->CreateForm(__classid(TForm1), &Form1);

// Application->CreateForm(__classid(TLoginForm), &LoginForm);

Application->CreateForm(__classid(TFormAbout), &FormAbout);

Application->Run();

}

catch (Exception &exception)

{

Application->ShowException(&exception);

}

return 0;

}

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

另外,客户端的配置文件Config.ini内容如下:

[clientcfg]

user=yinhm

server=192.168.1.55

port=1555

misurl=http://192.168.1.101:7001/webmis/Login/loginController.jpf

以上程序在bcb5编译通过。

以上的东东一是给自己存个档,二是希望对各位有所帮助,并希望牛人批评指正。

马上就要过年了,顺祝大家新年快乐,幸福吉祥

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有