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

📄 demo2005dlg.cpp

📁 数据库管理软件,具有很多功能,应用DBS数据库
💻 CPP
📖 第 1 页 / 共 3 页
字号:

	// 没选中
	if ( info.iItem < 0) 
	{
		// hide m_edModifiedValue
		m_edModifiedValue.ShowWindow(SW_HIDE);

		return;
	}

	// 确定选中的item, subItem
	nCurRcdItem = info.iItem;
	nCurRcdSubItem = info.iSubItem;

	// 移动m_edModifiedValue到选中的item上
	m_recordList.GetSubItemRect(nCurRcdItem,nCurRcdSubItem,LVIR_LABEL,rect);
	m_edModifiedValue.MoveWindow(&rect);

	// Show m_edModifiedValue
	m_edModifiedValue.ShowWindow(SW_SHOW);

	// Put the text in the selected item to m_edModifiedValue
	m_edModifiedValue.SetWindowText(
					m_recordList.GetItemText(nCurRcdItem,nCurRcdSubItem) );
	m_edModifiedValue.SetFocus();
	m_edModifiedValue.SetSel(0, -1, TRUE);


	*pResult = 0;
}

// finish update
void CDemo2005Dlg::OnOK() 
{
	CString sztmp;

	UpdateData(TRUE);

	
	if( GetFocus( )==(CWnd*)&m_edFieldValue ) // input a new fieldValue
	{
		OnNextValue();
	}
	else if( GetFocus( )==(CWnd*)&m_edModifiedValue ) // modify a fieldValue
	{
		m_edModifiedValue.GetWindowText(sztmp);

		//make sure the value you input is correct at lest in form
		if( !CheckValue( sztmp, pFieldDsc[nCurRcdSubItem].fType,
						 pFieldDsc[nCurRcdSubItem].fLen) )
			return;

		m_recordList.SetItemText( nCurRcdItem, nCurRcdSubItem, (LPCTSTR)sztmp);

		m_edModifiedValue.ShowWindow(SW_HIDE);	
	}

	return;
}


// save all records
void CDemo2005Dlg::OnRecordSave() 
{
	byte *buf, *p;
	int row, col;
	CString sztmp;

	UpdateData(TRUE);


	// seek file pointer 
	if( !m_fhd )
	{
		AfxMessageBox("There isn't any file opened!");
		return;
	}

	fseek( m_fhd, sizeof(m_fileHeader)+fcnt*sizeof(fieldDesc), SEEK_SET );


	// if the last fieldValue has been input, increment rcnt
	if( vIndex==m_fileHeader.fcnt )
	{
		rcnt++;
		vIndex = 0;
	}


	// scan m_recordList, save it to file
	buf = (byte*)malloc( m_fileHeader.rlen );

	for( row=0; row<rcnt; row++ )
	{
		memset(buf,0,m_fileHeader.rlen);
		p = buf;

		for( col=0; col<fcnt; col++)
		{
			sztmp = m_recordList.GetItemText(row, col);
			switch( pFieldDsc[col].fType )
			{
			case _INTEGER:
				*(int*)p = atoi( (LPCTSTR)sztmp );
				break;
			case _DOUBLE:
				*(double*)p = atof( (LPCTSTR)sztmp );
				break;
			case _DATE:
			case _TIME:
			case _STRING:
				strncpy( (char *)p, (LPCTSTR)sztmp, pFieldDsc[col].fLen );
			case _ERRTYPE:
			default:
				break;
			}

			// next field value
			p += pFieldDsc[col].fLen;
		}

		// write a record to file
		fwrite( (void*)buf, m_fileHeader.rlen, 1, m_fhd );
	}


	// write file_header
	m_fileHeader.rcnt = rcnt;
	fseek( m_fhd, 0, SEEK_SET );
	fwrite( (void*)&m_fileHeader,sizeof(m_fileHeader),1,m_fhd);


	// 对记录的操作已保存
	SwitchRcdOp(rop);
	rop = _NOTOPERATED;


	// free buf
	free(buf);
	buf = NULL;

}


// set column header for record_list
void CDemo2005Dlg::SetRecordlistHeader()
{
	if( !m_fhd || !pFieldDsc || m_fileHeader.fcnt<=0 )
	{
		AfxMessageBox("没有定义域", MB_OK|MB_ICONERROR);
		return ;
	}
	

	// column width
	CRect rect;
	m_recordList.GetWindowRect(&rect);
	int nWidth = (rect.right-rect.left-4)/m_fileHeader.fcnt;

	// insert column header
	LV_COLUMN lvcol;
	lvcol.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
	lvcol.fmt = LVCFMT_LEFT;
	for(int i=0; i<m_fileHeader.fcnt; i++)
	{
		lvcol.cx = nWidth;
		lvcol.pszText = (char*)(LPCTSTR)pFieldDsc[i].fName; // column title
		lvcol.iSubItem = i;
		m_recordList.SetColumn(i,&lvcol);
		m_recordList.InsertColumn(i,&lvcol);
	}

}


// clear up m_recordList
void CDemo2005Dlg::ClearRecordlist()
{
	// clear up items
	m_recordList.DeleteAllItems();

	// Delete all of the columns
	int nColumnCount = m_recordList.GetHeaderCtrl()->GetItemCount();
	for (int i=0;i < nColumnCount;i++)
	{
	   m_recordList.DeleteColumn(0);
	}

}


// ready for next value
void CDemo2005Dlg::OnNextValue() 
{

	 UpdateData(TRUE);

	// validate fieldValue
	if( m_FieldValue.IsEmpty() )
	{
		AfxMessageBox("请输入域值", MB_OK|MB_ICONERROR);
		return ;
	}
	if( !CheckValue(m_FieldValue, pFieldDsc[vIndex].fType, pFieldDsc[vIndex].fLen) )
		return;

	// show the new fieldValue in m_recordList
	ShowItemText( m_recordList, rcnt, vIndex, m_FieldValue);

	// update vcnt, rcnt
	vIndex++;
	if( vIndex==m_fileHeader.fcnt )
	{
		rcnt++;
		vIndex = 0;
	}

	// ready for the next field
	ShowFieldCaption(vIndex);
	m_edFieldValue.SetWindowText("");
	m_edFieldValue.SetFocus();
}


// Show FieldCaption for editing fieldValue
void CDemo2005Dlg::ShowFieldCaption(int fIndex)
{
	CString sztmp;

	if( fIndex<0 || fIndex>m_fileHeader.fcnt )
		sztmp = _T("");
	else
	{
		sztmp = pFieldDsc[fIndex].fName;
		switch( pFieldDsc[fIndex].fType )
		{
		case _INTEGER:
			sztmp += "(Int)";
			break;
		case _DOUBLE:
			sztmp += "(Double)";
			break;
		case _DATE:
			sztmp += "(Date)[mm/dd/yyyy]";
			break;
		case _TIME:
			sztmp += "(Time)[hh:mm:ss]";
			break;
		case _STRING:
			sztmp += "(String)";
			break;
		case _ERRTYPE:
		default:
			sztmp = _T("");
			break;
		}
	}

	m_staFieldCaption.SetWindowText(sztmp);

}


// Reset variable for DBfile, mode, record
void CDemo2005Dlg::ResetVariable()
{
	// reset file head
	m_fileHeader.fcnt = -1;
	m_fileHeader.rcnt = -1;
	m_fileHeader.rlen = -1;

	memset((void*)m_fileName,0,256); // file name
	m_fhd = NULL; // file handler

	beModeEdit = FALSE;
	maxFcnt = 20; // maximum field count
	fcnt = -1;
	pFieldDsc = NULL;
	nCurModeItem = -1; // not selectd a item

	rcnt = -1;
	vIndex = -1;
	rop = _NOTOPERATED;
	nCurRcdItem = -1;
	nCurRcdSubItem = -1;

}


// Browse Records
void CDemo2005Dlg::OnBrowse() 
{
	int row, col;
	char *tmp;
	byte *buf, *p;
	// there must be a open file
	if(!m_fhd)
	{
		AfxMessageBox("There isn't any file opened!");
		return;
	}
	else if( !pFieldDsc || m_fileHeader.fcnt<=0 )
	{
		AfxMessageBox("没有定义域", MB_OK|MB_ICONERROR);
		return ;
	}

	// save records first?
	if( rop==_ADD || rop==_DELETE || rop==_UPDATE )
	{
		if( AfxMessageBox("The record hasn't been saved.\r\nDo you want to save it first?",
						  MB_YESNO | MB_ICONQUESTION) == IDYES )
			OnRecordSave();
		else
			rcnt = m_fileHeader.rcnt;
	}

	// record operation switch to _BROWSE
	SwitchRcdOp(rop);
	rop = _BROWSE;


	// clear up m_recordList first
	ClearRecordlist();


	// set column header for record_list
	SetRecordlistHeader();


	// read records from the file, and show them in m_recordList
	tmp = (char*)malloc( m_fileHeader.rlen );
	buf = (byte*)malloc( m_fileHeader.rlen * m_fileHeader.rcnt );

	// read records from file
	fseek( m_fhd, sizeof(m_fileHeader)+fcnt*sizeof(fieldDesc), SEEK_SET );
	fread( buf, m_fileHeader.rlen, m_fileHeader.rcnt, m_fhd );

	p = buf;
	for( row=0; row<m_fileHeader.rcnt; row++)
	{
		for( col=0; col<m_fileHeader.fcnt; col++)
		{
			// Show a fieldValue
			CString sztmp;
			switch( pFieldDsc[col].fType )
			{
			case _INTEGER:
				sztmp.Format("%d",*(int*)p);
				break;
			case _DOUBLE:
				sztmp.Format("%f",*(double*)p);
				break;
			case _DATE:
			case _TIME:
			case _STRING:
				memset( (void*)tmp, 0, m_fileHeader.rlen);
				strncpy( tmp, (const char *)p, pFieldDsc[col].fLen );
				sztmp = tmp;
				break;
			case _ERRTYPE:
			default:
				break;
			}
			ShowItemText( m_recordList, row, col, sztmp);

			// next field value
			p += pFieldDsc[col].fLen;
		}
	}

	free(buf);
	buf = NULL;
	
}


// Query
void CDemo2005Dlg::OnQuery() 
{
	// there must be a open file
	if(!m_fhd)
	{
		AfxMessageBox("There isn't any file opened!");
		return;
	}
	else if( !pFieldDsc || m_fileHeader.fcnt<=0 )
	{
		AfxMessageBox("没有定义域", MB_OK|MB_ICONERROR);
		return ;
	}
	else if( m_fileHeader.rcnt<=0 )
	{
		AfxMessageBox("没有记录", MB_OK|MB_ICONERROR);
		return ;
	}

	// save records first?
	if( rop==_ADD || rop==_DELETE || rop==_UPDATE )
	{
		if( AfxMessageBox("The record hasn't been saved.\r\nDo you want to save it first?",
						  MB_YESNO | MB_ICONQUESTION) == IDYES )
			OnRecordSave();
		else
		{
			rcnt = m_fileHeader.rcnt;
			OnBrowse();
		}
	}

	// record operation switch to _UPDATE
	SwitchRcdOp(rop);
	rop = _QUERY;


	// Show Query-Wizard
	OnWizard();

}

void CDemo2005Dlg::OnWizard()
{
	// TODO: The property sheet attached to your project
	// via this function is not hooked up to any message
	// handler.  In order to actually use the property sheet,
	// you will need to associate this function with a control
	// in your project such as a menu item or tool bar button.

	CQuerySheet propSheet;

	// initialize variable for propSheet
//	propSheet.fcnt = fcnt;
//	propSheet.rcnt = rcnt;

	propSheet.DoModal();

	// This is where you would retrieve information from the property
	// sheet if propSheet.DoModal() returned IDOK.  We aren't doing
	// anything for simplicity.
}


// Statistic
void CDemo2005Dlg::OnStatistic() 
{
	// there must be a open file
	if(!m_fhd)
	{
		AfxMessageBox("There isn't any file opened!");
		return;
	}
	else if( !pFieldDsc || m_fileHeader.fcnt<=0 )
	{
		AfxMessageBox("没有定义域", MB_OK|MB_ICONERROR);
		return ;
	}
	else if( m_fileHeader.rcnt<=0 )
	{
		AfxMessageBox("没有记录", MB_OK|MB_ICONERROR);
		return ;
	}

	// save records first?
	if( rop==_ADD || rop==_DELETE || rop==_UPDATE )
	{
		if( AfxMessageBox("The record hasn't been saved.\r\nDo you want to save it first?",
						  MB_YESNO | MB_ICONQUESTION) == IDYES )
			OnRecordSave();
		else
		{
			rcnt = m_fileHeader.rcnt;
			OnBrowse();
		}
	}

	// record operation switch to _UPDATE
	SwitchRcdOp(rop);
	rop = _STATISTIC;


	// Show Statistics-Wizard
	OnWizard1();
	
}

void CDemo2005Dlg::OnWizard1()
{
	// TODO: The property sheet attached to your project
	// via this function is not hooked up to any message
	// handler.  In order to actually use the property sheet,
	// you will need to associate this function with a control
	// in your project such as a menu item or tool bar button.

	CStatisticsSheet propSheet;

	propSheet.DoModal();

	// This is where you would retrieve information from the property
	// sheet if propSheet.DoModal() returned IDOK.  We aren't doing
	// anything for simplicity.
}

void CDemo2005Dlg::OnChangeFieldName() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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