本文講述了如何用C++Builder實現Messenger的詳細方法,其中使用了如下技術:
1. 多線程序技術,防止界面出現死鎖。
2. 使用了網絡枚舉來枚舉在線的網絡用戶。
3. 使用了Unicode的轉換函數進從AnsiString到Unicode的轉換。
4. 使用了簡單的不規則窗口的技術。
5. 自動安裝,將自己加入系統起動中。
6. 系統托盤技術。
編程方法:
1. 如圖所示在窗體上加入的有的控件:TListView,TTreeView,TMemo,菜單、托盤、按鈕、狀態欄
2. 針對各控件按本文所寫加入代碼即可。
3. 以下是發送函數
void __fastcall TFormMain::ButtonSendClick(TObject *Sender)
{
AnsiString Message,UserName,FromName;
TListItem *ListItem;
if(CheckBoxAnony->Checked)FromName=EditAnony->Text;
else FromName="";
Message=Memo1->Text;
if(Message==""){ShowMessage("Please Enter words to send");return;}
if(!ListView1->SelCount){
if(TreeView1->Selected==NULL){ShowMessage("Please select destnation");return;}
UserName=TreeView1->Selected->Text;
if(TreeView1->Selected->Level==1)UserName=UserName+"*";
new NetMessageSend(false,UserName,FromName,Message,CheckBoxGetRet->Checked);
return;
}
ListItem=ListView1->Selected;
UserName=ListItem->Caption;
if(TreeView1->Selected->Level==0)UserName+="*";
while(UserName[1]=='\\')UserName=UserName.SubString(2,UserName.Length()-1);
new NetMessageSend(false,UserName,FromName,Message,CheckBoxGetRet->Checked);
for(int i=1;iSelCount;i++){
ListItem=ListView1->GetNextItem(ListItem,sdAll,TItemStates()< UserName=ListItem->Caption;
if(TreeView1->Selected->Level==0)UserName+="*";
while(UserName[1]=='\\')UserName=UserName.SubString(2,UserName.Length()-1);
new NetMessageSend(false,UserName,FromName,Message,CheckBoxGetRet->Checked);
}
}
以下是網絡結點枚舉函數,是個線程
bool __fastcall EnumNetResource::GetNetRes(TTreeNodes *Nodes,TTreeNode * Node,NETRESOURCE *pNetResource,int Depth)
{
#define ENUMBUFSIZE (16*1024)
#define ENUMCOUNT 10
NETRESOURCE buff[1024];
AnsiString UserName;
if(Terminated) return false;
if(Nodes==NULL) return false;
if(Depth==0) return true;
HANDLE hEnum;
bool reValue=true;
NETRESOURCE *pNetRes,NetRes;
if(WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,0,
pNetResource,&hEnum)!=NO_ERROR)return false;
DWORD ECount=ENUMCOUNT;
DWORD ESize=ENUMBUFSIZE;
DWORD Error_num;
do{
Error_num=WNetEnumResource(hEnum,&ECount,buff,&ESize);
if (ESize>ENUMBUFSIZE) return false;
if (Error_num==NO_ERROR){
for (DWORD num=0;num if(Terminated)return false;
pNetRes=&buff[num];
file://if(Node)Node->ImageIndex=1-Depth;
UserName=AnsiString(pNetRes->lpRemoteName);
while(UserName[1]=='\\')
UserName=UserName.SubString(2,UserName.Length()-1);
TTreeNode *tNode=Nodes->AddChild(Node,UserName);
if(tNode)
switch(pNetRes->dwType){
case RESOURCETYPE_ANY:
switch(tNode->Level){
case 0:tNode->ImageIndex=1;break;
case 1:
case 2:tNode->ImageIndex=2;break;
}
break;
case RESOURCETYPE_DISK:tNode->ImageIndex=3;break;
case RESOURCETYPE_PRINT:tNode->ImageIndex=4;break;
}
if(pNetRes->dwDisplayType!=RESOURCEDISPLAYTYPE_SHARE){
if(!GetNetRes(Nodes,tNode,pNetRes,Depth-1)) reValue=false;
}
}
}
}while(ECount==ENUMCOUNT);
if (Error_num!=ERROR_NO_MORE_ITEMS && Error_num!=NO_ERROR) reValue=false;
WNetCloseEnum(hEnum);
return reValue;
}
以下是多線程發送函數,是個線程
void __fastcall NetMessageSend::Execute()
{
file://---- Place thread code here ----
const maxlen=1600;
wchar_t *pMsgBuff;
AnsiString retstr;
long int retvalue,msglen,sendlen;
msglen=FMessage.WideCharBufSize();
pMsgBuff=(wchar_t *)malloc(msglen*2+10);
FMessage.WideChar(pMsgBuff,msglen);
for(long int i=0;i if((msglen*2-i)>maxlen)sendlen=maxlen;
else sendlen=msglen*2-i;
retvalue=NetMessageBufferSend(NULL,FUserName,pFromName,(char *)pMsgBuff+i,sendlen);
}
if(FGetRet){
switch(retvalue){
case NERR_Success:
retstr="@_@ Sent OK!";
break;
case ERROR_ACCESS_DENIED:
retstr=":( Has no access";
break;
case ERROR_INVALID_PARAMETER:
retstr=":( Parameter error.";
break;
case ERROR_NOT_SUPPORTED:
retstr=":( Network not supported.";
break;
case NERR_NameNotFound:
retstr=":( Parameter is invalid.";
break;
case NERR_NetworkError:
retstr=":( General failure network hardware.";
break;
default:
retstr=":( Unknow error";
break;
}
Application->MessageBox(retstr.c_str(),("Result for "+AnsiString(FUserName)).c_str(),MB_OK);
}
free(pMsgBuff);
}
本文代碼在C++ Builder6.0,Windows2000專業版下實現。