📄 editidlg.cpp
字号:
file.Open(m_fNameOut,CFile::modeCreate|CFile::modeWrite);
int i,j;
CString str;
for(i=1;i<=m_editNum;i++)
{
str="";
for(j=0;j<nColumn;j++)
str=str+m_gridCtrl.GetTextMatrix(i,j)+", ";
str=str.Mid(0,str.GetLength()-2);
str=str+"\r\n";
file.Seek(0,CFile::end);
file.WriteString(str);
}
file.Close();
}
void CEditiDlg::ParseLine(CString strIn, double outData[7], CString outStr[7], int iChar)
{
int ips, num=-1, iResid;
CString str,ss;
str=strIn;
str.TrimLeft(); str.TrimRight();
ips=str.Find(char(iChar));
while(ips!=-1)
{
num=num+1;
ss=str.Mid(0,ips);
outData[num]=atof(ss.GetBuffer(0));
ss.TrimLeft(); ss.TrimRight();
outStr[num]=ss;
iResid=str.GetLength()-ips-1;
if(iResid>0)
{
str=str.Mid(ips+1,iResid);
str.TrimLeft();
ips=str.Find(char(iChar));
}
else
{str="";ips=-1;}
}
if(str.GetLength()>0)
{
num=num+1;
ss=str.Mid(0,str.GetLength());
outData[num]=atof(ss.GetBuffer(0));
ss.TrimLeft(); ss.TrimRight();
outStr[num]=ss;
}
}
void CEditiDlg::refreshFlexGrid(int m,int n)
{
int m0,n0;
m0=m_gridCtrl.GetRows();
n0=m_gridCtrl.GetCols();
m_gridCtrl.SetRows(m);
m_gridCtrl.SetCols(n);
if(m>m0)
{
int i,j; char buf[20];
for(i=m0;i<m;i++)
{
for(j=0;j<n;j++)
{
m_gridCtrl.SetRow(long(i)); m_gridCtrl.SetCol(long(j));
m_gridCtrl.SetCellAlignment(4);
}
}
m_gridCtrl.SetRow(0L); m_gridCtrl.SetCol(0L);
m_gridCtrl.SetCellAlignment(4);
for(i=m0;i<m;i++)
{
sprintf(buf, "%d", i);
m_gridCtrl.SetTextMatrix(i, 0, buf);
m_gridCtrl.SetRowHeight(i,260);
}
}
m_gridCtrl.Refresh();
}
void CEditiDlg::updateFlexGrid(int m,int n)
{
m_gridCtrl.Clear();
// int m=m_editNum+1; int n = 8;
char buf[20];
m_gridCtrl.SetRows(m);
m_gridCtrl.SetCols(n);
m_gridCtrl.SetRowHeight(0,600);
m_gridCtrl.SetColWidth(0,400);
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
m_gridCtrl.SetRow(long(i)); m_gridCtrl.SetCol(long(j));
m_gridCtrl.SetCellAlignment(4);
if(j==n-1) m_gridCtrl.SetCellForeColor(m_gridCtrl.GetBackColorSel());
}
}
m_gridCtrl.SetRow(0L); m_gridCtrl.SetCol(0L);
m_gridCtrl.SetCellAlignment(4);
for(i=0;i<m;i++)
{
sprintf(buf, "%d", i);
m_gridCtrl.SetTextMatrix(i, 0, buf);
if(i!=0) m_gridCtrl.SetRowHeight(i,260);
for(j=1;j<n;j++)
{
sprintf(buf, "[%d,%d]", i, j );
m_gridCtrl.SetTextMatrix( i, j, buf);
}
}
CString str;
str="No.";
m_gridCtrl.SetTextMatrix(0,0,str);
m_gridCtrl.SetWordWrap(true);
str="轴材料\n重度\n(kg/m)";
m_gridCtrl.SetTextMatrix(0,1,str);
m_gridCtrl.SetColWidth(1,900);
str="各段\n长度\n(m)";
m_gridCtrl.SetTextMatrix(0,2,str);
m_gridCtrl.SetColWidth(2,900);
str="各段\n外径\n(m)";
m_gridCtrl.SetTextMatrix(0,3,str);
m_gridCtrl.SetColWidth(3,900);
str="各段\n内径\n(m)";
m_gridCtrl.SetTextMatrix(0,4,str);
m_gridCtrl.SetColWidth(4,900);
str="集中\n载荷\n(N)";
m_gridCtrl.SetTextMatrix(0,5,str);
m_gridCtrl.SetColWidth(5,900);
str="集中载荷\n距支点距离\n(m)";
m_gridCtrl.SetTextMatrix(0,6,str);
m_gridCtrl.SetColWidth(6,1000);
str="支反力\n计算结果\n(N)";
m_gridCtrl.SetTextMatrix(0,7,str);
m_gridCtrl.SetColWidth(7,1200);
}
void CEditiDlg::OnBtn2Matlab()
{
// TODO: Add your control notification handler code here
if(m_edit->GetSafeHwnd()!=NULL) m_edit->DestroyWindow(); //销毁编辑框窗口
CString strRow[7], strDest;
int i,j; CString str0,str;
for(j=1;j<7;j++)
{
str0="[";
for(i=1;i<=m_editNum;i++)
{
str=m_gridCtrl.GetTextMatrix(i,j);
str0=str0+str+" ";
}
str0.TrimRight();
str0=str0+"]";
strRow[j]=str0;
}
CString str3,str4;
str3=m_gridCtrl.GetTextMatrix(1,1);
str4.Format("%d",m_editNum);
strDest="zhifanli_jisuan("+strRow[2]+","+strRow[3]+","+strRow[5]+","+strRow[6]+","+str3+","+str4+")";
MessageBox(strDest);
double zfl_iRow;
for(i=1;i<=m_editNum;i++)
{
zfl_iRow=0.0;
for(j=1;j<7;j++)
{
str3=m_gridCtrl.GetTextMatrix(i,j);
zfl_iRow=zfl_iRow+atof(str3.GetBuffer(0));
}
str4.Format("%10.2f",zfl_iRow);
m_gridCtrl.SetTextMatrix(i,7,str4);
}
}
BEGIN_EVENTSINK_MAP(CEditiDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CEditiDlg)
ON_EVENT(CEditiDlg, IDC_MSFLEXGRID1, 71 /* EnterCell */, OnEnterCellMsflexgrid1, VTS_NONE)
ON_EVENT(CEditiDlg, IDC_MSFLEXGRID1, -601 /* DblClick */, OnDblClickMsflexgrid1, VTS_NONE)
ON_EVENT(CEditiDlg, IDC_MSFLEXGRID1, 73 /* Scroll */, OnScrollMsflexgrid1, VTS_NONE)
ON_EVENT(CEditiDlg, IDC_MSFLEXGRID1, -600 /* Click */, OnClickMsflexgrid1, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CEditiDlg::OnEnterCellMsflexgrid1()
{
// TODO: Add your control notification handler code here
if(bMultiSel) return; //如果MultiSel状态则返回
if(m_edit->GetSafeHwnd()!=0)
m_edit->DestroyWindow();
int col = m_gridCtrl.GetCol();
int row = m_gridCtrl.GetRow();
if(row==0 || col==0) return; //如果进入标题栏则返回
iColWidth0=m_gridCtrl.GetColWidth(long(col));
iRowHeight0=m_gridCtrl.GetRowHeight(long(row));
m_selectedRow = row;
m_selectedCol = col;
CreateEditBox();
//保存网格中原有的值
m_oldGridValue = m_gridCtrl.GetTextMatrix(row,col);
m_oldSelectedCol = m_selectedCol;
m_oldSelectedRow = m_selectedRow;
}
LONG CEditiDlg::OnGridDataChanged(UINT wParam, LONG lParam)
{
if(CEditiDlg::m_Ikey==27)
{
m_edit->SetWindowText(m_oldGridValue);
m_edit->DestroyWindow();
} //ESC
else if(CEditiDlg::m_Ikey==13)
{
CString str;
m_edit->GetWindowText(str);
m_gridCtrl.SetTextMatrix(m_oldSelectedRow,m_oldSelectedCol,str);
//Below for the Title
if((m_oldSelectedRow==0)||(m_oldSelectedCol==0))
{m_edit->DestroyWindow();}
else{
if(m_oldSelectedRow==m_gridCtrl.GetRows()-1)
{
if(m_oldSelectedCol==m_gridCtrl.GetCols()-1)
{
m_edit->DestroyWindow();
}
else
{
m_gridCtrl.SetCol(m_oldSelectedCol+1); m_gridCtrl.SetRow(1L);
}
}
else
{
m_gridCtrl.SetRow(m_oldSelectedRow+1);
}
}
} //CEditiDlg::m_Ikey==13, Enter
else
{
CString str;
m_edit->GetWindowText(str);
m_gridCtrl.SetTextMatrix(m_oldSelectedRow,m_oldSelectedCol,str);
}
return 1L;
}
void CEditiDlg::CreateEditBox()
{
long row_value = m_gridCtrl.GetRowHeight((long)m_selectedRow);//行高
long col_value = m_gridCtrl.GetColWidth((long)m_selectedCol); //列宽
long col_pos = m_gridCtrl.GetColPos(m_selectedCol); //列的起始位置
long row_pos = m_gridCtrl.GetRowPos(m_selectedRow); //行的起始位置
CClientDC dc(this);
int x_pixels = dc.GetDeviceCaps(LOGPIXELSX);//X方向没逻辑英寸的象素数
int y_pixels = dc.GetDeviceCaps(LOGPIXELSY);//y方向没逻辑英寸的象素数
//MM_TWIPS 一逻辑单位为1/1440英寸
CRect editrect;
editrect.left = (int)((col_pos/1440.0)*x_pixels);
editrect.top = (int)((row_pos/1440.0)*y_pixels);
editrect.right = (int)(((col_pos + col_value)/1440.0)*x_pixels);
editrect.bottom = (int)(((row_pos + row_value)/1440.0)*y_pixels);
if(m_edit->GetSafeHwnd()!= NULL)
{
m_edit->DestroyWindow();
m_gridCtrl.SetAllowUserResizing(1L);
}
delete m_edit;
m_edit = 0;
m_edit = new CMyEdit;
m_edit->Create(ES_AUTOHSCROLL|WS_VISIBLE|WS_BORDER|LVS_NOLABELWRAP ,
editrect,m_gridCtrl.FromHandle(m_gridCtrl.GetSafeHwnd()),MY_EDIT_CTRL);
m_edit->SetFont(GetFont());
m_edit->SetFocus();
m_gridCtrl.SetAllowUserResizing(0L);
CString str = m_gridCtrl.GetTextMatrix(m_selectedRow,m_selectedCol);
m_edit->SetWindowText(str);
m_edit->SetSel(str.GetLength(),str.GetLength(),FALSE);
}
void CEditiDlg::OnDblClickMsflexgrid1()
{
// TODO: Add your control notification handler code here
if(m_edit->GetSafeHwnd()!=0) m_edit->DestroyWindow(); //销毁编辑框窗口
int col = m_gridCtrl.GetCol();
int row = m_gridCtrl.GetRow();
//to Adjust if the selection of Cell Range is effective
long iRow,iCol,iRowSel,iColSel,iRows,iCols;
iRow=m_gridCtrl.GetRow(); iCol=m_gridCtrl.GetCol();
iRows=m_gridCtrl.GetRows(); iCols=m_gridCtrl.GetCols();
iRowSel=m_gridCtrl.GetRowSel(); iColSel=m_gridCtrl.GetColSel();
if((iRowSel==iRows-1) && (iRow==1) && (iCol==iColSel))
{
row=0; col=iCol;
m_selectedRow = row;
m_selectedCol = col;
CreateEditBox();
//保存网格中原有的值
m_oldGridValue = m_gridCtrl.GetTextMatrix(row,col);
m_oldSelectedCol = m_selectedCol;
m_oldSelectedRow = m_selectedRow;
} //above to enter Fixed Columns to edit Title
//Below to change Title of Rows
else if((iColSel==iCols-1) && (iCol==1) && (iRow==iRowSel))
{
long row_value = m_gridCtrl.GetRowHeight(iRow);//行高
long col_value = m_gridCtrl.GetColWidth((long)0); //列宽
long col_pos = m_gridCtrl.GetColPos((long)0); //列的起始位置
long row_pos = m_gridCtrl.GetRowPos(iRow); //行的起始位置
CClientDC dc(this);
int x_pixels = dc.GetDeviceCaps(LOGPIXELSX);//X方向没逻辑英寸的象素数
int y_pixels = dc.GetDeviceCaps(LOGPIXELSY);//y方向没逻辑英寸的象素数
//MM_TWIPS 一逻辑单位为1/1440英寸
CRect editrect;
editrect.left = (int)((col_pos/1440.0)*x_pixels);
editrect.top = (int)((row_pos/1440.0)*y_pixels);
editrect.right = (int)(((col_pos + col_value)/1440.0)*x_pixels);
editrect.bottom = (int)(((row_pos + row_value)/1440.0)*y_pixels);
CPoint point;
point.x = editrect.left + (editrect.right-editrect.left)/2;
// point.y = editrect.top + (editrect.bottom-editrect.top)/2;
point.y = editrect.bottom;
CRect oRect(0,0,0,0);
CWnd* pWnd = this->GetDlgItem(IDC_MSFLEXGRID1);
ASSERT(pWnd);
pWnd->GetWindowRect(&oRect);
point.x =point.x +oRect.left;
point.y =point.y +oRect.top;
CMenu* pPopMenu = new CMenu;
pPopMenu->LoadMenu(IDR_mnuRowInsDel);
CMenu* pFileMenu = pPopMenu->GetSubMenu(0);
if(iRows==2)
{
int x=pFileMenu->GetMenuItemCount();
UINT pID=pFileMenu->GetMenuItemID(x-1);
int RetVal=pFileMenu->EnableMenuItem(pID,MF_GRAYED);
}
pFileMenu->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON, point.x, point.y, this);
delete pPopMenu;
} //above to enter Fixed Rows to insert/delete one Row
else
{
return;
}
}
void CEditiDlg::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CRect rc;
GetWindowRect(&rc);
point.x = rc.left + point.x;
point.y = rc.top + point.y;
CMenu* pPopMenu = new CMenu;
pPopMenu->LoadMenu(IDR_mnuCopyPaste);
CMenu* pFileMenu = pPopMenu->GetSubMenu(0);
pFileMenu->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON, point.x,point.y, this);
delete pPopMenu;
CDialog::OnRButtonDown(nFlags, point);
}
void CEditiDlg::OnmnuCOPY()
{
// TODO: Add your command handler code here
int n; CString str0,str2;
str2="";
str2=str2+char(13)+char(10)+":";
//获得数据
CString strData;
strData=m_gridCtrl.GetClip();
n = strData.Replace(str2.Mid(0,1), str2.Mid(0,2));
//打开系统剪贴板
if (OpenClipboard())
{
//使用之前,清空系统剪贴板
EmptyClipboard();
//分配一内存,大小等于要拷贝的字符串的大小,返回一的内存控制句柄
HGLOBAL hClipboardData;
hClipboardData = GlobalAlloc(GMEM_DDESHARE, strData.GetLength()+1);
//内存控制句柄加锁,返回值为指向那内存控制句柄所在的特定数据格式的指针
char * pchData;
pchData = (char*)GlobalLock(hClipboardData);
//将本地变量的值赋给全局内存
strcpy(pchData, LPCSTR(strData));
//给加锁的全局内存控制句柄解锁
GlobalUnlock(hClipboardData);
//通过全局内存句柄将要拷贝的数据放到剪贴板上
SetClipboardData(CF_TEXT,hClipboardData);
CloseClipboard(); //使用完后关闭剪贴板
}
}
void CEditiDlg::OnmnuPASTE()
{
// TODO: Add your command handler code here
//打开系统剪贴板
if (OpenClipboard())
{
//判断剪贴板上的数据是否是指定的数据格式
if (::IsClipboardFormatAvailable(CF_TEXT)
|| ::IsClipboardFormatAvailable(CF_OEMTEXT))
{
// Retrieve the Clipboard data (specifying that
// we want ANSI text (via the CF_TEXT value).
//从剪贴板上获得数据
HANDLE hClipboardData = GetClipboardData(CF_TEXT);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -