view.cpp
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:16k
- // [!output VIEW_IMPL] : implementation of the [!output VIEW_CLASS] class
- //
- #include "stdafx.h"
- #include "[!output APP_HEADER]"
- [!if OLEDB_RECORD_VIEW || ODBC_RECORD_VIEW]
- #include "[!output ROWSET_HEADER]"
- [!endif]
- #include "[!output DOC_HEADER]"
- [!if CONTAINER || CONTAINER_SERVER]
- #include "[!output CONTAINER_ITEM_HEADER]"
- [!endif]
- #include "[!output VIEW_HEADER]"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // [!output VIEW_CLASS]
- IMPLEMENT_DYNCREATE([!output VIEW_CLASS], [!output VIEW_BASE_CLASS])
- BEGIN_MESSAGE_MAP([!output VIEW_CLASS], [!output VIEW_BASE_CLASS])
- [!if PROJECT_STYLE_EXPLORER]
- [!if LIST_VIEW]
- ON_WM_STYLECHANGED()
- [!endif]
- [!endif]
- [!if CONTAINER || CONTAINER_SERVER]
- ON_WM_DESTROY()
- [!if !RICH_EDIT_VIEW]
- ON_WM_SETFOCUS()
- ON_WM_SIZE()
- ON_COMMAND(ID_OLE_INSERT_NEW, OnInsertObject)
- ON_COMMAND(ID_CANCEL_EDIT_CNTR, OnCancelEditCntr)
- ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
- [!else]
- [!if PRINTING]
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, &[!output VIEW_BASE_CLASS]::OnFilePrint)
- [!endif]
- [!endif]
- [!else]
- [!if PRINTING]
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, &[!output VIEW_BASE_CLASS]::OnFilePrint)
- [!endif]
- [!endif]
- [!if MINI_SERVER || FULL_SERVER || CONTAINER_SERVER]
- ON_COMMAND(ID_CANCEL_EDIT_SRVR, &[!output VIEW_CLASS]::OnCancelEditSrvr)
- [!endif]
- [!if PRINTING]
- [!if !HTML_VIEW && !HTML_EDITVIEW && !ACTIVE_DOC_CONTAINER]
- ON_COMMAND(ID_FILE_PRINT_DIRECT, &[!output VIEW_BASE_CLASS]::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, &[!output VIEW_BASE_CLASS]::OnFilePrintPreview)
- [!endif]
- [!if ACTIVE_DOC_CONTAINER]
- ON_COMMAND(ID_FILE_PRINT_DIRECT, &[!output VIEW_BASE_CLASS]::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview)
- ON_UPDATE_COMMAND_UI(ID_FILE_PRINT_PREVIEW, OnFilePrintPreviewUIUpdate)
- [!endif]
- [!endif]
- END_MESSAGE_MAP()
- [!if HTML_EDITVIEW]
- BEGIN_DHTMLEDITING_CMDMAP([!output VIEW_CLASS])
- DHTMLEDITING_CMD_ENTRY(ID_EDIT_COPY, IDM_COPY)
- DHTMLEDITING_CMD_ENTRY(ID_EDIT_CUT, IDM_CUT)
- DHTMLEDITING_CMD_ENTRY(ID_EDIT_PASTE, IDM_PASTE)
- DHTMLEDITING_CMD_ENTRY(ID_EDIT_UNDO, IDM_UNDO)
- END_DHTMLEDITING_CMDMAP()
- [!endif]
- // [!output VIEW_CLASS] construction/destruction
- [!output VIEW_CLASS]::[!output VIEW_CLASS]()
- [!if FORM_VIEW || OLEDB_RECORD_VIEW || ODBC_RECORD_VIEW]
- : [!output VIEW_BASE_CLASS]([!output VIEW_CLASS]::IDD)
- [!endif]
- {
- [!if ACCESSIBILITY]
- EnableActiveAccessibility();
- [!endif]
- [!if FORM_VIEW || OLEDB_RECORD_VIEW || ODBC_RECORD_VIEW]
- [!if OLEDB_RECORD_VIEW || ODBC_RECORD_VIEW]
- m_pSet = NULL;
- [!endif]
- [!endif]
- [!if CONTAINER || CONTAINER_SERVER]
- [!if !RICH_EDIT_VIEW]
- m_pSelection = NULL;
- [!endif]
- [!endif]
- // TODO: add construction code here
- }
- [!output VIEW_CLASS]::~[!output VIEW_CLASS]()
- {
- }
- [!if FORM_VIEW || OLEDB_RECORD_VIEW || ODBC_RECORD_VIEW]
- void [!output VIEW_CLASS]::DoDataExchange(CDataExchange* pDX)
- {
- [!output VIEW_BASE_CLASS]::DoDataExchange(pDX);
- [!if ODBC_RECORD_VIEW]
- // you can insert DDX_Field* functions here to 'connect' your controls to the database fields, ex.
- // DDX_FieldText(pDX, IDC_MYEDITBOX, m_pSet->m_szColumn1, m_pSet);
- // DDX_FieldCheck(pDX, IDC_MYCHECKBOX, m_pSet->m_bColumn2, m_pSet);
- // See MSDN and ODBC samples for more information
- [!endif]
- [!if OLEDB_RECORD_VIEW]
- // you can insert DDX_* functions, as well as SetDlgItem*/GetDlgItem* API calls to link your database to the view
- // ex. ::SetDlgItemText(m_hWnd, IDC_MYCONTROL, m_pSet->m_MyColumn);
- // See MSDN and OLEDB samples for more information
- [!endif]
- }
- [!endif]
- BOOL [!output VIEW_CLASS]::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- [!if EDIT_VIEW]
- BOOL bPreCreated = CEditView::PreCreateWindow(cs);
- cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL); // Enable word-wrapping
- return bPreCreated;
- [!else]
- return [!output VIEW_BASE_CLASS]::PreCreateWindow(cs);
- [!endif]
- }
- [!if !TREE_VIEW && !LIST_VIEW && !HTML_VIEW && !HTML_EDITVIEW && !RICH_EDIT_VIEW && !EDIT_VIEW && !FORM_VIEW && !OLEDB_RECORD_VIEW && !ODBC_RECORD_VIEW]
- // [!output VIEW_CLASS] drawing
- [!if CONTAINER || CONTAINER_SERVER]
- [!if !ACTIVE_DOC_CONTAINER]
- void [!output VIEW_CLASS]::OnDraw(CDC* pDC)
- {
- [!output DOC_CLASS]* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- // TODO: also draw all OLE items in the document
- // Draw the selection at an arbitrary position. This code should be
- // removed once your real drawing code is implemented. This position
- // corresponds exactly to the rectangle returned by [!output CONTAINER_ITEM_CLASS],
- // to give the effect of in-place editing.
- // TODO: remove this code when final draw code is complete.
- if (m_pSelection != NULL)
- {
- CSize size;
- CRect rect(10, 10, 210, 210);
- if (SUCCEEDED(m_pSelection->GetExtent(&size, m_pSelection->m_nDrawAspect)))
- {
- pDC->HIMETRICtoLP(&size);
- rect.right = size.cx + 10;
- rect.bottom = size.cy + 10;
- }
- m_pSelection->Draw(pDC, rect);
- }
- }
- [!else]
- void [!output VIEW_CLASS]::OnDraw(CDC* /*pDC*/)
- {
- [!output DOC_CLASS]* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- }[!endif]
- [!else]
- void [!output VIEW_CLASS]::OnDraw(CDC* /*pDC*/)
- {
- [!output DOC_CLASS]* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- }
- [!endif]
- [!endif]
- [!if PRINTING]
- [!if TREE_VIEW || LIST_VIEW]
- void [!output VIEW_CLASS]::OnDraw(CDC* /*pDC*/)
- {
- [!output DOC_CLASS]* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- }
- [!endif]
- [!endif]
- [!if SCROLL_VIEW || CONTAINER || CONTAINER_SERVER || OLEDB_RECORD_VIEW || LIST_VIEW || TREE_VIEW || FORM_VIEW || HTML_VIEW || ODBC_RECORD_VIEW]
- void [!output VIEW_CLASS]::OnInitialUpdate()
- {
- [!if ODBC_RECORD_VIEW]
- m_pSet = &GetDocument()->[!output ROWSET_CLASS_VARIABLE_NAME];
- [!endif]
- [!if OLEDB_RECORD_VIEW]
- m_pSet = &GetDocument()->[!output ROWSET_CLASS_VARIABLE_NAME];
- {
- CWaitCursor wait;
- HRESULT hr = m_pSet->OpenAll();
- if (FAILED(hr))
- {
- // Failed to open recordset. If the recordset is a
- // stored procedure, make sure that you have properly
- // initialized any input parameters before calling
- // the OpenAll() method.
- AfxMessageBox(_T("Record set failed to open."), MB_OK);
- // Disable the Next and Previous record commands,
- // since attempting to change the current record without an
- // open RowSet will cause a crash
- m_bOnFirstRecord = TRUE;
- m_bOnLastRecord = TRUE;
- }
- if( hr == DB_S_ENDOFROWSET )
- {
- // the rowset is empty (does not contain any rows)
- AfxMessageBox(_T("Record set opened but there were no rows to return."), MB_OK);
- // Disable the Next and Previous record commands
- m_bOnFirstRecord = TRUE;
- m_bOnLastRecord = TRUE;
- }
- }
- [!endif]
- [!output VIEW_BASE_CLASS]::OnInitialUpdate();
- [!if FORM_VIEW]
- [!if APP_TYPE_SDI]
- GetParentFrame()->RecalcLayout();
- [!endif]
- ResizeParentToFit();
- [!endif]
- [!if LIST_VIEW]
- // TODO: You may populate your ListView with items by directly accessing
- // its list control through a call to GetListCtrl().
- [!if TREE_VIEW]
- // TODO: You may populate your TreeView with items by directly accessing
- // its tree control through a call to GetTreeCtrl().
- [!if HTML_VIEW]
- // TODO: This code navigates to a popular spot on the web.
- // change the code to go where you'd like.
- [!endif]
- [!endif]
- [!endif]
- [!if HTML_VIEW]
- Navigate2(_T("http://www.msdn.microsoft.com/visualc/"),NULL,NULL);
- [!endif]
- [!if CONTAINER || CONTAINER_SERVER]
- [!if !RICH_EDIT_VIEW]
- // TODO: remove this code when final selection model code is written
- m_pSelection = NULL; // initialize selection
- [!endif]
- [!if RICH_EDIT_VIEW]
- // Set the printing margins (720 twips = 1/2 inch)
- SetMargins(CRect(720, 720, 720, 720));
- [!endif]
- [!endif]
- [!if SCROLL_VIEW]
- CSize sizeTotal;
- // TODO: calculate the total size of this view
- sizeTotal.cx = sizeTotal.cy = 100;
- SetScrollSizes(MM_TEXT, sizeTotal);
- [!endif]
- }
- [!endif]
- [!if PRINTING]
- // [!output VIEW_CLASS] printing
- [!if ACTIVE_DOC_CONTAINER]
- void [!output VIEW_CLASS]::OnFilePrintPreviewUIUpdate(CCmdUI* pCmdUI)
- {
- if (m_pSelection != NULL)
- {
- DWORD dwStatus = 0;
- if (SUCCEEDED(m_pSelection->QueryCommand(OLECMDID_PRINTPREVIEW, &dwStatus)) &&
- dwStatus & OLECMDF_ENABLED)
- {
- pCmdUI->Enable(TRUE);
- return;
- }
- }
- pCmdUI->Enable(FALSE);
- }
- void [!output VIEW_CLASS]::OnFilePrintPreview()
- {
- if (!m_pSelection)
- return;
- m_pSelection->ExecCommand(OLECMDID_PRINTPREVIEW);
- }
- [!endif]
- [!if !HTML_VIEW && !HTML_EDITVIEW]
- BOOL [!output VIEW_CLASS]::OnPreparePrinting(CPrintInfo* pInfo)
- {
- [!if EDIT_VIEW]
- // default CEditView preparation
- return CEditView::OnPreparePrinting(pInfo);
- [!else]
- [!if ACTIVE_DOC_CONTAINER]
- if (!CView::DoPreparePrinting(pInfo))
- return FALSE;
- if (!COleDocObjectItem::OnPreparePrinting(this, pInfo))
- return FALSE;
- return TRUE;
- [!else]
- // default preparation
- return DoPreparePrinting(pInfo);
- [!endif]
- [!endif]
- }
- [!if !RICH_EDIT_VIEW]
- [!if EDIT_VIEW]
- void [!output VIEW_CLASS]::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
- [!else]
- void [!output VIEW_CLASS]::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- [!endif]
- {
- [!if EDIT_VIEW]
- // Default CEditView begin printing
- CEditView::OnBeginPrinting(pDC, pInfo);
- [!else]
- // TODO: add extra initialization before printing
- [!endif]
- }
- [!if EDIT_VIEW]
- void [!output VIEW_CLASS]::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
- [!else]
- void [!output VIEW_CLASS]::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- [!endif]
- {
- [!if EDIT_VIEW]
- // Default CEditView end printing
- CEditView::OnEndPrinting(pDC, pInfo);
- [!else]
- // TODO: add cleanup after printing
- [!endif]
- }
- [!endif]
- [!endif]
- [!if FORM_VIEW || ACTIVE_DOC_CONTAINER]
- [!if ACTIVE_DOC_CONTAINER]
- void [!output VIEW_CLASS]::OnPrint(CDC* pDC, CPrintInfo* pInfo)
- [!else]
- void [!output VIEW_CLASS]::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
- [!endif]
- {
- // TODO: add customized printing code here
- [!if ACTIVE_DOC_CONTAINER]
- if(pInfo->m_bDocObject)
- COleDocObjectItem::OnPrint(this, pInfo, TRUE);
- else
- CView::OnPrint(pDC, pInfo);
- [!endif]
- }
- [!endif]
- [!endif]
- [!if CONTAINER || CONTAINER_SERVER]
- void [!output VIEW_CLASS]::OnDestroy()
- {
- // Deactivate the item on destruction; this is important
- // when a splitter view is being used
- COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
- if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
- {
- pActiveItem->Deactivate();
- ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
- }
- [!output VIEW_BASE_CLASS]::OnDestroy();
- }
- [!if !RICH_EDIT_VIEW]
- // OLE Client support and commands
- BOOL [!output VIEW_CLASS]::IsSelected(const CObject* pDocItem) const
- {
- // The implementation below is adequate if your selection consists of
- // only [!output CONTAINER_ITEM_CLASS] objects. To handle different selection
- // mechanisms, the implementation here should be replaced
- // TODO: implement this function that tests for a selected OLE client item
- return pDocItem == m_pSelection;
- }
- void [!output VIEW_CLASS]::OnInsertObject()
- {
- // Invoke the standard Insert Object dialog box to obtain information
- // for new [!output CONTAINER_ITEM_CLASS] object
- COleInsertDialog dlg;
- [!if ACTIVE_DOC_CONTAINER]
- if (dlg.DoModal(COleInsertDialog::DocObjectsOnly) != IDOK)
- return;
- [!else]
- if (dlg.DoModal() != IDOK)
- return;
- [!endif]
- BeginWaitCursor();
- [!output CONTAINER_ITEM_CLASS]* pItem = NULL;
- TRY
- {
- // Create new item connected to this document
- [!output DOC_CLASS]* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- pItem = new [!output CONTAINER_ITEM_CLASS](pDoc);
- ASSERT_VALID(pItem);
- // Initialize the item from the dialog data
- if (!dlg.CreateItem(pItem))
- AfxThrowMemoryException(); // any exception will do
- ASSERT_VALID(pItem);
- [!if ACTIVE_DOC_CONTAINER]
- pItem->DoVerb(OLEIVERB_SHOW, this);
- [!else]
- if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)
- pItem->DoVerb(OLEIVERB_SHOW, this);
- [!endif]
- ASSERT_VALID(pItem);
- // As an arbitrary user interface design, this sets the selection
- // to the last item inserted
- // TODO: reimplement selection as appropriate for your application
- m_pSelection = pItem; // set selection to last inserted item
- pDoc->UpdateAllViews(NULL);
- }
- CATCH(CException, e)
- {
- if (pItem != NULL)
- {
- ASSERT_VALID(pItem);
- pItem->Delete();
- }
- AfxMessageBox(IDP_FAILED_TO_CREATE);
- }
- END_CATCH
- EndWaitCursor();
- }
- // The following command handler provides the standard keyboard
- // user interface to cancel an in-place editing session. Here,
- // the container (not the server) causes the deactivation
- void [!output VIEW_CLASS]::OnCancelEditCntr()
- {
- // Close any in-place active item on this view.
- COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
- if (pActiveItem != NULL)
- {
- pActiveItem->Close();
- }
- ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
- }
- // Special handling of OnSetFocus and OnSize are required for a container
- // when an object is being edited in-place
- void [!output VIEW_CLASS]::OnSetFocus(CWnd* pOldWnd)
- {
- COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
- if (pActiveItem != NULL &&
- pActiveItem->GetItemState() == COleClientItem::activeUIState)
- {
- // need to set focus to this item if it is in the same view
- CWnd* pWnd = pActiveItem->GetInPlaceWindow();
- if (pWnd != NULL)
- {
- pWnd->SetFocus(); // don't call the base class
- return;
- }
- }
- [!output VIEW_BASE_CLASS]::OnSetFocus(pOldWnd);
- }
- void [!output VIEW_CLASS]::OnSize(UINT nType, int cx, int cy)
- {
- [!output VIEW_BASE_CLASS]::OnSize(nType, cx, cy);
- COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
- if (pActiveItem != NULL)
- pActiveItem->SetItemRects();
- }
- void [!output VIEW_CLASS]::OnFilePrint()
- {
- //By default, we ask the Active document to print itself
- //using IOleCommandTarget. If you don't want this behavior
- //remove the call to COleDocObjectItem::DoDefaultPrinting.
- //If the call fails for some reason, we will try printing
- //the docobject using the IPrint interface.
- CPrintInfo printInfo;
- ASSERT(printInfo.m_pPD != NULL);
- if (S_OK == COleDocObjectItem::DoDefaultPrinting(this, &printInfo))
- return;
- CView::OnFilePrint();
- }
- [!endif]
- [!endif]
- [!if MINI_SERVER || FULL_SERVER || CONTAINER_SERVER]
- // OLE Server support
- // The following command handler provides the standard keyboard
- // user interface to cancel an in-place editing session. Here,
- // the server (not the container) causes the deactivation
- void [!output VIEW_CLASS]::OnCancelEditSrvr()
- {
- GetDocument()->OnDeactivateUI(FALSE);
- }
- [!endif]
- // [!output VIEW_CLASS] diagnostics
- #ifdef _DEBUG
- void [!output VIEW_CLASS]::AssertValid() const
- {
- [!output VIEW_BASE_CLASS]::AssertValid();
- }
- void [!output VIEW_CLASS]::Dump(CDumpContext& dc) const
- {
- [!output VIEW_BASE_CLASS]::Dump(dc);
- }
- [!output DOC_CLASS]* [!output VIEW_CLASS]::GetDocument() const // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS([!output DOC_CLASS])));
- return ([!output DOC_CLASS]*)m_pDocument;
- }
- #endif //_DEBUG
- [!if OLEDB_RECORD_VIEW]
- // [!output VIEW_CLASS] database support
- CRowset<>* [!output VIEW_CLASS]::OnGetRowset()
- {
- return m_pSet->GetRowsetBase();
- }
- [!endif]
- [!if ODBC_RECORD_VIEW]
- // [!output VIEW_CLASS] database support
- CRecordset* [!output VIEW_CLASS]::OnGetRecordset()
- {
- return m_pSet;
- }
- [!endif]
- // [!output VIEW_CLASS] message handlers
- [!if PROJECT_STYLE_EXPLORER]
- [!if LIST_VIEW]
- void [!output VIEW_CLASS]::OnStyleChanged(int /*nStyleType*/, LPSTYLESTRUCT /*lpStyleStruct*/)
- {
- //TODO: add code to react to the user changing the view style of your window
- }
- [!endif]
- [!endif]