OLE_DATABASEVIEW.CPP
上传用户:sygkzy
上传日期:2013-03-13
资源大小:150k
文件大小:8k
- // ole_databaseView.cpp : implementation of the COle_databaseView class
- //
- #include "stdafx.h"
- #include "ole_database.h"
- #include "ole_databaseDoc.h"
- #include "ole_databaseView.h"
- #include "connect.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // COle_databaseView
- IMPLEMENT_DYNCREATE(COle_databaseView, CView)
- BEGIN_MESSAGE_MAP(COle_databaseView, CView)
- //{{AFX_MSG_MAP(COle_databaseView)
- ON_COMMAND(ID_CONNECT, OnConnect)
- ON_UPDATE_COMMAND_UI(ID_CONNECT, OnUpdateConnect)
- ON_COMMAND(ID_DISCONNECT, OnDisconnect)
- ON_UPDATE_COMMAND_UI(ID_DISCONNECT, OnUpdateDisconnect)
- ON_COMMAND(ID_EXECUTESQL, OnExecutesql)
- ON_UPDATE_COMMAND_UI(ID_EXECUTESQL, OnUpdateExecutesql)
- ON_WM_ERASEBKGND()
- ON_WM_SIZE()
- ON_WM_LBUTTONDOWN()
- ON_COMMAND(ID_SELECTTABLE, OnSelecttable)
- ON_UPDATE_COMMAND_UI(ID_SELECTTABLE, OnUpdateSelecttable)
- ON_COMMAND(ID_CLEARESQL, OnClearesql)
- ON_UPDATE_COMMAND_UI(ID_CLEARESQL, OnUpdateClearesql)
- ON_COMMAND(ID_CLEARRESULT, OnClearresult)
- ON_UPDATE_COMMAND_UI(ID_CLEARRESULT, OnUpdateClearresult)
- ON_COMMAND(ID_FILE_NEW, OnFileNew)
- ON_COMMAND(ID_FILE_SAVE, OnFileSave)
- ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // COle_databaseView construction/destruction
- COle_databaseView::COle_databaseView()
- {
- OStartup();
- }
- COle_databaseView::~COle_databaseView()
- {
- OShutdown();
- }
- BOOL COle_databaseView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- //cs.style&= ~FWS_ADDTOTITLE ;
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // COle_databaseView drawing
- void COle_databaseView::OnDraw(CDC* pDC)
- {
- COle_databaseDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- }
- /////////////////////////////////////////////////////////////////////////////
- // COle_databaseView printing
- BOOL COle_databaseView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void COle_databaseView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void COle_databaseView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // COle_databaseView diagnostics
- #ifdef _DEBUG
- void COle_databaseView::AssertValid() const
- {
- CView::AssertValid();
- }
- void COle_databaseView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- COle_databaseDoc* COle_databaseView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COle_databaseDoc)));
- return (COle_databaseDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // COle_databaseView message handlers
- BOOL COle_databaseView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
- {
- CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
-
- m_sqledit.Create(WS_CHILD|WS_VISIBLE|ES_MULTILINE|
- ES_AUTOVSCROLL|WS_EX_RIGHTSCROLLBAR|WS_BORDER,CRect(0,0,0,0),this,ID_SQLEDIT);
-
- m_flexgrid.Create("ABC",WS_CHILD|WS_VISIBLE,CRect(0,0,0,0),
- this,ID_FLEXGRID,NULL,FALSE);
- m_flexgrid.SetAllowUserResizing(1);
- OnClearresult();
- return TRUE;
- }
- BOOL COle_databaseView::OnCommand(WPARAM wParam, LPARAM lParam)
- {
- switch(LOWORD(wParam))
- {
- case ID_SQLDATA :
- //switch(HIWORD(wParam)
- // case WM_C
- break;
- default:
- return CView::OnCommand(wParam, lParam);
- }
- return CView::OnCommand(wParam, lParam);
- }
- BOOL COle_databaseView::DestroyWindow()
- {
- if(odb.IsOpen())
- {
- odb.Close();
- }
- return CView::DestroyWindow();
- }
- void COle_databaseView::OnConnect()
- {
- CConnect m_connect;
- CString m_dbname;
- CString m_username;
- CString m_userpsw;
-
- if(m_connect.DoModal()!=IDOK)
- return;
- m_connect.GetData(m_dbname,m_username,m_userpsw);
-
- odb.Open(m_dbname,m_username,m_userpsw);
- if(!odb.IsOpen())
- {
- const char *m_errortext=odb.GetErrorText();
- this->MessageBox(m_errortext,"错误",MB_OK|MB_ICONERROR);
- return ;
- }
- }
- void COle_databaseView::OnUpdateConnect(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(!odb.IsOpen());
- }
- void COle_databaseView::OnDisconnect()
- {
- if (!odb.IsOpen())
- {
- ::MessageBox(this->m_hWnd," the database is closed!","error",MB_OK);
- }else
- {
- odb.Close();
- }
- }
- void COle_databaseView::OnUpdateDisconnect(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(odb.IsOpen());
- }
- void COle_databaseView::OnExecutesql()
- {
- ///DeleteFromListBox();
- CString m_sSql,m_tmp,m_tmp2;
- int m_fieldcount,m_recordcount,m_i;
- m_sqledit.GetWindowText(m_sSql);
- odyn.Open(odb,m_sSql);
- m_fieldcount=odyn.GetFieldCount();
-
- //加入列名
- m_flexgrid.SetCols(m_fieldcount+1);
- m_flexgrid.SetTextMatrix(0,0,"序号");
- for(int i=0;i<m_fieldcount;i++)
- {
- m_flexgrid.SetTextMatrix(0,i+1,odyn.GetFieldOriginalName(i));
- }
- //加入数据
- i=1;
- m_recordcount=odyn.GetRecordCount();
- m_flexgrid.SetRows(m_recordcount+1);
- while(!odyn.IsEOF())
- {
- char x[10];
- ::itoa(i,x,10);
- m_flexgrid.SetTextMatrix(i,0,x);
- for(m_i=0;m_i<m_fieldcount;m_i++)
- {
- odyn.GetFieldValue(m_i,&ovalue);
- m_flexgrid.SetTextMatrix(i,m_i+1,&(*ovalue));
- }
- odyn.MoveNext();
- i++;
- }
- }
- void COle_databaseView::OnUpdateExecutesql(CCmdUI* pCmdUI)
- {
- CString m_tmp;
- m_sqledit.GetWindowText(m_tmp);
- pCmdUI->Enable(!m_tmp.IsEmpty());
- }
- BOOL COle_databaseView::OnEraseBkgnd(CDC* pDC)
- {
-
- // Set brush to desired background color
- CBrush backBrush(RGB(148, 148, 148));
-
- // Save old brush
- CBrush* pOldBrush = pDC->SelectObject(&backBrush);
- CRect rect;
- pDC->GetClipBox(&rect);
-
- // Erase the area needed
- pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
- pDC->SelectObject(pOldBrush);
- //CView::OnEraseBkgnd(pDC);
- return TRUE;
- }
- void COle_databaseView::OnSize(UINT nType, int cx, int cy)
- {
- CView::OnSize(nType, cx, cy);
-
- ::SetWindowPos(m_sqledit.m_hWnd,HWND_TOP,0,0,cx,cy/3*1,SWP_SHOWWINDOW);
- ::SetWindowPos(m_flexgrid.m_hWnd,HWND_TOP,0,cy/3*1,cx,cy/3*2,SWP_SHOWWINDOW);
- }
- void COle_databaseView::OnLButtonDown(UINT nFlags, CPoint point)
- {
-
- CView::OnLButtonDown(nFlags, point);
- }
- void COle_databaseView::OnSelecttable()
- {
- CSelectTable m_selecttable;
- m_selecttable.odb=this->odb;
- if(m_selecttable.DoModal()!=IDOK)
- return;
- m_sqledit.SetWindowText(m_selecttable.m_sSql);
- }
- void COle_databaseView::OnUpdateSelecttable(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(odb.IsOpen());
- }
- void COle_databaseView::OnClearesql()
- {
- m_sqledit.SetWindowText("");
- }
- void COle_databaseView::OnUpdateClearesql(CCmdUI* pCmdUI)
- {
- CString m_tmp;
- m_sqledit.GetWindowText(m_tmp);
- pCmdUI->Enable(!m_tmp.IsEmpty());
- }
- void COle_databaseView::OnClearresult()
- {
- m_flexgrid.Clear();
- m_flexgrid.SetCols(1);
- m_flexgrid.SetRows(1);
- }
- void COle_databaseView::OnUpdateClearresult(CCmdUI* pCmdUI)
- {
- }
- void COle_databaseView::OnFileNew()
- {
- if(m_sqledit.GetModify())
- {
- if(this->MessageBox("数据已经被更改,是否要保存?","提示",MB_YESNO)==IDYES)
- {
- OnFileSave();
- m_sqledit.SetModify(FALSE);
- }
- OnClearesql();
- OnClearresult();
- }
-
- }
- void COle_databaseView::OnFileSave()
- {
- // TODO: Add your command handler code here
-
- }
- void COle_databaseView::OnFileOpen()
- {
- if(m_sqledit.GetModify())
- {
- if(this->MessageBox("数据已经被更改,是否要保存?","提示",MB_YESNO)==IDYES)
- {
- OnFileSave();
- m_sqledit.SetModify(FALSE);
- }
- }
- }