?? superwords.cpp
字號:
// SuperWords.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "SuperWords.h"
#include "SuperWordsDlg.h"
#include "LoginDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSuperWordsApp
BEGIN_MESSAGE_MAP(CSuperWordsApp, CWinApp)
//{{AFX_MSG_MAP(CSuperWordsApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSuperWordsApp construction
CSuperWordsApp::CSuperWordsApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CSuperWordsApp object
CSuperWordsApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CSuperWordsApp initialization
BOOL CSuperWordsApp::InitInstance()
{
AfxEnableControlContainer();
// 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.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
SetRegistryKey(_T("HMN-SOFT"));
if(!InitODBC())
return FALSE;
CLoginDlg * log = new CLoginDlg;
if(log->DoModal() != IDOK)
return FALSE;
DWORD dwUser;
dwUser = log->GetUserInfo();
delete log;
CSuperWordsDlg dlg;
m_pMainWnd = &dlg;
dlg.DoModal();
return FALSE;
}
void CSuperWordsApp::GetCurDir(TCHAR *szBuffer, TCHAR *szTempBuf)
{
CString szTemp;
szTemp.Empty();
szTemp=szBuffer;
char ch='\\';
char *pdest;
int n=0;
memset(szTempBuf,0,sizeof(szTempBuf));
pdest = strrchr( szBuffer, ch );
n = pdest - (char *)szBuffer + 1;
CString szReturn;
szReturn.Empty();
szReturn = szTemp.Left(n);
memset(szTempBuf,0,sizeof(szTempBuf));
lstrcpy(szTempBuf,szReturn);
}
BOOL CSuperWordsApp::InitODBC()
{
char strCurfile[256];
char * p;
CFileFind file;
::GetModuleFileName(m_hInstance,strCurfile,sizeof(strCurfile));
p = strCurfile;
while(strchr(p,'\\'))
{
p = strchr(p,'\\');
p++;
}
*p = '\0';
strcat(strCurfile,"BookData.mdb");
if(!file.FindFile(strCurfile))
{
AfxMessageBox("找不到BookData.mdb數據庫文件,請保證該文件和本程序在同一個目錄中!",MB_OK|MB_ICONSTOP);
return FALSE;
}
if(GetProfileInt("數據源","IsFirstRun",0))
{
CString strKeyDir = GetProfileString("數據源","CurDir");
if(strKeyDir.Compare(strCurfile))
{
WriteProfileString("數據源","CurDir",strCurfile);
return(LoadODBC(CString("CarsDataBase"),CString(strCurfile),CString("數據源")));
}
}
else
{
USERINFO sysuser;
strcpy(sysuser.name,"System");
sysuser.oper = 1;
strcpy(sysuser.password,"system");
AfxGetApp()->WriteProfileBinary("用戶","User0000",(LPBYTE)&sysuser,sizeof(sysuser));
AfxGetApp()->WriteProfileInt("用戶","LastID",1);
WriteProfileInt("數據源","IsFirstRun",1);
WriteProfileString("數據源","CurDir",strCurfile);
return(LoadODBC(CString("CarsDataBase"),CString(strCurfile),CString("數據源")));
}
return TRUE;
}
BOOL CSuperWordsApp::LoadODBC(CString strSourceName,CString strSourceDb, CString strDescription)
{
//存放打開的注冊表鍵
HKEY hKey;
DWORD dw;
//存放注冊表 API函數執行的返回值
LONG lReturn;
//存放要打開的子鍵
CString strSubKey;
//檢測是否安裝了 MS Access ODBC driver:odbcjt32.dll
//獲得 Windows系統目錄
char sysDir[MAX_PATH];
char drvName[]="\\odbcjt32.dll" ;
::GetSystemDirectory(sysDir,MAX_PATH);
strcat(sysDir,drvName);
CFileFind findFile;
if(!findFile.FindFile(sysDir))
{
AfxMessageBox("找不到MS Access的ODBC驅動程序odbcjt32.dll,加載數據源失敗!",
MB_OK|MB_ICONSTOP);
return FALSE;
}
strSubKey="SOFTWARE\\ODBC\\ODBC.INI\\"+strSourceName;
//創建ODBC數據源在注冊表中的子鍵
lReturn=::RegCreateKeyEx(HKEY_LOCAL_MACHINE,(LPCTSTR)strSubKey,0,NULL,
REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&hKey,&dw);
if(lReturn != ERROR_SUCCESS)
return false;
//設置數據源的各項參數
CString strDbq=strSourceDb;
CString strDriver=sysDir;
DWORD dwDriverId=25;
CString strFil="MS Access;";
CString strPwd=strSourceName;
DWORD dwSafeTransactions=0;
CString strUid=strSourceName;
::RegSetValueEx(hKey,"DBQ",0L,REG_SZ,(const BYTE*)((LPCTSTR) strDbq),strDbq.GetLength());
::RegSetValueEx(hKey,"Description",0L,REG_SZ,(const BYTE*)((LPCTSTR)strDescription),strDescription.GetLength());
::RegSetValueEx(hKey,"Driver",0L,REG_SZ,(const BYTE*)((LPCTSTR)strDriver),strDriver.GetLength());
::RegSetValueEx(hKey,"DriverId",0L,REG_DWORD,(const BYTE*)(&dwDriverId),sizeof(dw));
::RegSetValueEx(hKey,"FIL",0L,REG_SZ,(const BYTE*)((LPCTSTR)strFil),strFil.GetLength());
::RegSetValueEx(hKey,"PWD",0L,REG_SZ,(const BYTE*)((LPCTSTR)strPwd),strPwd.GetLength ());
::RegSetValueEx(hKey,"SafeTransactions",0L,REG_DWORD,(const BYTE*)(&dwSafeTransactions),sizeof(dw));
::RegSetValueEx(hKey,"UID",0L,REG_SZ,(const BYTE*)((LPCTSTR)strUid),strUid.GetLength());
::RegCloseKey(hKey);
//創建 ODBC數據源的 Jet子鍵
strSubKey +="\\Engines\\Jet" ;
lReturn=::RegCreateKeyEx(HKEY_LOCAL_MACHINE,(LPCTSTR)strSubKey,0,
NULL,REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&hKey,&dw);
if(lReturn != ERROR_SUCCESS)
return false;
//設置該子鍵下的各項參數
CString strImplict="" ;
CString strUserCommit="Yes" ;
DWORD dwPageTimeout=5;
DWORD dwThreads=3;
DWORD dwMaxBufferSize=2048;
::RegSetValueEx(hKey,"ImplictCommitSync",0L,REG_SZ,(const BYTE*)((LPCTSTR)strImplict),strImplict.GetLength()+1);
::RegSetValueEx(hKey,"MaxBufferSize",0L,REG_DWORD,(const BYTE*)(&dwMaxBufferSize),sizeof(dw));
::RegSetValueEx(hKey,"PageTimeout",0L,REG_DWORD,(const BYTE*)(&dwPageTimeout),sizeof(dw));
::RegSetValueEx(hKey,"Threads",0L,REG_DWORD,(const BYTE*)(&dwThreads),sizeof(dw));
::RegSetValueEx(hKey,"UserCommitSync" ,0L,REG_SZ,(const BYTE*)((LPCTSTR)strUserCommit),strUserCommit.GetLength());
::RegCloseKey(hKey);
//設置 ODBC數據庫引擎名稱
lReturn=::RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources" ,0L,KEY_WRITE,&hKey);
if(lReturn !=ERROR_SUCCESS)
return false;
CString strDbType="Microsoft Access Driver (*.mdb)";
::RegSetValueEx(hKey,strSourceName,0L,REG_SZ,(const BYTE*)((LPCTSTR)strDbType),strDbType.GetLength());
::RegCloseKey(hKey);
return true;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -