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

📄 timedlg.cpp

📁 通过获取SQL服务器时间来修改本地时间 运行:2000/XP
💻 CPP
字号:
// timeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "time.h"
#include "timeDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTimeDlg dialog

CTimeDlg::CTimeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTimeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTimeDlg)
	m_ip = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTimeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTimeDlg)
	DDX_Text(pDX, IDC_ET_IP, m_ip);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTimeDlg, CDialog)
	//{{AFX_MSG_MAP(CTimeDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BT_UP, OnBtUp)
	ON_BN_CLICKED(IDC_BT_IP, OnBtIp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTimeDlg message handlers

BOOL CTimeDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
    CRegKey reg;
	HKEY m_hKey;
	DWORD pCount=1024;
	CString KeyValue;
	char szValue[1024];
    LPCTSTR lpszKey = "SOFTWARE\\microsoft";
	if(reg.Open(HKEY_LOCAL_MACHINE,lpszKey)== ERROR_SUCCESS)
	{
		LPCTSTR lKeyName="TigerIP";
		if(reg.QueryValue(szValue,lKeyName,& pCount)== ERROR_SUCCESS)
		{
			KeyValue=szValue;
			m_ip=KeyValue;
            myip=m_ip;
			CTimeDlg::OnBtUp();
			CDialog::OnOK();
		} 
		else
		{
			AfxMessageBox("您第一次使用本软件请输入服务器IP!!\n以后将不再输入");
			GetDlgItem(IDC_BT_IP)->ShowWindow(true);
			GetDlgItem(IDC_BT_UP)->ShowWindow(false);
			return false;
		}
	}
	else

	{
		AfxMessageBox("您第一次使用本软件请输入服务器IP!!");
		return false;
	}
    //done, close
    reg.Close();
	UpdateData(false);
	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 CTimeDlg::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 CTimeDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTimeDlg::OnOK() 
{
	// TODO: Add extra validation here


	CDialog::OnOK();
}

void CTimeDlg::OnBtUp() 
{
	// TODO: Add your control notification handler code here
//	UpdateData(true);

	if( FAILED(::CoInitialize(NULL)) ) 
	{
	    AfxMessageBox("ADO Init failed");
		return;
	}
	_RecordsetPtr pPtr;
	if (FAILED(pPtr.CreateInstance("ADODB.Recordset")))
	{
		AfxMessageBox("Create Instance failed!");
		return;
	}
	CString strSRC;
	strSRC="Driver=SQL Server;Server=";
	strSRC+=myip;
	strSRC+=";Database=";
	strSRC+="master";
	strSRC+=";UID=sa;PWD=";
	_variant_t varSRC(strSRC);
//******************************
    CString strSQL="select getdate() as servertime";
	_variant_t varSQL(strSQL);
	try
	{
       pPtr->Open(varSQL,varSRC,adOpenStatic,adLockOptimistic,adCmdText);
	}
	catch(_com_error &e)
	{
       CString err;
	   err.Format("%s",(char*)e.Description());
	   AfxMessageBox(err);
	   return;
	}

	_variant_t varTime;
	varTime = pPtr->GetCollect ("servertime");
	CString strTime =(char *)_bstr_t(varTime);
	strTime.TrimRight();
	if (strTime.GetLength()!=0)
	{
		CString sPath;
		//取年月日
		int nPos=strTime.Find('-');
		sPath=strTime.Left(nPos);
		int year=atoi(sPath.GetBuffer(10));
		sPath.ReleaseBuffer();
		strTime=strTime.Mid(nPos+1);
		nPos=strTime.ReverseFind('-');
		sPath=strTime.Left(nPos);
		int month=atoi(sPath.GetBuffer(10));
		sPath.ReleaseBuffer();
		strTime=strTime.Mid(nPos+1);
		int day=atoi(strTime.GetBuffer(10));
		strTime.ReleaseBuffer();
//取时分秒
		nPos=strTime.Find(' ');
		strTime=strTime.Mid(nPos+1);
		nPos=strTime.Find(':');
		sPath=strTime.Left(nPos);
		int hour=atoi(sPath.GetBuffer(10));
		sPath.ReleaseBuffer();
		strTime=strTime.Mid(nPos+1);
		nPos=strTime.ReverseFind(':');
		sPath=strTime.Left(nPos);
		int minute=atoi(sPath.GetBuffer(10));
		sPath.ReleaseBuffer();
		strTime=strTime.Mid(nPos+1);
		int second=atoi(strTime.GetBuffer(10));
		strTime.ReleaseBuffer();
/*************************************************************
		CString ctime;
		ctime.Format("%d,%d,%d,%d,%d,%d",year,month,day,hour,minute,second);
		MessageBox(ctime);
/*************************************************************/
		SYSTEMTIME td;
		td.wMilliseconds = 0;
		td.wSecond = second;
		td.wMinute = minute;
		td.wHour = hour;
		td.wDay = day;
		td.wMonth = month;
		td.wYear = year;
		SetLocalTime(&td);
	}
	else
	{
		MessageBox("时间同步失败");
	}
	WinExec("tiger_pos.exe", SW_SHOW); 
}

void CTimeDlg::OnBtIp() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	if (m_ip.IsEmpty())
	{
		MessageBox("请输入正确的IP地址");
		return;
	}
	LONG lResult = 0,lRe=0;
    CRegKey reg,regF;
    //open the required registry key
    LPCTSTR lpszKeyF = "SOFTWARE\\microsoft";
    lRe = regF.Open(HKEY_LOCAL_MACHINE,lpszKeyF);
    
    //check if opened successfully
    if(ERROR_SUCCESS != lResult)
	{
         AfxMessageBox("注册表打开错误!!");
		 return;
	} 

	//set the value
    lResult = regF.SetValue(m_ip,"TigerIp");
    if(ERROR_SUCCESS != lResult)
	{
         AfxMessageBox("注册表修改错误!!\n请重新安装泰格软件!!!");
		 return;
	}
    else
	{
        GetDlgItem(IDC_BT_IP)->ShowWindow(false);
		CTimeDlg::OnBtUp();
	}
}

⌨️ 快捷键说明

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