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

📄 bqqdlg.cpp

📁 该源代码实现了局域网内的信息传递、文件传输
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	GetDlgItem(IDC_SEND)->EnableWindow(TRUE);
	GetDlgItem(IDC_INOUT)->SetWindowText("");
	((CBQQApp*)AfxGetApp())->tmpMsg ="";
	((CEdit*)GetDlgItem(IDC_INOUT))->SetReadOnly(FALSE);
	((CButton*)GetDlgItem(IDC_INCLUDEATTACH))->SetCheck(BST_UNCHECKED);
	((CButton*)GetDlgItem(IDC_INCLUDEATTACH))->EnableWindow(FALSE);

	if(!sendTarget.IsEmpty())
	{
		if(hasAttachment)
		{
			hasAttachment = FALSE;
			CString prom="";
			prom.Format("不能向%s发送附件!可能对方没有装本信使!",sendTarget);
			MessageBox(prom,"提示",MB_ICONINFORMATION|MB_OK);	
		}
		//else
			AfxBeginThread(SendData,this);
	}
	((CEdit*)GetDlgItem(IDC_ADDRLIST))->SetWindowText("");
	((CDimEditCtrl*)GetDlgItem(IDC_ADDRLIST))->SetShowDimControl( true );
	GetDlgItem(IDC_SEND)->EnableWindow(TRUE);
	
}

void CBQQDlg::OnAddress() 
{
	// TODO: Add your control notification handler code here
	CMyAddrDlg myAddrDlg;
	if(myAddrDlg.DoModal()==IDOK)
	{
		((CDimEditCtrl*)GetDlgItem(IDC_ADDRLIST))->SetShowDimControl( false );
		int len = GetDlgItem(IDC_ADDRLIST)->GetWindowTextLength();
		((CDimEditCtrl*)GetDlgItem(IDC_ADDRLIST))->SetSel(len,len);
		((CDimEditCtrl*)GetDlgItem(IDC_ADDRLIST))->ReplaceSel(myAddrDlg.sendStr);
		CString tmpAddr ="";
		((CDimEditCtrl*)GetDlgItem(IDC_ADDRLIST))->GetWindowText(tmpAddr);
		if(tmpAddr.Find(';')==0)
		{
			tmpAddr = tmpAddr.Mid(1);
			((CDimEditCtrl*)GetDlgItem(IDC_ADDRLIST))->SetWindowText(LPCTSTR(tmpAddr));
		}
	}
}
LRESULT CBQQDlg::OnLiben(WPARAM wParam, LPARAM lParam)//reload the window
{
	UINT uID;//发出该消息的图标的ID
     UINT uMouseMsg;//鼠标动作
     POINT pt;
     uID=(UINT) wParam;
     uMouseMsg=(UINT) lParam;
     if(uMouseMsg==WM_LBUTTONDOWN)//如果是
     {
        switch(uID)
       {
         case IDR_MAINFRAME://如果是我们的图标
              GetCursorPos(&pt);//取得鼠标位置
              //…//执行相应操作
			  	AnimateWindow(this->GetSafeHwnd(),400,AW_HOR_POSITIVE|AW_SLIDE|AW_VER_NEGATIVE|AW_CENTER|AW_ACTIVATE);
		
				NOTIFYICONDATA tnd; 
				tnd.cbSize=sizeof(NOTIFYICONDATA);
				tnd.hWnd=this->m_hWnd;
				tnd.uID=IDR_MAINFRAME;
				tnd.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
				tnd.uCallbackMessage=WM_LIBEN;
				tnd.hIcon=LoadIcon(AfxGetInstanceHandle(),
				MAKEINTRESOURCE(IDR_MAINFRAME)); 
				strcpy(tnd.szTip,"Q.QQ");
				Shell_NotifyIcon(NIM_DELETE,&tnd);
				((CBQQApp*)AfxGetApp())->m_pMainWnd->ModifyStyleEx(SW_HIDE,SW_SHOW, SWP_NOMOVE | SWP_NOSIZE| SWP_NOZORDER);
		 		((CBQQApp*)AfxGetApp())->m_pMainWnd->ShowWindow(SW_SHOW);
				((CBQQApp*)AfxGetApp())->m_pMainWnd->SetActiveWindow();
				GetDlgItem(IDC_INOUT)->SetFocus();
				((CBQQApp*)AfxGetApp())->m_pMainWnd->SetForegroundWindow();
         break;
         //…
         default:;
         //…
       }
     }
	 
     return 0; 
}
BOOL CBQQDlg::HidenIt()//minimize to systray
{
	NOTIFYICONDATA tnd; 
	tnd.cbSize=sizeof(NOTIFYICONDATA);
	tnd.hWnd=this->m_hWnd;
	tnd.uID=IDR_MAINFRAME;
	tnd.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
	tnd.uCallbackMessage=WM_LIBEN;
	tnd.hIcon=LoadIcon(AfxGetInstanceHandle(),
	MAKEINTRESOURCE(IDR_MAINFRAME)); 
	strcpy(tnd.szTip,"Q.QQ");
	Shell_NotifyIcon(NIM_ADD,&tnd);
	AnimateWindow(this->GetSafeHwnd(),400,AW_BLEND|AW_HIDE|AW_HOR_NEGATIVE|AW_VER_POSITIVE);

	((CBQQApp*)AfxGetApp())->m_pMainWnd->ModifyStyleEx(SW_SHOW,SW_HIDE, SWP_NOMOVE | SWP_NOSIZE);
	((CBQQApp*)AfxGetApp())->m_pMainWnd->ShowWindow(SW_HIDE);
	return TRUE;
}
void CBQQDlg::OnSetMessage(WPARAM size, LPARAM lParam)
{
	SetMessage(g_buffer, (DWORD)size);
}
void CBQQDlg::SetMessage(LPBYTE buffer, DWORD dwNumberOfBytesRead)
{
	 /////error
	TCHAR content[MAX_BUFFER_SIZE+1];
	CHAR from[MAX_NAME_SIZE+1];
	CHAR to[MAX_NAME_SIZE+1];
	strcpy(from, (LPCSTR)buffer);
	int pos = strlen(from) + 1;
	strcpy(to, (LPCSTR)buffer + pos);
	pos += strlen(to) + 1;
	memset(content, 0 , MAX_BUFFER_SIZE+1);
	memcpy(content, (LPCSTR)buffer + pos, dwNumberOfBytesRead - pos);

	CString tm="";
	CTime t = CTime::GetCurrentTime();
	tm = t.Format( "%m月%d日%H:%M:%S");

	CString msg = CString(content);
	CString fromNa = CString(from);
	CString toNa = CString(to);
	CString show ="";
	fromNa.MakeUpper();
	toNa.MakeUpper();
	show.Format("\r\n消息自%s发往%s[%s]\r\n%s\r\n",fromNa,toNa,tm,msg);
	((CBQQApp*)AfxGetApp())->hisMsg+=show;
	if(!(((CEdit*)GetDlgItem(IDC_INOUT))->GetStyle()&ES_READONLY ))
		GetDlgItem(IDC_INOUT)->GetWindowText(((CBQQApp*)AfxGetApp())->tmpMsg);
	((CEdit*)GetDlgItem(IDC_INOUT))->SetReadOnly(FALSE);
	int len = GetDlgItem(IDC_INOUT)->GetWindowTextLength();
	CString add="";
	GetDlgItem(IDC_ADDRLIST)->GetWindowText(add);
	if(add.IsEmpty())
		((CEdit*)GetDlgItem(IDC_ADDRLIST))->SetWindowText(fromNa);
	((CDimEditCtrl*)GetDlgItem(IDC_ADDRLIST))->SetShowDimControl( false );
	((CEdit*)GetDlgItem(IDC_INOUT))->SetSel(len,len);
	((CEdit*)GetDlgItem(IDC_INOUT))->ReplaceSel(show);
	((CEdit*)AfxGetMainWnd()->GetDlgItem(IDC_INOUT))->SetReadOnly(TRUE);
	GetDlgItem(IDC_INPUTMSG)->EnableWindow(TRUE);
	::PostMessage(AfxGetMainWnd()->m_hWnd,WM_LIBEN,IDR_MAINFRAME,WM_LBUTTONDOWN);
	MessageBeep(-1);

}


void CBQQDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	/*
	static bool firstChar = true;
	if(firstChar)
	{
		GetDlgItem(IDC_INOUT)->SetWindowText("");
		firstChar	= false;
	}
	*/

	CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}



BOOL CBQQDlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class

	return CDialog::DestroyWindow();
}

void CBQQDlg::OnInputmsg() //reply the msg
{
	// TODO: Add your control notification handler code here
	if(isHistory)
	{
		((CBQQApp*)AfxGetApp())->tmpMsg ="";
		((CButton*)GetDlgItem(IDC_HISTORY))->SetWindowText("历史(&H)");
		((CEdit*)GetDlgItem(IDC_INOUT))->SetReadOnly(FALSE);
		GetDlgItem(IDC_INOUT)->SetFocus();
		OnClear();
	}
	isHistory = FALSE;
	GetDlgItem(IDC_INOUT)->SetWindowText(((CBQQApp*)AfxGetApp())->tmpMsg);
	GetDlgItem(IDC_SEND)->EnableWindow(TRUE);
	GetDlgItem(IDC_HISTORY)->EnableWindow(TRUE);
	GetDlgItem(IDC_INPUTMSG)->EnableWindow(FALSE);
	((CEdit*)AfxGetMainWnd()->GetDlgItem(IDC_INOUT))->SetReadOnly(FALSE);
	GetDlgItem(IDC_INOUT)->SetFocus();

}

BOOL CBQQDlg::CreateListenerSock()
{
	BOOL rs = TRUE;
	rs = sockSvr.Create(2118);
	rs= sockSvr.Listen();
	return rs;
}

BOOL CBQQDlg::ConnectServer(CString user)
{
	BOOL res = FALSE;
	unsigned long ipAddr=0L,rs = 0L;
	try
	{
		ipAddr=inet_addr(user.GetBuffer(user.GetLength()));
		sendTarget.ReleaseBuffer();
		if(ipAddr != INADDR_NONE)
		{
			SOCKADDR_IN add_in;
			add_in.sin_family = AF_INET;
			ZeroMemory(add_in.sin_zero,0) ;
			add_in.sin_addr.s_addr = ipAddr;
			add_in.sin_port = htons(2118);
			sockClient.Close();
			res = sockClient.Create();
			res = sockClient.Connect((SOCKADDR*)&add_in, sizeof(add_in));
		}
		else if( (ipAddr = GetHsByName(user)) != rs)
		{
			SOCKADDR_IN add_in;
			add_in.sin_family = AF_INET;
			ZeroMemory(add_in.sin_zero,0) ;
			add_in.sin_addr.s_addr = ipAddr;
			add_in.sin_port = htons(2118);
			sockClient.Close();
			res = sockClient.Create();
			res = sockClient.Connect((SOCKADDR*)&add_in, sizeof(add_in));
		}
		else
		{
			sockClient.Close();
			res = sockClient.Create();
			res = sockClient.Connect(user,2118);
		}
	}
	catch(...)
	{
		res = FALSE;
	}
	if(res)
		((CBQQApp*)AfxGetApp())->closed = FALSE;
	else
		((CBQQApp*)AfxGetApp())->closed = TRUE;
	return res;
}

unsigned long CBQQDlg::GetHsByName(CString name)
{
	unsigned long ip=0L;
	LPHOSTENT lphost;
	lphost = gethostbyname(name.GetBuffer(name.GetLength()));
	name.ReleaseBuffer();
	if (lphost != NULL)
		ip = ((LPIN_ADDR)lphost->h_addr)->s_addr;

	return ip;
}

BOOL CBQQDlg::FormatAddr()
{
	GetDlgItem(IDC_ADDRLIST)->GetWindowText(sendTarget);
	sendTarget.TrimLeft();
	sendTarget.TrimRight();
	sendTarget.MakeLower();
	int pos=0;
	while((pos=sendTarget.Find(";"),pos) !=-1)
	{
		sendTarget.Delete(pos,2);
		sendTarget.Insert(pos,';');
	}
	if(sendTarget =="*"||sendTarget=="*.*")
		 sendTarget ="*";
	if(sendTarget.IsEmpty())
	{
		MessageBox("请输入主机名","提示");
		return FALSE;
	}
	return TRUE;
}



BOOL CBQQDlg::SendTheMsg(CString targ)
{
	byte *buffer = new byte[50];
	CString msg="",info="",flg="E",name =hostIP;
	int rtn=0;
	ZeroMemory(buffer,50);
	if(((CButton*)GetDlgItem(IDC_ANONYMOUS))->GetCheck()==1)
	{
		flg = "B";
	}
	if(((CButton*)GetDlgItem(IDC_INCLUDEATTACH))->GetCheck()==1)
	{
		flg = "D";
		name.Format("%s/%s",fileName,hostIP);
	}
	memcpy(buffer, flg,1);
	int msgSz = sendMessageStr.GetLength();
	info.Format("%s$%s$%d",hostName,name,msgSz);
	int infoSz = info.GetLength();
	memcpy(buffer+2,info,infoSz);
	int ss = msgSz+50;
	byte *sdData = new byte[ss];
	ZeroMemory(sdData,ss);
	memcpy(sdData,buffer, 50);
	memcpy(sdData+ 50, (LPCTSTR)sendMessageStr,msgSz);
	rtn = sockClient.Send(sdData,ss);
	
	delete sdData;
	delete buffer;
	if(rtn == SOCKET_ERROR)
		return FALSE;
	else
	{
		CString tm="",promp="";
		CTime t = CTime::GetCurrentTime();
		tm = t.Format( "%m月%d日%H:%M:%S");
		((CBQQApp*)AfxGetApp())->hisMsg+="\r\n您将消息发往"+targ+"["+tm+"]\r\n"+sendMessageStr+"\r\n";
		promp.Format("消息成功发向%s!",targ);
		MessageBox(promp,"提示",MB_OK);
		GetDlgItem(IDC_INOUT)->SetWindowText("");
	}
	return TRUE;
}
void CBQQDlg::OnCancel()
{
	if( m_bClose )
	{
		if(MessageBox("您确定要关闭本信使吗?","提示",MB_ICONINFORMATION|MB_OKCANCEL)==IDOK)
		{
			DoClose();
			CDialog::OnCancel();
		}
		else
			m_bClose = FALSE;
	}
	else
		HidenIt();
}

void CBQQDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	m_bClose = TRUE;
	CDialog::OnClose();
}



int CBQQDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	BOOL suc= TRUE;
	suc=RegisterHotKey(m_hWnd,1001,MOD_CONTROL|MOD_ALT,'A'); //MOD_CONTROL|MOD_ALT
	suc &=RegisterHotKey(m_hWnd,1002,MOD_CONTROL|MOD_ALT,'a'); 
	suc &=RegisterHotKey(m_hWnd,1003,0,VK_F4); 
	if(!suc)
		MessageBox("系统热键有冲突,注册失败","提示",MB_ICONINFORMATION|MB_OK);
	// TODO: Add your specialized creation code here
	
	return 0;
}

LRESULT CBQQDlg::OnHotKey(WPARAM wParam,LPARAM lParam) 
{

	::PostMessage(m_hWnd,WM_LIBEN,IDR_MAINFRAME,WM_LBUTTONDOWN);

	return 0;
}





void CBQQDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	//
	if(first)
	{
		HidenIt();
		KillTimer(m_nTimer0);
		first = FALSE;
		return;
	}
	/*
	
		CTime t = CTime::GetCurrentTime();
		static int no1 = 0,no2=0,no3=0;
		CString tm = "",prom="";
		tm = t.Format( "%H");
		if(tm== "09")
		{
			no1++;
			prom = "你今天打卡了吗?";
		}
		if(tm == "13")
		{
			no2++;
			prom = "13点了,打卡了吗?";
		}
		if(tm == "17")
		{
			no3++;
			prom = "打卡下班回家吧!";
		}
		if(!prom.IsEmpty())
		{
			
			if(((no1 >0) && (no1<2))||((no2 >0) && (no2<2))||((no3 >0) && (no3<2)))
			{
				::MessageBox(NULL,prom,"BQQ提醒您",MB_OK);
			}
		}
		else if(no3 >2)
		{
			KillTimer(m_nTimer);
		}*/
	
	CDialog::OnTimer(nIDEvent);
}

BOOL CBQQDlg::OnQueryEndSession() 
{
	DoClose();

	if (!CDialog::OnQueryEndSession())
		return FALSE;
	
	// TODO: Add your specialized query end session code here
	return TRUE;
}

void CBQQDlg::DoClose()
{
	sockSvr.Close();
	sockClient.Close();
	g_bExit = TRUE;

	int autosav = ((CBQQApp*)AfxGetApp())->GetProfileInt("privatesetting","autoSave",0);
	if(autosav == 1)
	{
		CString tmp=((CBQQApp*)AfxGetApp())->hisMsg;
		if(!tmp.IsEmpty())
		{
			///*
			TCHAR exeFullPath[MAX_PATH];
			GetModuleFileName(GetModuleHandle(AfxGetApp()->m_pszAppName),exeFullPath,MAX_PATH);
			CString workDir = ".\\";
			workDir = CString(exeFullPath);
			int curPost=0,posx=0;
			while((curPost=workDir.Find('\\',curPost+1))!=-1)
			{
				posx = curPost;
			}
			workDir = workDir.Left(posx+1);
			//*/			
			
			FILE *fp;
			CString time="",logName="";
			CTime t = CTime::GetCurrentTime();
			time = t.Format( "%B");
			time+=".log";
			//logName = time;
			logName.Format("%s%s",workDir,time);
			fp = fopen(logName,"a");
			if(fp!=NULL)
			{
				fwrite(tmp,sizeof(char),tmp.GetLength(),fp);
				fclose(fp);
			}

		}
	}
	int autoRestart = ((CBQQApp*)AfxGetApp())->GetProfileInt("privatesetting","autoRestart",0);
	if(g_bSendOnly==FALSE)
	{
		CMailSlot mslot = CMailSlot(".", MESSNGR_MAILSLOT);
		if (mslot.Create())
		{
			mslot.Write("I\0Me\0", 5);
			mslot.Close();
		}
		if(autoRestart == 1)
		{
			TCHAR sComputerName[MAX_NAME_SIZE+1];
			DWORD nSize = MAX_NAME_SIZE;
			if ( !GetComputerName( sComputerName,  &nSize) )
					strcpy(sComputerName, "unknown");
			if (CNTService::IsServiceStarted(sComputerName, MESSENGER_SERVICE_NAME) == 2)
			{
				//int result = ::MessageBox( NULL, "Windows NT messenger service 已停止. 启动它吗?", "提示", MB_YESNO | MB_ICONSTOP );
				//if ( result == IDYES )
				{
					CNTService::StartMService(sComputerName, MESSENGER_SERVICE_NAME);// start it
					Sleep(3000);
				}
			}
			else if(CNTService::IsServiceStarted(sComputerName, MESSENGER_SERVICE_NAME) == 3)
			{
				MessageBox("服务暂时不能启动,有必要的话请手工启动!","提示",MB_ICONINFORMATION|MB_OK);
			}
		}
	}
	UnregisterHotKey(m_hWnd,1001);
	UnregisterHotKey(m_hWnd,1002);
	UnregisterHotKey(m_hWnd,1003);
}

⌨️ 快捷键说明

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