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

📄 rfidlg.cpp

📁 一个使用MFC和SQL的例子
💻 CPP
📖 第 1 页 / 共 2 页
字号:
}

// sorted database entries
void CRfiDlg::LoadSortList ( CString& strSortBy )
{
	// clear out the listbox
	m_lcRepeaterList . DeleteAllItems ();
	
	m_setComplete . m_strSort = strSortBy; // set the recordset's sort parameter
	LoadList (); // re-load the database contents

	return;
}

// load based on SQL filter query
void CRfiDlg::LoadFilterList ( CString& strFilterBy )
{
	// clear out the listbox
	m_lcRepeaterList . DeleteAllItems ();
	
	m_setComplete . m_strFilter = strFilterBy; // set the recordset's filter parameter
	LoadList (); // re-load the database contents

	return;
}

// loads the main listbox with the contents of the database
void CRfiDlg::LoadList()
{
	char szBuffer [ 256 ]; // idiot buffer needed by the runtime library calls

	// clear out the listbox
	m_lcRepeaterList . DeleteAllItems ();

	try {
		if ( ! m_setComplete . IsOpen () ) // if the recordset isn't already open...
			m_setComplete . Open (); // open it
		for ( int nListEntry = 0 ; ! m_setComplete . IsEOF () ; nListEntry ++ ) { // for each entry int the database...
			// make an entry in the listbox from the recordset contents
			m_lcRepeaterList . InsertItem ( nListEntry, LPCTSTR ( ltoa ( m_setComplete . m_lID, szBuffer, 10 ) ), 0 );
			m_lcRepeaterList . SetItemText ( nListEntry, 1, LPCTSTR ( m_setComplete . m_strFrequency ) );
			m_lcRepeaterList . SetItemText ( nListEntry, 2, LPCTSTR ( m_setComplete . m_strCallsign ) );
			m_lcRepeaterList . SetItemText ( nListEntry, 3, LPCTSTR ( m_setComplete . m_strInput ) );
			m_lcRepeaterList . SetItemText ( nListEntry, 4, LPCTSTR ( m_setComplete . m_strCity ) );
			m_lcRepeaterList . SetItemText ( nListEntry, 5, LPCTSTR ( m_setComplete . m_strState ) );
			m_setComplete . MoveNext (); // cause the recordset to retrieve the next record from the database for the next loop
		}
		m_setComplete . Close (); // done with the recordset
	}
	catch ( CDaoException* e ) { // catch exceptions thrown by the MFC DAO classes
		char szBuffer [ 256 ];
		CString strExceptDesc =
			CString ( "JET Database Engine Error:\n\n Error Code: " ) +
			CString ( ltoa ( e -> m_pErrorInfo -> m_lErrorCode, szBuffer, 10 ) ) +
			CString ( "\nDescription: " ) +
			CString ( e -> m_pErrorInfo -> m_strDescription );
		AfxMessageBox ( strExceptDesc, MB_ICONEXCLAMATION );
		m_setComplete . Close (); // at least try to close the recordset
		return;
	}

	return;
}

// build an SQL filter query string based on contents of main dialog's filter parameter edit boxes
// and droplist boxes.  Wildcards and Regular expression syntax is supported by the SQL "like" keyword
// upon which this query will be built.
void CRfiDlg::OnExecuteQuery() 
{
	CString strCallsignQuery = "", strFrequencyQuery = "", strCityQuery = "";
	CString strStateQuery = "", strInputQuery = "";
	CString strFilter = "";
	BOOL bFirstSQLOperand =	TRUE;

	// get the contents of the main dialog's query parameter entry elements.
	GetDlgItemText ( IDC_QUERY_CALLSIGN, strCallsignQuery );
	GetDlgItemText ( IDC_QUERY_FREQUENCY, strFrequencyQuery );
	GetDlgItemText ( IDC_QUERY_CITY, strCityQuery );
	if ( ( ( ( CComboBox* ) GetDlgItem ( IDC_QUERY_STATE ) ) -> GetCurSel () ) !=  CB_ERR )
		( ( CComboBox* ) GetDlgItem ( IDC_QUERY_STATE ) ) -> GetLBText (
			( ( CComboBox* ) GetDlgItem ( IDC_QUERY_STATE ) ) -> GetCurSel (),
			strStateQuery
		);
	if ( ( ( ( CComboBox* ) GetDlgItem ( IDC_QUERY_INPUT ) ) -> GetCurSel () ) !=  CB_ERR )
		( ( CComboBox* ) GetDlgItem ( IDC_QUERY_INPUT ) ) -> GetLBText (
			( ( CComboBox* ) GetDlgItem ( IDC_QUERY_INPUT ) ) -> GetCurSel (),
			strInputQuery
		);


	// the next 5 "if" statements determine the status of the 5 filter parameters (first parameter,
	// empty or not, etc), and build an SQL filter string that will be used in the recordset's "filter"
	// parameter upon loading the recordset from the database.  The filter string built here will only
	// consist of the WHERE clause of a valid SQL statement as per the requirements of the DAO recorset
	// filter query scheme (for more info on this, see: DAO, CDaoRecordset).  The resulting string will
	// look something like the following--based on user input:
	//
	//	"[Callsign] Like 'W1kzy' AND [Frequency] Like '147.*' AND [State] Like 'Massachusetts'"


	if ( ! strCallsignQuery . IsEmpty () ) {
		if ( bFirstSQLOperand ) {
			strFilter += CString ( "[Callsign] Like '" ) + strCallsignQuery + CString ( "'" );
			bFirstSQLOperand = FALSE;
		}
		else
			strFilter += CString ( " AND [Callsign] Like '" ) + strCallsignQuery + CString ( "'" );
	}
	
	if ( ! strFrequencyQuery . IsEmpty () ) {
		if ( bFirstSQLOperand ) {
			strFilter += CString ( "[Frequency] Like '" ) + strFrequencyQuery + CString ( "'" );
			bFirstSQLOperand = FALSE;
		}
		else
			strFilter += CString ( " AND [Frequency] Like '" ) + strFrequencyQuery + CString ( "'" );
	}
	
	if ( ! strCityQuery . IsEmpty () ) {
		if ( bFirstSQLOperand ) {
			strFilter += CString ( "[City] Like '" ) + strCityQuery + CString ( "'" );
			bFirstSQLOperand = FALSE;
		}
		else
			strFilter += CString ( " AND [City] Like '" ) + strCityQuery + CString ( "'" );
	}
	
	if ( ! strStateQuery . IsEmpty () ) {
		if ( bFirstSQLOperand ) {
			strFilter += CString ( "[State] Like '" ) + strStateQuery + CString ( "'" );
			bFirstSQLOperand = FALSE;
		}
		else
			strFilter += CString ( " AND [State] Like '" ) + strStateQuery + CString ( "'" );
	}
	
	if ( ! strInputQuery . IsEmpty () ) {
		if ( bFirstSQLOperand ) {
			strFilter += CString ( "[Input] Like '" ) + strInputQuery + CString ( "'" );
			bFirstSQLOperand = FALSE;
		}
		else
			strFilter += CString ( " AND [Input] Like '" ) + strInputQuery + CString ( "'" );
	}
	
	LoadFilterList ( strFilter ); // prep the recordset, and load the database contents into the listbox
}

// reset the contents of the listbox, clearing any query parameters
void CRfiDlg::OnClearQuery() 
{

	SetDlgItemText ( IDC_QUERY_CALLSIGN, "" );
	SetDlgItemText ( IDC_QUERY_CITY, "" );
	SetDlgItemText ( IDC_QUERY_FREQUENCY, "" );
	
	( ( CComboBox* ) ( GetDlgItem ( IDC_QUERY_STATE ) ) ) -> SetCurSel ( -1 );
	( ( CComboBox* ) ( GetDlgItem ( IDC_QUERY_INPUT ) ) ) -> SetCurSel ( -1 );

	LoadFilterList ( CString ( "" ) );

	return;
}

// editing an existing database entry
void CRfiDlg::OnEditRepeater() 
{
	CEditRepeaterDlg* pdlgEditRepeater;
	CString strRecordIdQuery;
	int nItemIndex = -1;
	BOOL bFieldsHaveChanged = FALSE;

	// if there's an element selected in the listbox
	if ( ( m_lcRepeaterList . GetNextItem ( -1, LVNI_SELECTED ) ) != -1 ) {
		try {
			// open the database
			if ( ! m_setComplete . IsOpen () )
				m_setComplete . Open ();
			// cycle through the selected listbox elements (there will only be one for this listbox, it is a single
			// selection only listbox) but for the sake of like-access between all listboxes in this app, and future
			// ease of extension, it will be done with a while loop.
			while ( ( nItemIndex = m_lcRepeaterList . GetNextItem ( nItemIndex, LVNI_SELECTED ) ) != -1 ) {
				strRecordIdQuery =
					CString ( "[ID] = " ) +
					CString ( m_lcRepeaterList . GetItemText ( nItemIndex, 0 ) ); // put the ID into the query string
				if ( m_setComplete . FindFirst ( strRecordIdQuery ) ) { // looking for this ID in the database, ID is a unique 'autonumber'
					// get an "edit" dialog and init the members
					pdlgEditRepeater = new CEditRepeaterDlg ( m_lcRepeaterList . GetItemText ( nItemIndex, 0 ) );
					pdlgEditRepeater -> SetCallsign ( m_setComplete . m_strCallsign );
					pdlgEditRepeater -> SetCity ( m_setComplete . m_strCity );
					pdlgEditRepeater -> SetFrequency ( m_setComplete . m_strFrequency );
					pdlgEditRepeater -> SetState ( m_setComplete . m_strState );
					pdlgEditRepeater -> SetInput ( m_setComplete . m_strInput );
					if ( pdlgEditRepeater -> DoModal () == IDOK ) { // show the dialog
						m_setComplete . Edit (); // begin the record edit
						m_setComplete . m_strCallsign = pdlgEditRepeater -> GetCallsign (); // change the recordset fields
						m_setComplete . m_strCity = pdlgEditRepeater -> GetCity ();
						m_setComplete . m_strFrequency = pdlgEditRepeater -> GetFrequency ();
						m_setComplete . m_strState = pdlgEditRepeater -> GetState ();
						m_setComplete . m_strInput = pdlgEditRepeater -> GetInput ();
						m_setComplete . Update (); // complete the edit by doing an update
						bFieldsHaveChanged = TRUE; // make a note that we have changed the database
					}
					delete pdlgEditRepeater; // get rid of the dialog
				}
				else {
					// if we EVER end up here, either the database is in the crapper, or I will have screwed up horribly--been known to happen from time to time :) ...
					// so let's cover our ass-ets just in case.
					AfxMessageBox ( "Internal failure\n\nCannot find selected repeater in database\nor database is corrupted", MB_ICONSTOP );
				}
			}
			m_setComplete . Close (); // close the database
		}
		catch ( CDaoException* e ) { // yeah, yeah, same comments as all the other "catch" blocks.
			char szBuffer [ 256 ];
			CString strExceptDesc =
				CString ( "JET Database Engine Error:\n\n Error Code: " ) +
				CString ( ltoa ( e -> m_pErrorInfo -> m_lErrorCode, szBuffer, 10 ) ) +
				CString ( "\nDescription: " ) +
				CString ( e -> m_pErrorInfo -> m_strDescription );
			AfxMessageBox ( strExceptDesc, MB_ICONEXCLAMATION );
			m_setComplete . Close ();
			return;
		}
		if ( bFieldsHaveChanged ) {
			LoadList (); // reload if necessary
		}
	}
	else { // there aren't any items selected in the list control
		AfxMessageBox ( "No repeater selected", MB_ICONEXCLAMATION );
	}
	
	return;
}

// this does some necessary setup with the tray icon object.
int CRfiDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// assign a menu, icon, tooltip, and handler to the tray icon.
	if ( ! m_classTrayNotifyIcon . Create ( this, IDR_POPUP, "RFI - Repeater Frequency Index", AfxGetApp () -> LoadIcon ( IDI_TRAYICON ), WM_TRAYNOTIFY ) ) {
		AfxMessageBox ( "Failure Creating Tray Icon", MB_ICONSTOP );
		return -1;
	}
	
	return 0;
}

// pass the tray notification message to the tray icon object for handling.
LRESULT CRfiDlg::OnTrayNotify ( WPARAM wParam, LPARAM lParam ) {
  return m_classTrayNotifyIcon . OnTrayNotification ( wParam, lParam );
}

// show the main window.  the tray icon object shows the popup menu 'IDR_POPUP' which
// has a command for calling this handler
void CRfiDlg::OnShowmain() 
{
	ShowWindow ( SW_SHOW );
}

// show the main window.  the tray icon object shows the popup menu 'IDR_POPUP' which
// has a command for calling this handler
void CRfiDlg::OnHidemain() 
{
	ShowWindow ( SW_HIDE );
}

// if stuff like "check marks on the tray icon popup menu" were implemented, the code
// would be here.
void CRfiDlg::OnUpdateShowmain(CCmdUI* pCmdUI) 
{
}

// if stuff like "check marks on the tray icon popup menu" were implemented, the code
// would be here.
void CRfiDlg::OnUpdateHidemain(CCmdUI* pCmdUI) 
{
}

⌨️ 快捷键说明

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