?? lock.cpp
字號:
// lock.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "lock.h"
#include "MainFrm.h"
#include "lockDoc.h"
#include "lockView.h"
#include "UserAddView.h"
#include "UserRevView.h"
#include "UserDelView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLockApp
BEGIN_MESSAGE_MAP(CLockApp, CWinApp)
//{{AFX_MSG_MAP(CLockApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
ON_COMMAND(ID_USER_ADD, OnUserAdd)
ON_UPDATE_COMMAND_UI(ID_USER_ADD, OnUpdateUserAdd)
ON_COMMAND(ID_USER_REV, OnUserRev)
ON_UPDATE_COMMAND_UI(ID_USER_REV, OnUpdateUserRev)
ON_COMMAND(ID_USER_DEL, OnUserDel)
ON_UPDATE_COMMAND_UI(ID_USER_DEL, OnUpdateUserDel)
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLockApp construction
CLockApp::CLockApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CLockApp object
CLockApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CLockApp initialization
BOOL CLockApp::InitInstance()
{
AfxEnableControlContainer();
//Initial OLE DLLs
if (!AfxOleInit())
{
AfxMessageBox("初始化OLE DLL失敗");
return FALSE;
}
// 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
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
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(CLockDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CLockView));
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;
/*** Begin modification of default InitInstance ***/
// DB Connection
if(!ConnectionDB())
{
AfxMessageBox("DataBase Connection Failure ");
return FALSE;
}
// Save current view ID
m_nCurView = 0; // The CLockView with nothing
// Keep array of views as member of WinApp
CView * pActiveView = ((CFrameWnd*)m_pMainWnd)->GetActiveView();
m_pViews[0] = pActiveView;
m_pViews[1] = (CView*) new UserAddView;
m_pViews[2] = (CView*) new UserRevView;
m_pViews[3] = (CView*) new UserDelView;
// Get the current document
CDocument * pCurrentDoc = ((CFrameWnd*)m_pMainWnd)->GetActiveDocument();
// Initialize a CCreateContext to point to the active document.
// With this context, the new view is added to the document
// when the view is created in CView::OnCreate().
CCreateContext newContext;
newContext.m_pNewViewClass = NULL;
newContext.m_pNewDocTemplate = NULL;
newContext.m_pLastView = NULL;
newContext.m_pCurrentFrame = NULL;
newContext.m_pCurrentDoc = pCurrentDoc;
// The ID of the initial active view is AFX_IDW_PANE_FIRST.
// Incrementing this value by one for additional views works
// in the standard document/view case but the technique cannot
// be extended for the CSplitterWnd case.
UINT viewID[4];
viewID[1] = AFX_IDW_PANE_FIRST + 1;
viewID[2] = AFX_IDW_PANE_FIRST + 2;
viewID[3] = AFX_IDW_PANE_FIRST + 3;
CRect rect(0, 0, 0, 0); // gets resized later
// Need to cast pointers to have correct Create functions called
// CForm2 is CFormView::Create
for ( int nView=1; nView<NUMVIEWS; nView++ )
{
// Create the new view. In this example, the view persists for
// the life of the application. The application automatically
// deletes the view when the application is closed.
m_pViews[nView]->Create(NULL, NULL,
(AFX_WS_DEFAULT_VIEW & ~WS_VISIBLE),
// views are created with the style of AFX_WS_DEFAULT_VIEW
// In MFC 4.0, this is (WS_BORDER | WS_VISIBLE | WS_CHILD)
rect, m_pMainWnd,
viewID[nView], &newContext);
}
// When a document template creates a view, the WM_INITIALUPDATE
// message is sent automatically. However, this code must
// explicitly send the message, as follows.
((UserAddView*)m_pViews[1])->OnInitialUpdate();
((UserRevView*)m_pViews[2])->OnInitialUpdate();
((UserDelView*)m_pViews[3])->OnInitialUpdate();
// The one and only window has been initialized, so show and update it.
//m_pMainWnd->ShowWindow(SW_SHOW);
//m_pMainWnd->UpdateWindow();
/*** End modification of default InitInstance ***/
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// 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)
// No message handlers
//}}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 CLockApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CLockApp message handlers
void CLockApp::OnUserAdd()
{
// TODO: Add your command handler code here
// CString name = "李崇國";
// UserAddView * pView = (UserAddView *)m_pViews[1];
// pView->m_username = name;
// pView->GetDlgItem(IDC_USID_A)->SetWindowText(name);
// pView->GetDlgItem(IDC_COMBO_SEX_A)->SetWindowText("男");
SwitchView(1);
}
void CLockApp::OnUpdateUserAdd(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable( m_nCurView != 1 );
}
void CLockApp::OnUserRev()
{
// TODO: Add your command handler code here
SwitchView(2);
}
void CLockApp::OnUpdateUserRev(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable( m_nCurView != 2 );
}
void CLockApp::OnUserDel()
{
// TODO: Add your command handler code here
SwitchView(3);
}
void CLockApp::OnUpdateUserDel(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable( m_nCurView != 3 );
}
CView * CLockApp::SwitchView(UINT nIndex)
{
ASSERT( nIndex >=0 && nIndex < NUMVIEWS );
CView* pNewView = m_pViews[nIndex];
CView* pActiveView = ((CFrameWnd*) m_pMainWnd)->GetActiveView();
if ( !pActiveView ) // No currently active view
return NULL;
if ( pNewView == pActiveView ) // Already there
return pActiveView;
m_nCurView = nIndex; // Store the new current view's index
// exchange view window ID's so RecalcLayout() works
UINT temp = ::GetWindowLong(pActiveView->m_hWnd, GWL_ID);
::SetWindowLong(pActiveView->m_hWnd, GWL_ID,
::GetWindowLong(pNewView->m_hWnd, GWL_ID));
::SetWindowLong(pNewView->m_hWnd, GWL_ID, temp);
// Display and update the new current view - hide the old one
pActiveView->ShowWindow(SW_HIDE);
pNewView->ShowWindow(SW_SHOW);
((CFrameWnd*) m_pMainWnd)->SetActiveView(pNewView);
((CFrameWnd*) m_pMainWnd)->RecalcLayout();
pNewView->Invalidate();
return pActiveView;
}
BOOL CLockApp::ConnectionDB()
{
// Create ADO connection
m_pConnection.CreateInstance(__uuidof(Connection));
CString strSQL;
strSQL = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=lockinfo.mdb";
//strSQL = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=lock.mdb";
try
{
//m_pConnection->Open((_bstr_t)strSQL,"","",adModeUnknown);
m_pConnection->Open((_bstr_t)strSQL,"","",adModeUnknown);
}
catch (_com_error e)
{
CString strError;
strError.Format("Warning:Open Connetcion Error: %s",e.ErrorMessage());
AfxMessageBox(strError);
return FALSE;
}
return TRUE;
}
CString CLockApp::VariantToCString(const _variant_t &var)
{
CString strValue;
switch (var.vt)
{
case VT_BSTR://字符串
case VT_LPSTR:
case VT_LPWSTR:
strValue = (LPCTSTR)(_bstr_t)var;
break;
case VT_I1://無符號字符
case VT_UI1:
strValue.Format("%d", var.bVal);
break;
case VT_I2://短整型
strValue.Format("%d", var.iVal);
break;
case VT_UI2://無符號短整型
strValue.Format("%d", var.uiVal);
break;
case VT_INT://整型
strValue.Format("%d", var.intVal);
break;
case VT_I4: //整型
case VT_I8: //長整型
strValue.Format("%d", var.lVal);
break;
case VT_UINT://無符號整型
strValue.Format("%d", var.uintVal);
break;
case VT_UI4: //無符號整型
case VT_UI8: //無符號長整型
strValue.Format("%d", var.ulVal);
break;
case VT_VOID:
strValue.Format("%8x", var.byref);
break;
case VT_R4://浮點型
strValue.Format("%.4f", var.fltVal);
break;
case VT_R8://雙精度型
strValue.Format("%.8f", var.dblVal);
break;
case VT_DECIMAL: //小數
strValue.Format("%.8f", (double)var);
break;
case VT_CY:
{
COleCurrency cy = var.cyVal;
strValue = cy.Format();
}
break;
case VT_BLOB:
case VT_BLOB_OBJECT:
case 0x2011:
strValue = "[BLOB]";
break;
case VT_BOOL://布爾型
strValue = var.boolVal ? "TRUE" : "FALSE";
break;
case VT_DATE: //日期型
{
DATE dt = var.date;
COleDateTime da = COleDateTime(dt);
strValue = da.Format("%Y-%m-%d");
}
break;
case VT_NULL://NULL值
case VT_EMPTY://空
strValue = "";
break;
case VT_UNKNOWN://未知類型
default:
strValue = "UN_KNOW";
break;
}
return strValue;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -