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

📄 cbsystem.cpp

📁 基于WIN CE 的抄表系统,环境wince .net
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// Cbsystem.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "Cbsystem.h"

#include "MainFrm.h"

#include "CbsystemDoc.h"
#include "CbsystemView.h"
#include "Logon.h"

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

volatile int m_off;
extern void CreateDatabase();
extern void Deletedb();
extern CString DbPrintMessage(CCeDBRecord* prec);
extern void ReadDB(LPWSTR lpsz);
extern LPTSTR nCharToTChar(LPSTR lpcstring);
extern LPSTR nTCharTochar(LPTSTR lpsz);
extern BOOL DataFresh();
extern void  ByteToChar(BYTE b,char sz[]);
extern BYTE  IntToByte(int i);
extern int  ByteToHex(BYTE b);

/////////////////////////////////////////////////////////////////////////////
// CCbsystemApp

BEGIN_MESSAGE_MAP(CCbsystemApp, CWinApp)
	//{{AFX_MSG_MAP(CCbsystemApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCbsystemApp construction

CCbsystemApp::CCbsystemApp(LPCTSTR lpszHelpName)
	: CWinApp(lpszHelpName)
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CCbsystemApp object

CCbsystemApp theApp(_T("抄表系统"));
TCHAR	szError[200];
char	szClientA[200];

CEOID	poid;
CCeDBDatabase db;
const DWORD	DB_IDENT_ID = 11111;
	const WORD PROP_BH=101,PROP_PORT=102,PROP_NAMEID=103,PROP_NAME=104,PROP_ID=105,PROP_CONST=106,PROP_POWER=107,PROP_POWEROLD=108;
	CCeDBProp dbprops[8]={
		CCeDBProp(CCeDBProp::Type_String,PROP_BH,CCeDBProp::Sort_Ascending),
		CCeDBProp(CCeDBProp::Type_String,PROP_PORT,CCeDBProp::Sort_Ascending),
		CCeDBProp(CCeDBProp::Type_String,PROP_NAMEID,CCeDBProp::Sort_Ascending),
		CCeDBProp(CCeDBProp::Type_String,PROP_NAME,NULL),
		CCeDBProp(CCeDBProp::Type_String,PROP_ID,CCeDBProp::Sort_Ascending),
		CCeDBProp(CCeDBProp::Type_String,PROP_CONST,NULL),
		CCeDBProp(CCeDBProp::Type_String,PROP_POWER,NULL),
		CCeDBProp(CCeDBProp::Type_String,PROP_POWEROLD,NULL)
	};
	CCeDBProp Sortprops[4]={
		CCeDBProp(CCeDBProp::Type_String,PROP_BH,CCeDBProp::Sort_Ascending),
		CCeDBProp(CCeDBProp::Type_String,PROP_PORT,CCeDBProp::Sort_Ascending),
		CCeDBProp(CCeDBProp::Type_String,PROP_NAMEID,CCeDBProp::Sort_Ascending),
		CCeDBProp(CCeDBProp::Type_String,PROP_ID,CCeDBProp::Sort_Ascending)
	};

/////////////////////////////////////////////////////////////////////////////
// CCbsystemApp initialization

BOOL CCbsystemApp::InitInstance()
{
	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.
	//application programe run only one 
	CWnd *pWndPrev;
	//HWND hwnd=::FindWindow(NULL,TEXT("抄表系统"));
	pWndPrev =CWnd::FindWindow(NULL,TEXT("抄表系统"));
	if (pWndPrev != NULL)
	{
		AfxMessageBox(TEXT("抄表系统程序已运行!"));
		pWndPrev->ShowWindow(SW_SHOW);
		pWndPrev->SetForegroundWindow();
		return FALSE;
	}

	CreateDatabase();
	int i;
	// Change the registry key under which our settings are stored.
	// You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("华陇理工科技\\抄表系统"));

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)


	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CCbsystemDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CCbsystemView));
	AddDocTemplate(pDocTemplate);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	// The one and only window has been initialized, so show and update it.
	//*****Logon*******//
	//***condemnate whether the first run this programe***//
	if("" == GetProfileString(TEXT("Lgkj\\cbsystem"),TEXT("name")))
	{
		AfxMessageBox(TEXT("你是第一次运行该程序,请注册用户名与口令!!!"));
		CLogon * dlg = new CLogon;
		if(dlg->DoModal() == IDOK)
		{
			if(!(dlg->m_user) && !(dlg->m_password))
			{
				AfxMessageBox(TEXT("必须注册用户名与口令!!!"));
			}
			else
			{
				WriteProfileString(TEXT("Lgkj\\cbsystem"),TEXT("name"),dlg->m_user);
				WriteProfileString(TEXT("Lgkj\\cbsystem"),TEXT("pass"),dlg->m_password);
			}

		}
	}
	else	//not the first run this programe
	{
		CString szname,szpass;
		szname = GetProfileString(TEXT("Lgkj\\cbsystem"),TEXT("name"));
		szpass = GetProfileString(TEXT("Lgkj\\cbsystem"),TEXT("pass"));

	
		CLogon * dlg = new CLogon;
		if(dlg->DoModal() == IDOK)
		{
			//dlg->UpdateData(FALSE);
			//AfxMessageBox(dlg->m_user+"|"+dlg->m_password);
			if(!(dlg->m_user == szname && dlg->m_password == szpass))
			{
				AfxMessageBox(TEXT("登陆失败!!!!"));
				i = 0;
			}
			else
				i = 1;
		}
	}

	if(1 == i)
	{
		m_pMainWnd->ShowWindow(SW_SHOW);
		m_pMainWnd->UpdateWindow();
		return TRUE;
	}
	else
		return FALSE;
}



/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	virtual BOOL OnInitDialog();		// Added for WCE apps
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CCbsystemApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CCbsystemApp commands
// Added for WCE apps

BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	CenterWindow();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


//******************************************************//
//****FUNCTION****//
//Total char number.
int CountChar(CString str,char ch)
{
	int i,j;
	i=j=0;
	CString tmp;

	while(j!=-1)
	{
		j=str.Find(ch);
		tmp=str.Right(str.GetLength() - j - 1);
		str=tmp;
		i++;
	}
	return --i;
	
}
//****FUNCTION****//
//Analysis string.
CString *SplitString(CString Str,char ch)
{
	CString *xx,tmp;
	int a,count,i;

	i=a=count = 0;

	count = CountChar(Str,ch);
	if (count == 0)
		return NULL;
	// Add a end string .(count+2)
	xx = new CString[count+2];
	while(a!=-1)
	{
		a = Str.Find(ch);
		xx[i] = Str.Left(a);
		tmp = Str.Right(Str.GetLength()-a-1);
		Str = tmp;
		if (a == -1)
		{
			xx[i] = tmp;
			break;
		}
		i++;
	}

	return xx;
}

//****FUNCTION****//
//Convert the ASCII string to a Unicode string.
LPTSTR nCharToTChar(LPSTR lpcstring)
{
			LPTSTR  lpwstring;
			int rc = 0;
			rc = MultiByteToWideChar(
				CP_ACP, 
				MB_PRECOMPOSED||MB_ERR_INVALID_CHARS||MB_USEGLYPHCHARS, 
				lpcstring, 
				-1, 
				NULL, 
				0);
			lpwstring  = (LPTSTR)malloc(rc*sizeof(TCHAR));
			memset( (void *)lpwstring, 0, rc*sizeof(TCHAR) );
			MultiByteToWideChar(
					CP_ACP, 
					MB_PRECOMPOSED||MB_ERR_INVALID_CHARS||MB_USEGLYPHCHARS, 
					lpcstring, 
					-1, 
					lpwstring, 
					rc);
			
			//MessageBox(NULL, (const unsigned short *)lpwstring, TEXT("Received from Server"), MB_OK);
			return lpwstring;
			
}

//****FUNCTION****//
//Conversion TCHAR to char.
LPSTR nTCharTochar(LPTSTR lpsz)
{
	int rc;
	LPSTR 	lpszchar;
	rc =  WideCharToMultiByte(
					CP_ACP, 
					WC_COMPOSITECHECK, 
					(const unsigned short *)lpsz , 
					-1, 
					NULL, 
					0, 
					NULL, 
					NULL);
	if(!rc){
	rc = GetLastError();
	switch(rc){
		case ERROR_INSUFFICIENT_BUFFER :
			MessageBox(NULL,TEXT("ERROR_INSUFFICIENT_BUFFER"),TEXT("ERROR"),MB_OK);
			break;
		case ERROR_INVALID_FLAGS  :
			MessageBox(NULL,TEXT("ERROR_INVALID_FLAGS "),TEXT("ERROR"),MB_OK);
			break;
		case ERROR_INVALID_PARAMETER :
			MessageBox(NULL,TEXT("ERROR_INVALID_PARAMETER "),TEXT("ERROR"),MB_OK);
			break;		
	  }
	}
	lpszchar  = (LPSTR)malloc(rc*sizeof(char));
	memset( (void *)lpszchar, 0, rc*sizeof(char));
	WideCharToMultiByte(
						CP_ACP, 
						WC_COMPOSITECHECK, 
						(const unsigned short *)lpsz , 
						-1, 
						lpszchar, 
						rc, 
						NULL, 
						NULL);

	return lpszchar;

}

/////*******************************************//////////
//****FUNCTION****//
//Create db.
void CreateDatabase()
{
	
	if(!CCeDBDatabase::Exists(_T("NEWPOWER")))
	{
		if(!(poid = db.Create(TEXT("NEWPOWER"),DB_IDENT_ID,4,Sortprops)))
		{
			AfxMessageBox(TEXT("数据库已经创建!!!"));
		}
	}
	
}

//****FUNCTION****//
//Insert data into table.
BOOL InsertTable(LPWSTR lpsz1,LPWSTR lpsz2,LPWSTR lpsz3,LPWSTR lpsz4,LPWSTR lpsz5,LPWSTR lpsz6)
{
	
	CCeDBRecord rec;
	CCeDBProp props[8];
	props[0] = CCeDBProp(LPWSTR(lpsz1),PROP_BH);
	props[1] = CCeDBProp(LPWSTR(lpsz2),PROP_PORT);
	props[2] = CCeDBProp(LPWSTR(lpsz3),PROP_NAMEID);
	props[3] = CCeDBProp(LPWSTR(lpsz4),PROP_NAME);
	props[4] = CCeDBProp(LPWSTR(lpsz5),PROP_ID);
	props[5] = CCeDBProp(LPWSTR(lpsz6),PROP_CONST);
	props[6] = CCeDBProp(TEXT("0"),PROP_POWER);
	props[7] = CCeDBProp(TEXT("0"),PROP_POWEROLD);
	rec.AddProps(props,8);
	if(db.AddRecord(&rec)) 
	{
		//AfxMessageBox(TEXT("记录插入成功!!!"));
			return TRUE;
	}
	{
		AfxMessageBox(TEXT("记录插入失败!!!"));
			return FALSE;
	}
	rec.DeleteAllProps();
	//db.Close();
}

//****FUNCTION****//
//Return Record from table and made of string.
LPTSTR PrintMessage(CCeDBRecord* prec)
{
	LPTSTR		strBh,strNameID, strName, strID,strPower;
	CCeDBProp	*pPropBh,*pPropNameID, *pPropName, *pPropID, *pPropPower;

	strBh		= NULL;
	strNameID	= NULL;
	strName		= NULL;
	strID		= NULL;
	strPower	= NULL;
	
	pPropBh     = prec->GetPropFromIdent(PROP_BH);
	pPropNameID = prec->GetPropFromIdent(PROP_NAMEID);
	pPropName	= prec->GetPropFromIdent(PROP_NAME);
	pPropID		= prec->GetPropFromIdent(PROP_ID);
	pPropPower	= prec->GetPropFromIdent(PROP_POWER);

⌨️ 快捷键说明

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