MFC:在编辑框中表示邻接矩阵



我有一个基于对话框的程序(MFC(,我想在其中制作邻接图表示。我有一个编辑框 (IDC_EDIT( 和 MFC 屏蔽编辑控件 (IDC_VERTEXES(,它有一个 2 位数字的掩码。

我写了一个代码,在失去对IDC_VERTEXES的关注时,IDC_EDIT将被矩阵填充,其中所有数字都将为 0。

void CTAB1::OnEnKillfocusVertexes()
{
CString Text;
GetDlgItemText(IDC_VERTEXES, Text);
int x = _wtoi(Text);
if (!Text.IsEmpty()) {
SetDlgItemText(IDC_EDIT, L"");
}
CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT);
int nPos = 0;
for (int i = 0; i < x; i++)
{
for (int j = 0; j < x; j++)
{   
pEdit->SetSel(nPos, nPos);
pEdit->ReplaceSel(L"0");
pEdit->ReplaceSel(L" ");
}
pEdit->ReplaceSel(L"rn");
}
} 

它按照我想要的方式工作,除了一件事。假设IDC_VERTEXES是否为 3 (x = 3(。IDC_EDIT应该是这样的:

0 0 0
0 0 0
0 0 0

但它看起来像这样:

0 
0 0 0 
0 0 0 
0 0

我错过了什么还是把\r放在了错误的地方?

在每次迭代中,您都会将插入符号位置重置为编辑控件的开头。

相反,您可以在开始时设置一次插入符号位置。ReplaceSel只需插入插入符号处,然后将插入符号向前移动。例:

pEdit->SetSel(nPos, nPos);
for(int i = 0; i < x; i++)
{
for(int j = 0; j < x; j++)
{
pEdit->ReplaceSel(L"0");
pEdit->ReplaceSel(L" ");
}
pEdit->ReplaceSel(L"rn");
}

或者您可以将pEdit->SetSel(nPos, nPos);放在每行的开头(这将以相反的顺序插入行(

最新更新