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

📄 astimelitedlg.cpp

📁 一个简易Internet校时程序
💻 CPP
字号:
// asTimeLiteDlg.cpp : implementation file
//

#include "stdafx.h"
#include "asTimeLite.h"
#include "asTimeLiteDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern char glo_szAppIniFile[MAX_PATH];

/////////////////////////////////////////////////////////////////////////////
// CAsTimeLiteDlg dialog
__declspec( dllexport ) BOOL DMExePluginGetDesc(char* pszDesc,int iMax)
{
	char szDesc[]="简易时间校正软件";
	if(iMax< (int)strlen(szDesc)+1)
		return FALSE;
	strcpy(pszDesc,szDesc);
	return TRUE;
}

BOOL SplitTimeString(char* pszTime,CTime &timeGet)
{//Sun Mar  5 18:44:39 2000 
	while(1)
	{
		if(isalpha(pszTime[strlen(pszTime)-1]) || isdigit(pszTime[strlen(pszTime)-1]))
			break;
		pszTime[strlen(pszTime)-1] = '\0';
	}
	strcat(pszTime,"  ");
	TRACE("org data %s\n",pszTime);
	char szSplitted[5][20];
	memset(szSplitted,0,sizeof(szSplitted));
	int iBegin=0;
	for(int i=0;i<5;i++)
	{//分解数据为五个部分 weekday month day time year
		CString szT=pszTime+iBegin;
		int iFind=szT.Find(' ');
		if(iFind ==-1)
			return FALSE;
		if(szT[iFind+1] == ' ')
		{
			iFind++;
		}
		strncpy(szSplitted[i], pszTime + iBegin, iFind);
		TRACE("%s\n",szSplitted[i]);
		iBegin += iFind+1;
	}

	char szMonth[12][4]={"Jan","Feb","Mar","Apr","May","Jan","Jul","Aug","Sep","Oct","Nov","Dec"};
	int iMonth=-1;
	for(i=0;i<12;i++)
	{//决定月份
		if(strstr(szSplitted[1],szMonth[i]))
		{
			iMonth = i+1;
			break;
		}
	}
	if(iMonth >=0)
	{
		//决定日期
		{
			int iYear = atoi(szSplitted[4]);
			if(iYear)
			{
				int iDay = atoi(szSplitted[2]);
				if(iDay)
				{
					char szTmp[3]="12";
					strncpy(szTmp,szSplitted[3],2);
					int iHour=atoi(szTmp);
					strncpy(szTmp,szSplitted[3]+3,2);
					int iMinute=atoi(szTmp);
					strncpy(szTmp,szSplitted[3]+5,2);
					int iSecond=atoi(szTmp);
					timeGet = CTime(iYear,iMonth,iDay,iHour,iMinute,iSecond);
					return TRUE;
				}
			}
		}
	}
	return FALSE;
}

void GetServerTimeByDayTimeProtocol(CTime &timeGet,LPCSTR pszServer)
{
	BOOL fOK=FALSE;
	CString szError="未知错误";
	CSocket sockConn;
	char szRecv[100]="";
	szError="创建套接口失败";
	if(sockConn.Create())
	{
		szError="连接服务器失败,请重新选择服务器";
		if(sockConn.Connect(pszServer,13))
		{
			char cRecv[2]="1";
			int iRecv=sockConn.Receive(szRecv,100,0);
			szError = "网络传输错误";
			if(iRecv != SOCKET_ERROR )
			{
				szRecv[iRecv] = '\0';
				TRACE("%s\n",szRecv);
				szError="数据错误";
				if(SplitTimeString(szRecv,timeGet))
				{
					CTime timeC=CTime::GetCurrentTime();
					CTimeSpan ts= timeC -timeGet ;
					CString szOut;
					szOut.Format("你的系统时间比服务器时间%s %d 秒(%d:%d:%d),是否调整?",
									(ts.GetTotalSeconds())?"早":"晚",
									abs(ts.GetTotalSeconds()),
									abs(ts.GetTotalSeconds())/3600,
									(abs(ts.GetTotalSeconds())%3600)/60,
									abs(ts.GetTotalSeconds())%60);
					if(AfxMessageBox(szOut,MB_YESNO)==IDYES)
					{
						timeC= CTime::GetCurrentTime();
						timeC -=ts;
						SYSTEMTIME sysTime;
						sysTime.wYear = timeC.GetYear();
						sysTime.wMonth = timeC.GetMonth();
						sysTime.wDay = timeC.GetDay();
						sysTime.wDayOfWeek = timeC.GetDayOfWeek();
						sysTime.wHour = timeC.GetHour();
						sysTime.wMinute = timeC.GetMinute();
						sysTime.wSecond = timeC.GetSecond();
						sysTime.wMilliseconds =0;
						SetLocalTime(&sysTime);
					}
					return ;
				}
			}
		}
	}
	if(!fOK)
		AfxMessageBox(szError);
	return ;
}

void CAsTimeLiteDlg::AddAdrToList(LPCSTR pszFileName)
{
	FILE* pFile=fopen(pszFileName,"r");
	if(!pFile)
		return;
	char szLine[100]="";
	while(fgets(szLine,100,pFile))
	{
		if(strlen(szLine)<3)
			continue;
		szLine[strlen(szLine)-1]='\0';
		//if(m_cbSvr.FindString(-1,szLine)==CB_ERR)
		{
			m_cbSvr.AddString(szLine);
		}
		memset(szLine,0,100);
	}
	fclose(pFile);
}

void CAsTimeLiteDlg::OnOK(void)
{
	CString szSvr;
	m_cbSvr.GetWindowText(szSvr);
	if(0==szSvr.GetLength())
	{
		AfxMessageBox(IDS_INPUT_SVR);
		m_cbSvr.SetFocus();
		return;
	}
	CTime tmGet;
	GetServerTimeByDayTimeProtocol(tmGet,szSvr);
}

CAsTimeLiteDlg::CAsTimeLiteDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAsTimeLiteDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAsTimeLiteDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_fExped=TRUE;
//	CTime tmGet;
//	char szGet[40]={"Sun Mar  5 18:44:39 2000"};
//	SplitTimeString(szGet,tmGet);
}

void CAsTimeLiteDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAsTimeLiteDlg)
	DDX_Control(pDX, IDC_SVR_LIST, m_cbSvr);
	DDX_Control(pDX, IDC_SEP_LINE, m_staLine);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAsTimeLiteDlg, CDialog)
	//{{AFX_MSG_MAP(CAsTimeLiteDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ABOUT, OnAbout)
	ON_BN_CLICKED(IDC_ADD_SVR, OnAddSvr)
	ON_BN_CLICKED(IDC_DEL_SVR, OnDelSvr)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAsTimeLiteDlg message handlers

BOOL CAsTimeLiteDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	CenterWindow();
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	//计算窗口高度
	CRect rcW,rcW2;
	GetWindowRect(rcW);
	m_iExpHeight = rcW.Height();
	m_staLine.GetWindowRect(rcW2);
	//ClientToScreen(rcW2);
	m_iColHeight = rcW2.top - rcW.top - 4;
	OnAbout();//隐藏ABOUT信息

	AddAdrToList(glo_szAppIniFile);

	// CG: The following block was added by the ToolTips component.	{//添加提示		// Create the ToolTip control.		m_tooltip.Create(this);		m_tooltip.Activate(TRUE);		m_tooltip.AddTool(GetDlgItem(IDC_ADD_SVR),IDS_ADD);
		m_tooltip.AddTool(GetDlgItem(IDC_DEL_SVR),IDS_DEL);
		m_tooltip.AddTool(GetDlgItem(IDC_SVR_LIST),IDS_LIST);
		m_tooltip.AddTool(GetDlgItem(IDC_ABOUT),IDS_ABOUT);		// TODO: Use one of the following forms to add controls:		// m_tooltip.AddTool(GetDlgItem(IDC_<name>), <string-table-id>);		// m_tooltip.AddTool(GetDlgItem(IDC_<name>), "<text>");	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CAsTimeLiteDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CAsTimeLiteDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CAsTimeLiteDlg::OnAbout() 
{
	CRect rcW;
	GetWindowRect(rcW);
	if(m_fExped)
	{//关闭
		rcW.bottom = rcW.top + m_iColHeight;
	}
	else
	{//展开
		rcW.bottom = rcW.top + m_iExpHeight;
	}
	m_fExped = !m_fExped;
	MoveWindow(rcW);
}

BOOL CAsTimeLiteDlg::PreTranslateMessage(MSG* pMsg)
{
	// CG: The following block was added by the ToolTips component.	{		// Let the ToolTip process this message.		m_tooltip.RelayEvent(pMsg);	}	return CDialog::PreTranslateMessage(pMsg);	// CG: This was added by the ToolTips component.
}

void CAsTimeLiteDlg::OnAddSvr() 
{
	CString szSvr;
	m_cbSvr.GetWindowText(szSvr);
	if(0==szSvr.GetLength())
	{
		return;
	}
	int iFind=m_cbSvr.FindString(-1,szSvr);
	if(iFind !=CB_ERR)
	{
		m_cbSvr.SetCurSel(iFind);
	}
	else
		m_cbSvr.AddString(szSvr);
}

void CAsTimeLiteDlg::OnDelSvr() 
{
	CString szSvr;
	m_cbSvr.GetWindowText(szSvr);
	if(0==szSvr.GetLength())
	{
		AfxMessageBox(IDS_INPUT_SVR);
		return;
	}
	int iFind=m_cbSvr.FindString(-1,szSvr);
	if(iFind !=CB_ERR)
	{
		m_cbSvr.DeleteString(iFind);
	}
	m_cbSvr.SetWindowText("");
}

void CAsTimeLiteDlg::OnDestroy() 
{
	int iTotal=m_cbSvr.GetCount();
	FILE *pFile=fopen(glo_szAppIniFile,"wt");
	if(pFile)
	{
		for(int i=0;i<iTotal;i++)
		{
			CString szAdr;
			m_cbSvr.GetLBText(i,szAdr);
			fprintf(pFile,"%s\n",szAdr);
			//fputs(szAdr,pFile);
		}
		fclose(pFile);
	}
	CDialog::OnDestroy();
	
	
}

⌨️ 快捷键说明

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