www.pudn.com > gps-time.rar > MainFrm.cpp


// MainFrm.cpp : implementation of the CMainFrame class 
// 
 
#include "stdafx.h" 
#include "RegionComm.h" 
#include "Communication.h" 
#include "MainFrm.h" 
#include "RegionCommView.h" 
#include "RegionCommDoc.h" 
#include "InputRegionNo.h" 
#include "RegionSocket.h" 
#include "ComPort.h" 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
#define TIMER_INDEX		5 
#define IP_ADD	4 
///////////////////////////////////////////////////////////////////////////// 
// CMainFrame 
 
extern bool bgpsOK; 
extern long igps; 
extern CString sgps; 
 
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd) 
 
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) 
	//{{AFX_MSG_MAP(CMainFrame) 
	ON_WM_CREATE() 
	ON_WM_TIMER() 
	ON_WM_DESTROY() 
	ON_WM_CLOSE() 
	ON_COMMAND(IconDispMainFace,OnIconDispMainFace) 
	ON_COMMAND(IconExit,OnIconExit) 
	ON_COMMAND(IconMinimize,OnIconMinimize) 
	ON_COMMAND(ID_APP_EXIT, OnAppExit) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
static UINT indicators[] = 
{ 
	ID_SEPARATOR,           // status line indicator 
	ID_INDICATOR_CAPS, 
	ID_INDICATOR_NUM, 
	ID_INDICATOR_SCRL, 
	ID_SEPARATOR, 
	ID_SEPARATOR, 
}; 
 
///////////////////////////////////////////////////////////////////////////// 
// CMainFrame construction/destruction 
 
CMainFrame::CMainFrame() 
{ 
	m_msgbox=false; 
	 
} 
 
CMainFrame::~CMainFrame() 
{ 
} 
 
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{ 
	TCHAR szBuf[MAX_PATH];	 
 
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1) 
		return -1; 
	 
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP 
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || 
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) 
	{ 
		TRACE0("Failed to create toolbar\n"); 
		return -1;      // fail to create 
	} 
 
	if (!m_wndStatusBar.Create(this) || 
		!m_wndStatusBar.SetIndicators(indicators, 
		  sizeof(indicators)/sizeof(UINT))) 
	{ 
		TRACE0("Failed to create status bar\n"); 
		return -1;      // fail to create 
	} 
	//add by mdw 
	UINT nID, nStyle; 
	int cxWidth; 
	m_wndStatusBar.GetPaneInfo( TIMER_INDEX, nID, nStyle, cxWidth);	 
	m_wndStatusBar.SetPaneInfo( TIMER_INDEX, nID, SBPS_NORMAL, 120); 
	m_wndStatusBar.GetPaneInfo( IP_ADD, nID, nStyle, cxWidth);	 
	m_wndStatusBar.SetPaneInfo( IP_ADD, nID, SBPS_NORMAL, 100); 
    
	m_TimerId = SetTimer(4, 1000, NULL); 
 
	DogTimer=SetTimer(5,10000,NULL);//lv 2003 5 17 add 
 
	//创建日志文件 
	m_LogFile.CreateLogFile(); 
	 
	m_LogFile.AddLogData("通知: 区域通信服务器启动。"); 
 
 
	HOSTENT * ht; 
	SOCKADDR_IN sockAddr; 
 
	if (SOCKET_ERROR == ::gethostname(szBuf, 80))  
		return FALSE;	 
	if (NULL == (ht = ::gethostbyname(szBuf)))  
		return FALSE;	 
	memset(&sockAddr, 0, sizeof(sockAddr)); 
	memcpy(&(sockAddr.sin_addr), ht->h_addr,ht->h_length); 
	CString Str = inet_ntoa(sockAddr.sin_addr); 
	m_wndStatusBar.SetPaneText(IP_ADD, Str); 
 
    NOTIFYICONDATA niData; 
	niData.cbSize=sizeof(NOTIFYICONDATA); 
	niData.hWnd=m_hWnd; 
	niData.uID=1; 
	niData.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP; 
	niData.uCallbackMessage =SysTrayNotifyMsg; 
	niData.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
	wsprintf(niData.szTip,"DW-UTC2000区域通信正在运行......"); 
	 
	Shell_NotifyIcon(NIM_ADD,&niData);	 
 
	CenterWindow(); 
	return 0; 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMainFrame diagnostics 
 
///////////////////////////////////////////////////////////////////////////// 
// CMainFrame message handlers 
//display time  
 
void CMainFrame::OnTimer(UINT nIDEvent)  
{ 
	if (nIDEvent == m_TimerId) 
	{ 
		CTime  pTime;	 
		pTime = CTime::GetCurrentTime();		 
		m_wndStatusBar.SetPaneText(TIMER_INDEX, pTime.Format("%Y-%m-%d %H:%M:%S") ); 
		igps++; 
		if(igps>1200)//20分钟没数据 
		{ 
			igps=0; 
			if(bgpsOK)  
			{ 
				bgpsOK=false; 
				//日志 
				ProcessNotify("GPS 20分钟没时间信息"); 
			} 
		} 
		if(igps==0)// 
		{ 
			if(bgpsOK==false)  
			{ 
				bgpsOK=true; 
				//日志 
				ProcessNotify("GPS 收到时间信息"); 
			} 
		} 
		if(sgps!="")  
		{ 
			ProcessNotify(sgps); 
			sgps=""; 
		} 
	} 
 
	if(nIDEvent == DogTimer)////lv 2003 5 17 add 
	{ 
		dog.run(); 
	} 
 
	CFrameWnd::OnTimer(nIDEvent); 
} 
 
void CMainFrame::OnDestroy()  
{	 
	KillTimer(m_TimerId); 
 
	KillTimer(DogTimer);//lv 2003 5 17 add 
 
	//写日志文件	 
	m_LogFile.AddLogData("通知:区域通信服务器退出。"); 
	m_LogFile.CloseLogFile(); 
    CFrameWnd::OnDestroy();	 
} 
 
BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam)  
{ 
	CRegionCommView * pView = (CRegionCommView *)GetActiveView(); 
	if(pView==NULL) 
		return TRUE; 
	switch(wParam) 
	{ 
		case IDM_OCCURERROR: 
			{ 
				CRegionSocket *pSocket = (CRegionSocket *)lParam; 
				if(pSocket==NULL) 
					return TRUE; 
				ProcessError(pSocket->m_dwErrorCode,DEVTYPE_SERVERSOCKET);	// 因为在Socket线程内无法显示消息框 
				if(m_msgbox==false) 
				{ 
					m_msgbox=true; 
					MessageBox(_T("请重新设置服务器。"), _T("错误"), MB_OK | MB_ICONSTOP); 
					 
				} 
			} 
			break; 
		case IDM_DELETECOM:					// 删除COM口 
			{ 
				CString szBuf; 
				szBuf.Format("RS232通讯口%d已被删除", (BYTE)lParam); 
				m_wndStatusBar.SetPaneText(0, szBuf);				 
				pView->UpdateTree(MAKELONG(DEVTYPE_COM, lParam)); 
				ProcessNotify(szBuf); 
			} 
			break; 
		case IDM_ADDCOM:				// 添加COM口提示 
			{				 
				CString str; 
				str.Format(_T("通知:区域机有RS232通讯口%d增加。"),(BYTE)lParam); 
				m_wndStatusBar.SetPaneText(0, str); 
				ProcessNotify(str); 
			} 
			break; 
		case IDM_CONNECTOK:				// Socket连接成功或失败或发生错误 
			{ 
				CString str; 
				CRegionCommDoc * pDoc = (CRegionCommDoc *)GetActiveDocument(); 
				if(pDoc==NULL) 
					return TRUE; 
				CRegionSocket * pRegionSocket = pDoc->m_pCommunication->m_pServerSocket; 
				if(pRegionSocket==NULL) 
					return TRUE; 
				int nState = pRegionSocket->GetState(); 
				switch (nState)  
				{ 
				case DEV_CONNECTED: 
					str = "已经与中心通信服务器连接。"; 
					break; 
				case DEV_SOCKCLOSED: 
					str = "发生错误,连接中心通信服务器网络端口被关闭。"; 
					break; 
				case DEV_ASKCONNECT: 
					str = "与中心通信服务器断接,并重新请求连接。"; 
					break; 
				} 
				m_wndStatusBar.SetPaneText(0, str);				 
				pView->UpdateList(MAKELONG(0,MAKEWORD(0,DEVTYPE_SERVERSOCKET))); 
				ProcessNotify(str); 
			} 
			break; 
		case IDM_LINKDEVUPDATE:			// 有新设备连接,更新未连接设备列表			 
			{ 
				CString strInfo; 
				 
				strInfo.Format("COM%d端口%d号设备%s",LOBYTE(LOWORD(lParam)), 
					HIBYTE(LOWORD(lParam)), (LOBYTE(HIWORD(lParam))==0 ? "脱机":"联机"));				 
				m_wndStatusBar.SetPaneText(0, strInfo);				 
				DWORD  iDevType=DEVTYPE_COM; 
				lParam=lParam & 0xffffff; 
				lParam=lParam | (iDevType<<24); 
				pView->UpdateList(lParam);// (WORD)(0x00FF & lParam)));				 
				ProcessNotify(strInfo);			 
			} 
			    break; 
		case IDM_REREGIST: 
			{// 区域号改变, 重新启动系统 
				CRegionCommView * pView = (CRegionCommView *)GetActiveView(); 
				if(pView==NULL) 
					return(TRUE); 
				pView->ResetSystem();				 
			} 
			break; 
		case IDM_CONNECTTIMEOUT:			// Socket连接超时 
			break; 
		default: 
			return CFrameWnd::OnCommand(wParam, lParam); 
	} 
	return TRUE; 
} 
 
void CMainFrame::ProcessError(DWORD dwError, DWORD dwDevType) 
{	 
	TCHAR szTmp[200]; 
	 
	switch (0x0000FFFF & dwDevType) { 
	case DEVTYPE_SERVERSOCKET: 
		_tcscpy(szTmp, _T("连接中心机的Socket端口"));	 
		break; 
	default: 
		_tcscpy(szTmp, _T("未知设备")); 
	} 
	 
	CString str;	 
    str.Format(_T("错误:%s发生错误! 错误代码是%d。"), szTmp, dwError);  
	m_LogFile.AddLogData(str); 
} 
 
void CMainFrame::ProcessNotify(CString pStr) 
{	 
	m_LogFile.AddLogData(pStr); 
} 
 
void CMainFrame::OnClose()  
{ 
   if(bDogServerAskMeExit==true || IDYES==MessageBox("真的要退出区域通信程序吗?","退出区域通信程序",MB_YESNO|MB_ICONWARNING)) 
   { 
	 NOTIFYICONDATA niData; 
	niData.cbSize=sizeof(NOTIFYICONDATA); 
	niData.hWnd=m_hWnd; 
	niData.uID=1; 
	niData.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP; 
	niData.uCallbackMessage =SysTrayNotifyMsg; 
	niData.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
	wsprintf(niData.szTip,"DW-UTC2000区域通信正在运行......"); 
	Shell_NotifyIcon(NIM_DELETE,&niData); 
//	CFrameWnd::OnClose();  
	DestroyWindow(); 
   } 
} 
 
 
LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)  
{ 
	 
	if(message==WM_SYSCOMMAND) 
	{ 
		if(SC_CLOSE==wParam) 
		{ 
			ShowWindow(SW_MINIMIZE);			 
			ShowWindow(SW_HIDE); 
			return(TRUE); 
		} 
	} 
	if(message==SysTrayNotifyMsg) 
	{	 
	    switch(lParam) 
		{ 
		  case WM_LBUTTONDBLCLK: 
			  ShowWindow(SW_RESTORE);			   
			  SetForegroundWindow(); 
			  return(TRUE); 
		  case WM_LBUTTONDOWN:				   
			  return(TRUE); 
		  case WM_RBUTTONDOWN: 
			  CMenu  iconMenu; 
			  if(iconMenu.CreatePopupMenu()) 
			  { 
				  iconMenu.AppendMenu(MF_STRING,IconDispMainFace,_T("窗口最大化"));				   
				  iconMenu.AppendMenu(MF_STRING,IconMinimize,_T("窗口最小化"));				  				   
				  iconMenu.AppendMenu(MF_STRING,IconExit,_T("退出区域通信程序"));	 
				  CPoint CursorPos; 
				  GetCursorPos(&CursorPos); 
				  SetForegroundWindow(); 
				  iconMenu.TrackPopupMenu(TPM_RIGHTALIGN|TPM_RIGHTBUTTON, 
					  CursorPos.x,CursorPos.y,this); 
			  } 
			  return(TRUE); 
		} 
	}	 
	return CFrameWnd::WindowProc(message, wParam, lParam); 
} 
 
void CMainFrame::OnIconDispMainFace() 
{ 
   ShowWindow(SW_RESTORE); 
	SetForegroundWindow(); 
} 
 
void CMainFrame::OnIconExit() 
{ 
    OnClose(); 
} 
 
void CMainFrame::OnIconMinimize() 
{ 
   	ShowWindow(SW_MINIMIZE);			 
	ShowWindow(SW_HIDE);	 
} 
 
void CMainFrame::OnAppExit()  
{ 
	OnClose(); 
//	DestroyWindow(); 
	 
}