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

📄 treedoc.cpp

📁 API经典入门
💻 CPP
字号:
// treedoc.cpp : implementation of the CTreeDoc class
//

#include "stdafx.h"
#include "tree.h"

#include "treeset.h"
#include "treedoc.h"
#include "persondl.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTreeDoc

IMPLEMENT_DYNCREATE(CTreeDoc, CDocument)

BEGIN_MESSAGE_MAP(CTreeDoc, CDocument)
	//{{AFX_MSG_MAP(CTreeDoc)
	ON_COMMAND(ID_INFO_RECORDSET, OnInfoRecordset)
	ON_COMMAND(ID_INFO_PERSON, OnInfoPerson)
	ON_UPDATE_COMMAND_UI(ID_RECORD_EDIT, OnUpdateRecordEdit)
	ON_COMMAND(ID_RECORD_EDIT, OnRecordEdit)
	ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
	ON_UPDATE_COMMAND_UI(ID_RECORD_ADD, OnUpdateRecordAdd)
	ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)
	ON_UPDATE_COMMAND_UI(ID_RECORD_DELETE, OnUpdateRecordDelete)
	ON_COMMAND(ID_PORTRAIT_LOADFROMFILE, OnPortraitLoadfromfile)
	ON_UPDATE_COMMAND_UI(ID_PORTRAIT_LOADFROMFILE, OnUpdatePortraitLoadfromfile)
	ON_COMMAND(ID_PORTRAIT_DELETE, OnPortraitDelete)
	ON_UPDATE_COMMAND_UI(ID_PORTRAIT_DELETE, OnUpdatePortraitDelete)
	ON_COMMAND(ID_PORTRAIT_SAVETOFILE, OnPortraitSavetofile)
	ON_UPDATE_COMMAND_UI(ID_PORTRAIT_SAVETOFILE, OnUpdatePortraitSavetofile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTreeDoc construction/destruction

CTreeDoc::CTreeDoc()
{
}

CTreeDoc::~CTreeDoc()
{
}

BOOL CTreeDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}

void CTreeDoc::EditCurrentPerson()
{
	// the user positions the record set at the 
	// desired record.

	// make sure the record set is open and updateable
	if ( !(m_treeSet.IsOpen() && m_treeSet.CanUpdate()) )
	{
		TRACE0("m_treeSet not ready in EditCurrentPerson().\n");
		return;
	}
	
	// alert the record set that changes may occur
	m_treeSet.Edit();

	CPersonDlg Dlg;
	
	Dlg.m_pSet = &m_treeSet;
	
	// make changes via dialog transfers
	// accept changes if OK or abort changes if CANCEL
	if (Dlg.DoModal() == IDOK)
		m_treeSet.Update();
	else
		m_treeSet.Move(AFX_MOVE_REFRESH);
		
}

double CTreeDoc::GetUniqueRandomID()
{
	// Assume srand() has already been called to 
	// seed the random number generator.
	//CTime time = CTime::GetCurrentTime();
	//srand(time.GetTime());
	
	// First set up the record set with ID param
	CTreeSet TS(m_treeSet.m_pDatabase);
	TS.m_strFilter = "ID = ?";

	// Next place an appropriately sized random
	// number in the parameter.
	TS.m_fIDParam = (double)rand() * 3 + 10000;
	
	if ( TS.Open() )
	{
		// if any records are selected then
		// the ID is NOT unique hence increment
		// the ID until it is unique.
		while ( !(TS.IsEOF() && TS.IsBOF()) )
		{
			TS.m_fIDParam++;
			TS.Requery();
		}
	
		TS.Close();
	}
	
	// return the unique ID
	return TS.m_fIDParam;
}

BOOL CTreeDoc::MoveTo (double ID)
{
	CTreeSet TS(m_treeSet.m_pDatabase);
	TS.m_strFilter = "ID = ?";
	TS.m_fIDParam = ID;
	if (TS.Open())
	{
		if (TS.IsBOF() && TS.IsEOF())
			return FALSE; // ID not valid
		TS.Close();
	}
	else
	{
		TRACE0("MoveTo failed upon tree set open.\n");
		return FALSE;
	} 
	
	// At this point we know that the ID exists
	// in the record set.
	
	// Make sure that m_treeSet has records in it.
	// It could have a different filter.
	if ( m_treeSet.IsBOF() && m_treeSet.IsEOF() )
		return FALSE;

	m_treeSet.MoveFirst();
	while ( !m_treeSet.IsEOF() )
	{
		if (m_treeSet.m_ID == ID)
		{
			UpdateAllViews(NULL);
			return TRUE;
		}
		else
			m_treeSet.MoveNext();
	}

	// if the ID is not found there must be an inconsistancy
	// between m_treeSet and TS.  So ...
	TRACE0("Record Set Inconsistancy Error!\n");
	ASSERT(FALSE);  // abort the program when in the debugger.

	// If the application is in a release stage (i.e. no debugger), then
	// alert the user of the problem and return false.
	AfxMessageBox("Record Set Inconsistancy Error in "
			"CTreeDoc::MoveTo().");
	return FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// CTreeDoc serialization

void CTreeDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CTreeDoc diagnostics

#ifdef _DEBUG
void CTreeDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CTreeDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTreeDoc commands

void CTreeDoc::OnInfoRecordset()
{
	TRACE("Recordset's associated table: %s\n", 
				m_treeSet.GetTableName());
	TRACE("Recordset's SQL statement: \n%s\n", 
				m_treeSet.GetSQL());

	TRACE0("\nRecordset Attributes:\n");
	if (m_treeSet.IsOpen())
		TRACE0("Recordset is open.\n");
	else
		TRACE0("Recordset is NOT open.\n");
		
	if (m_treeSet.CanAppend())
		TRACE0("Can Append\n");
	else
		TRACE0("Can NOT Append\n");
		
	if (m_treeSet.CanRestart())
		TRACE0("Can Restart\n");
	else
		TRACE0("Can NOT Restart\n");

	if (m_treeSet.CanScroll())
		TRACE0("Can Scroll\n");
	else
		TRACE0("Can NOT Scroll\n");

	if (m_treeSet.CanTransact())
		TRACE0("Can Transact\n");
	else
		TRACE0("Can NOT Transact\n");

	if (m_treeSet.CanUpdate())
		TRACE0("Can Update\n");
	else
		TRACE0("Can NOT Update\n");
		
	CRecordsetStatus rssObj;
	m_treeSet.GetStatus(rssObj);
	TRACE("The index of the current record is: %li\n", 
			rssObj.m_lCurrentRecord);
	
	CString S = "%li is the record count and that's ";
	if (rssObj.m_bRecordCountFinal)
		S = S + "final\n";
	else
		S = S + "NOT final\n";
		
	TRACE (S, m_treeSet.GetRecordCount());
	
	if (m_treeSet.IsBOF())
		TRACE0("Current position is BOF\n");
	if (m_treeSet.IsEOF())
		TRACE0("Current position is EOF\n");
	
	TRACE1("BLOB size = %lu.\n", m_treeSet.m_PORTRAIT.m_dwDataLength);
	if (m_treeSet.m_PORTRAIT.m_hData == NULL)
		TRACE0("BLOB handle is NULL.\n");
	else
		TRACE0("BLOB handle is NOT NULL.\n");	
}

void CTreeDoc::OnInfoPerson()
{
	// randomizer initialized in doc constructor
	//CTime time = CTime::GetCurrentTime();
	//srand((int)time.GetTime());
	TRACE1("RAND_MAX = %i.\n", RAND_MAX);
	TRACE0("Random #'s:\n");
	long lR, lmR;
	for (int i = 0; i < 10; i++)
	{
		lR = (long)rand();
		lmR = lR * 3 + 10000;
		TRACE2("     %li     %li\n", lR, lmR);
	}	
}

void CTreeDoc::OnRecordEdit()
{
	EditCurrentPerson();	
}

void CTreeDoc::OnUpdateRecordEdit(CCmdUI* pCmdUI)
{
	// Enable the command if the reocrd set is open
	// and also has available records
	BOOL bActivate = m_treeSet.IsOpen() && 
		!(m_treeSet.IsBOF() && m_treeSet.IsEOF());
		
	pCmdUI->Enable( bActivate );
}

void CTreeDoc::OnRecordDelete()
{
	if (!m_treeSet.CanUpdate())
	{
		TRACE0("m_treeSet is not useable in OnRecordDelete().\n");
		return;
	}

	CString S = "Do you really want to delete: " + m_treeSet.m_FIRST
			 + " " + m_treeSet.m_LAST + "?";
	if (AfxMessageBox(S, MB_YESNO) == IDYES)
	{
		m_treeSet.Delete();
		
		m_treeSet.MoveNext();
		if (m_treeSet.IsEOF())
		{
			m_treeSet.Requery();
			// Requery is not usually necessary but with
			// the selected driver, deleting the last record
			// can result in a corrupted state.
			m_treeSet.MoveLast();
		}

		UpdateAllViews(NULL);
	}
	
}

void CTreeDoc::OnUpdateRecordDelete(CCmdUI* pCmdUI)
{
	// Enable the command if the reocrd set is open
	// and also has available records
	BOOL bActivate = m_treeSet.IsOpen() && 
		!(m_treeSet.IsBOF() && m_treeSet.IsEOF());
		
	pCmdUI->Enable( bActivate );
}

void CTreeDoc::OnRecordAdd()
{
TRY
{

	// make sure the record set is updateable
	if ( !m_treeSet.CanUpdate() )
	{
		TRACE0("m_treeSet not updateable.\n");
		return;
	}
	
	// alert the record set that we are trying to add a record
	m_treeSet.AddNew();
	CPersonDlg Dlg;
	
	Dlg.m_pSet = &m_treeSet;

	// fill in record via dialog transfers
	// accept record if OK or abort the add if CANCEL
	if (Dlg.DoModal() == IDOK)
	{
		m_treeSet.m_ID = GetUniqueRandomID();
		m_treeSet.Update();
		m_treeSet.Requery();
		UpdateAllViews(NULL);
	}
	else
		m_treeSet.Move(AFX_MOVE_REFRESH);

}	
CATCH( CDBException, e )
{
	AfxMessageBox ("Add failed upon exception!");
}
END_CATCH

}

void CTreeDoc::OnUpdateRecordAdd(CCmdUI* pCmdUI)
{
	// Enable the command if the reocrd set is open
	BOOL bActivate = m_treeSet.IsOpen(); 
		
	pCmdUI->Enable( bActivate );
	
}



void CTreeDoc::OnPortraitLoadfromfile()
{
	CFileDialog dlg(TRUE, NULL, "C:\\WINDOWS\\*.bmp");
	CDIBitmap DIB;
	
	if (dlg.DoModal() == IDOK)
	{
		BeginWaitCursor();
		
		// Get the file name selected in the file dialog.
		if ( !DIB.LoadFromDIB(dlg.GetPathName()) )
		{
			// Failed to load DIB
			AfxMessageBox("Couldn't load DIB!");
			EndWaitCursor();
			return;
		}
		
		// Prepare the record set for editing.
		m_treeSet.Edit();
		
		// Transfer the DIB data to the CLongBinary object.
		if ( !DIB.SaveToHandle(m_treeSet.m_PORTRAIT.m_hData) )
		{
			// Failed to Save the DIB
			EndWaitCursor();
			return;
		}
		m_treeSet.m_PORTRAIT.m_dwDataLength = DIB.DIBDataSize();
		
		// Tell the database that we changed the field.
		m_treeSet.SetFieldDirty(&m_treeSet.m_PORTRAIT);
		m_treeSet.SetFieldNull(&m_treeSet.m_PORTRAIT, FALSE);
		
		// Commit the changes (write them to the db).
		m_treeSet.Update();
		
		EndWaitCursor();
	}

	// Tell all the attached views that the data changed.	
	UpdateAllViews(NULL);
}

void CTreeDoc::OnUpdatePortraitLoadfromfile(CCmdUI* pCmdUI)
{
	// disable the load portrait command when ... 
	//    -- the record set is not open
	//    -- no current record exists
	//    -- a portrait already exists for the current record
	
	BOOL bDisable = !m_treeSet.IsOpen() || 
			m_treeSet.IsEOF() || m_treeSet.IsBOF() || 
			(m_treeSet.m_PORTRAIT.m_dwDataLength != 0);
				 
	// Note: Yes I did use a logical OR instead of AND above
	// since I'm testing to see if any record is selected as
	// opposed to testing for an empty database.
	
	pCmdUI->Enable(!bDisable);
}

void CTreeDoc::OnPortraitDelete()
{
	BeginWaitCursor();

	// Prepare the record set for editing.
	m_treeSet.Edit();

	// Free the memory associated with the bitmap.
	//::GlobalFree(m_treeSet.m_PORTRAIT.m_hData);
	//m_treeSet.m_PORTRAIT.m_hData = NULL;
	m_treeSet.m_PORTRAIT.m_dwDataLength = 0;

	// Tell the database the bitmap is deleted.
	m_treeSet.SetFieldNull(&m_treeSet.m_PORTRAIT, TRUE);
	m_treeSet.SetFieldDirty(&m_treeSet.m_PORTRAIT, TRUE);

	// Commit the changes (write them to the db).
	m_treeSet.Update();

	EndWaitCursor();
}

void CTreeDoc::OnUpdatePortraitDelete(CCmdUI* pCmdUI)
{
	// disable the load portrait command when ...
	//    -- the record set is not open
	//    -- no current record exists
	//    -- a portrait doesn't exist for the current record
	
	BOOL bDisable = !m_treeSet.IsOpen() ||
			m_treeSet.IsEOF() || m_treeSet.IsBOF()
			|| (m_treeSet.m_PORTRAIT.m_dwDataLength == 0);

	// Note: Yes I did use a logical OR instead of AND above
	// since I'm testing to see if any record is selected as
	// opposed to testing for an empty database.
	
	pCmdUI->Enable(!bDisable);
}

void CTreeDoc::OnPortraitSavetofile()
{
	CFileDialog dlg(FALSE, "bmp", "Face.bmp");
	CDIBitmap DIB;
	
	if (dlg.DoModal() == IDOK)
	{
		BeginWaitCursor();
		
		// Get the file name selected in the file dialog
		// and Save the DIB to that file.
		if (!DIB.SaveToDIB(dlg.GetPathName()))
			TRACE0("Portrait NOT saved!!\n");
		
		EndWaitCursor();
	}
}

void CTreeDoc::OnUpdatePortraitSavetofile(CCmdUI* pCmdUI)
{
	// disable the load portrait command when ...
	//    -- no current record exists
	//    -- a portrait doesn't exist for the current record
	
	BOOL bDisable = !m_treeSet.IsOpen()
			|| m_treeSet.IsEOF() || m_treeSet.IsBOF()
			|| (m_treeSet.m_PORTRAIT.m_dwDataLength == 0);

	// Note: Yes I did use a logical OR instead of AND above
	// since I'm testing to see if any record is selected as
	// opposed to testing for an empty database.
	
	pCmdUI->Enable(!bDisable);
}

⌨️ 快捷键说明

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