📄 demo2005dlg.cpp
字号:
m_edFieldName.SetFocus();
// enable to response to OnClickModeList
beModeEdit = TRUE;
}
void CDemo2005Dlg::OnModeSave()
{
if(!m_fhd)
{
AfxMessageBox("There isn't any file opened!");
return;
}
// set file_header
strcpy(m_fileHeader.ver,VERSION);
m_fileHeader.fcnt = fcnt;
m_fileHeader.rlen = pFieldDsc[fcnt-1].offset + pFieldDsc[fcnt-1].fLen;
m_fileHeader.rcnt = rcnt = 0;
// write file_header
fwrite(&m_fileHeader,sizeof(m_fileHeader),1,m_fhd);
// write fields
fwrite(pFieldDsc,sizeof(fieldDesc),m_fileHeader.fcnt,m_fhd);
// Disable mode-menu
CMenu* mmenu = GetMenu();
CMenu* submenu = mmenu->GetSubMenu(1);
submenu->EnableMenuItem(IDM_MODE_EDIT, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
submenu->EnableMenuItem(IDM_MODE_SAVE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
// Disable buttons for editing mode
m_btnFieldAdd.EnableWindow(FALSE);
m_btnFieldDelete.EnableWindow(FALSE);
m_btnFieldModify.EnableWindow(FALSE);
beModeEdit = FALSE;
// set column header for record_list
SetRecordlistHeader();
}
// add a new field
void CDemo2005Dlg::OnFieldAdd()
{
UpdateData(TRUE);
// check field definition
if( CheckField(TRUE)==FALSE ) // inputed information error
return;
// Show the new field information in m_modeList
CString sztmp;
ShowItemText( m_modeList, fcnt, 0, m_FieldName);
ShowItemText( m_modeList, fcnt, 1, m_FieldType);
sztmp.Format("%ld",m_FieldLength);
ShowItemText( m_modeList, fcnt, 2, sztmp);
/*** save the new field information in fdsc[] ***/
// field count exceed maxFcnt
if( fcnt>=maxFcnt )
{
struct fieldDesc *ptmp = pFieldDsc;
pFieldDsc = (struct fieldDesc *)malloc( maxFcnt*2*sizeof(fieldDesc) );
memcpy( (void*)pFieldDsc, (void*)ptmp, maxFcnt*sizeof(fieldDesc) );
maxFcnt *= 2 ;
free(ptmp);
}
// save the new field information
strcpy( pFieldDsc[fcnt].fName, (char*)(LPCTSTR)(m_FieldName) );
pFieldDsc[fcnt].fType = ftype;
pFieldDsc[fcnt].fLen = m_FieldLength;
if( fcnt==0 )
pFieldDsc[fcnt].offset = 0;
else
pFieldDsc[fcnt].offset = pFieldDsc[fcnt-1].offset + pFieldDsc[fcnt-1].fLen;
// field count increment
fcnt++;
// Reset mode-edit information
ResetModeInfo();
}
// delete a field
void CDemo2005Dlg::OnFieldDelete()
{
POSITION pos;
int nItem;
// get all items selected
pos = m_modeList.GetFirstSelectedItemPosition();
if(!pos)
{
AfxMessageBox("No items were selected");
return;
}
if(AfxMessageBox("Are you sure to delete the field?",MB_YESNO | MB_ICONQUESTION)!=IDYES)
return;
nItem = m_modeList.GetNextSelectedItem(pos);
// field count decrement
fcnt--;
// delete the selected field from pFieldDsc
if( nItem<fcnt )
{
for(int i=nItem; i<fcnt; i++)
{
memcpy((void*)&pFieldDsc[i],(void*)&pFieldDsc[i+1],sizeof(struct fieldDesc));
}
}
// Show information of the left fields in m_modeList
m_modeList.DeleteAllItems();// clear up items
ShowAllFields();
// Reset mode-edit information
ResetModeInfo();
}
// modify an existing field
void CDemo2005Dlg::OnFieldModify()
{
UpdateData(TRUE);
// selected a field ?
POSITION pos = m_modeList.GetFirstSelectedItemPosition();
if( !pos )
{
AfxMessageBox("No items were selected");
return;
}
// check field definition
if( CheckField(FALSE)==FALSE ) // inputed information error
return;
// 先删除原行
m_modeList.DeleteItem(nCurModeItem);
// Show the new field information in m_modeList
CString sztmp;
ShowItemText( m_modeList, nCurModeItem, 0, m_FieldName);
ShowItemText( m_modeList, nCurModeItem, 1, m_FieldType);
sztmp.Format("%ld",m_FieldLength);
ShowItemText( m_modeList, nCurModeItem, 2, sztmp);
// save the modified field information
strcpy(pFieldDsc[nCurModeItem].fName,(char*)(LPCTSTR)(m_FieldName));
pFieldDsc[nCurModeItem].fType = ftype;
pFieldDsc[nCurModeItem].fLen = m_FieldLength;
if( nCurModeItem==0 )
pFieldDsc[nCurModeItem].offset = 0;
else
pFieldDsc[nCurModeItem].offset = pFieldDsc[nCurModeItem-1].offset
+ pFieldDsc[nCurModeItem-1].fLen;
// modify the followings' offset
for(int i=nCurModeItem+1; i<fcnt; i++)
{
pFieldDsc[i].offset = pFieldDsc[i-1].offset + pFieldDsc[i-1].fLen;;
}
// reset selectd index
// nCurModeItem = -1;
// Reset mode-edit information
ResetModeInfo();
}
// get field-length based on field-type
void CDemo2005Dlg::OnSelchangeFieldType()
{
CString sztmp;
UpdateData(TRUE);
// Get field-type
ftype = (fieldType)m_cmbFieldType.GetCurSel();
// Set field-length based on field-type
switch(ftype)
{
case _INTEGER:
sztmp.Format("%d",sizeof(int));
m_edFieldLength.SetWindowText(sztmp);
m_edFieldLength.EnableWindow(FALSE);
break;
case _DOUBLE:
sztmp.Format("%d",sizeof(double));
m_edFieldLength.SetWindowText(sztmp);
m_edFieldLength.EnableWindow(FALSE);
break;
case _DATE:
sztmp.Format("%d",10);
m_edFieldLength.SetWindowText(sztmp);
m_edFieldLength.EnableWindow(FALSE);
break;
case _TIME:
sztmp.Format("%d",8);
m_edFieldLength.SetWindowText(sztmp);
m_edFieldLength.EnableWindow(FALSE);
break;
case _STRING:
sztmp.Format("%d",0);
m_edFieldLength.SetWindowText(sztmp);
m_edFieldLength.EnableWindow(TRUE);
break;
case _ERRTYPE:
default:
m_FieldLength = 0;
break;
}
}
// Reset mode-edit information
void CDemo2005Dlg::ResetModeInfo()
{
m_edFieldName.SetWindowText("");
m_cmbFieldType.SetCurSel(-1);
m_cmbFieldType.SetWindowText("");
m_FieldType = _T("");
m_edFieldLength.SetWindowText("0");
}
// set column header for mode_list
void CDemo2005Dlg::SetModelistHeader()
{
// column header
LPTSTR lpszCols[]={" 域 名","域 类 型","域 长 度",NULL};
// column width
CRect rect;
m_modeList.GetWindowRect(&rect);
int nWidth = (rect.right-rect.left-4)/3;
// insert column header
LV_COLUMN lvcol;
lvcol.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvcol.fmt = LVCFMT_LEFT;
for(int i=0; lpszCols[i]!=NULL; i++)
{
lvcol.cx = nWidth;
lvcol.pszText = lpszCols[i];
lvcol.iSubItem = i;
m_modeList.SetColumn(i,&lvcol);
m_modeList.InsertColumn(i,&lvcol);
}
}
// Show the information of fields in m_modeList
void CDemo2005Dlg::ShowAllFields()
{
CString sztmp;
for(int row=0; row<(int)fcnt; row++)
{
ShowItemText( m_modeList, row, 0, pFieldDsc[row].fName);
ShowItemText( m_modeList, row, 1, szFieldTypes[(int)pFieldDsc[row].fType]);
sztmp.Format( "%d", pFieldDsc[row].fLen);
ShowItemText( m_modeList, row, 2, sztmp);
}
}
// clear up m_modeList
void CDemo2005Dlg::ClearModelist()
{
// clear up items
m_modeList.DeleteAllItems();
// Delete all of the columns
int nColumnCount = m_modeList.GetHeaderCtrl()->GetItemCount();
for (int i=0;i < nColumnCount;i++)
{
m_modeList.DeleteColumn(0);
}
}
// when click modeList,show the row in mode-edits
void CDemo2005Dlg::OnClickModeList(NMHDR* pNMHDR, LRESULT* pResult)
{
CString sztmp;
if( beModeEdit==FALSE )
return;
// 确定选中的item
NMITEMACTIVATE *lpnmitem = reinterpret_cast<NMITEMACTIVATE *>(pNMHDR);
LVHITTESTINFO info;
info.pt = lpnmitem->ptAction;
m_modeList.SubItemHitTest(&info);
// 没选中
if ( info.iItem < 0)
return;
// 记录当前选中的item
nCurModeItem = info.iItem;
// 在mode-edits中显示选中的item
m_edFieldName.SetWindowText(pFieldDsc[nCurModeItem].fName);
m_cmbFieldType.SetCurSel((int)pFieldDsc[nCurModeItem].fType);
OnSelchangeFieldType();
if( pFieldDsc[nCurModeItem].fType==_STRING )
{
sztmp.Format("%d",pFieldDsc[nCurModeItem].fLen);
m_edFieldLength.SetWindowText(sztmp);
}
*pResult = 0;
}
// check field definition
BOOL CDemo2005Dlg::CheckField(BOOL bNew)
{
// check field information
if( m_FieldName.IsEmpty() )
{
AfxMessageBox("请输入域名", MB_OK|MB_ICONERROR);
return FALSE;
}
if( m_FieldType.IsEmpty() )
{
AfxMessageBox("请选择域类型", MB_OK|MB_ICONERROR);
return FALSE;
}
if( m_FieldLength <= 0 )
{
AfxMessageBox("域长度应大于0", MB_OK|MB_ICONERROR);
return FALSE;
}
// is the fieldName redefined?
for(int i=0; i<fcnt; i++)
{
if( !bNew && i==nCurModeItem ) // 修改前后的域名可相同
continue;
if( !strcmp((LPCTSTR)m_FieldName, pFieldDsc[i].fName) )
{
AfxMessageBox("域名重定义!", MB_OK|MB_ICONERROR);
return FALSE;
}
}
return TRUE;
}
/*********************************************************
* functions for record *
*********************************************************/
// begin for adding records
void CDemo2005Dlg::OnRecordAdd()
{
// if click add-menu when adding records, do nothing
if(rop==_ADD)
return;
else if( rop==_UPDATE )
m_edModifiedValue.ShowWindow(SW_HIDE);
// recordOperation switch to _ADD
rop = _ADD;
// enable buttons for _ADD
m_edFieldValue.EnableWindow(TRUE);
m_btnNextValue.EnableWindow(TRUE);
// set focus for fieldValue
m_edFieldValue.SetFocus();
// show fieldName for the first field
vIndex = 0;
ShowFieldCaption(vIndex);
}
// delete a record
void CDemo2005Dlg::OnRecordDelete()
{
POSITION pos;
int *pdel;
int oldrcnt, i, nItem, nSubItem;
// recordOperation switch to _DELETE
SwitchRcdOp(rop);
rop = _DELETE;
// Gets the position of the first selected item in m_recordList
pos = m_recordList.GetFirstSelectedItemPosition();
if( pos==NULL )
{
AfxMessageBox("No records were selected!",MB_OK|MB_ICONWARNING);
return;
}
// make sure to delete those records
if(AfxMessageBox("Are you sure to delete those records?",
MB_YESNO | MB_ICONQUESTION) != IDYES )
return;
// multi-selection
oldrcnt = rcnt;
pdel = (int*)malloc( sizeof(int)*oldrcnt );
i = 0;
while(pos)
{
nItem = m_recordList.GetNextSelectedItem(pos);
if( nItem<oldrcnt )
{
pdel[i++] = nItem;
rcnt--;
}
else
{
AfxMessageBox("error selected!");
return;
}
}
pdel[i] = oldrcnt;
// delete selected items
for(nItem=pdel[0],i=1; nItem<rcnt; i++)
{
while( nItem<pdel[i]-i )
{
/* **work well expect remain seleted positions**
for( nSubItem=0; nSubItem<m_fileHeader.fcnt; nSubItem++ )
m_recordList.SetItemText(nItem, nSubItem,
(LPCTSTR)m_recordList.GetItemText( nItem+i, nSubItem) );
*/
m_recordList.DeleteItem(nItem);
ShowItemText( m_recordList, nItem, 0,
m_recordList.GetItemText( nItem+i-1, 0));
for( nSubItem=1; nSubItem<m_fileHeader.fcnt; nSubItem++ )
ShowItemText( m_recordList, nItem, nSubItem,
m_recordList.GetItemText( nItem+i, nSubItem));
nItem++;
}
}
// clear post items
for( nItem=oldrcnt-1; nItem>=rcnt; nItem--)
m_recordList.DeleteItem(nItem);
}
// begin to modify a fieldValue
void CDemo2005Dlg::OnRecordUpdate()
{
// recordOperation switch to _UPDATE
SwitchRcdOp(rop);
rop = _UPDATE;
}
// when DoubleClick recordList,show the fieldValue in modifiedValue-edit
void CDemo2005Dlg::OnDblclkRecordList(NMHDR* pNMHDR, LRESULT* pResult)
{
CRect rect, rect0;
CString sztmp;
if( rop!=_UPDATE )
return;
UpdateData(TRUE);
// 确定选中的item
NMITEMACTIVATE *lpnmitem = reinterpret_cast<NMITEMACTIVATE *>(pNMHDR);
LVHITTESTINFO info;
info.pt = lpnmitem->ptAction;
m_recordList.SubItemHitTest(&info);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -