MFC对话框用循环创建多个复选框

Hero_bule 2016-01-04 02:50:04
int x=10,y=10;
int row,col;

CRect rect(x,y,50,50);
for(row=0;row<3;row++)
{
for(col=0;col<3;col++)
{
CButton* MyChk=new CButton();
x=x+10*col;
MyChk->Create("(4,1)",WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_AUTOCHECKBOX,rect,this,(col+1)*(row+1));
}
y=y+10*row;
}



这样为什么只出来一个复选框?
...全文
618 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
schlafenhamster 2016-01-07
  • 打赏
  • 举报
回复
最后给你一个 通过 edit 来 uncheck 的 代码, 供窗口。


void CMdiInDlgDlg::OnUpdateEdit1() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.
	
	// TODO: Add your control notification handler code here
	CString oldStr=m_Edit;
	UpdateData(TRUE);
	if(m_Edit==oldStr)
		return;
//	afxDump << m_Edit << "\n";
	int col,row;
	CString str;
	CButton *pUnchk=0;
	oldStr="";
	for(row=0;row<m_row;row++)
	{
		for(col=0;col<m_col;col++)
		{
			CButton *p=(CButton*)GetDlgItem((2000+row*m_col+col));
			if(p->GetCheck()==1)
			{
				GetDlgItem((2000+row*m_col+col))->GetWindowText(str);
				if(m_Edit.Find(str)>=0)
				{
					oldStr += str;
				}
				else
				{
					pUnchk = p;
				}
			}
		}
	}
	if(pUnchk) pUnchk->SetCheck(0);
	m_Edit=oldStr;
	UpdateData(FALSE);
}
只要 edit “(x,y)” 里有一个 字符被 删除 button 就 uncheck 。
Hero_bule 2016-01-07
  • 打赏
  • 举报
回复
嗯嗯 好的 感谢为我解答了这么多问题 就此结贴啦 给你加分
schlafenhamster 2016-01-06
  • 打赏
  • 举报
回复
// void CxxxDlg::OnCheckButton(UINT nID) {// key pressed // TODO: Add your command handler code here CButton *pBt=(CButton *)GetDlgItem(nID); if(pBt->GetCheck()==1) // or if(m_pMyChk[nID-2000].GetCheck()==1) { CString txt; GetDlgItemText(nID,txt); // afxDump << txt << "\n"; m_Edit=txt; } else { m_Edit=""; } UpdateData(FALSE); }
zhouxiaofeng1021 2016-01-06
  • 打赏
  • 举报
回复
CButton* MyChk=new CButton(); 局部对象~~~ 你创建后 在Onpaint函数中 setwindowspos 弄下button的位置 呗
schlafenhamster 2016-01-06
  • 打赏
  • 举报
回复
// void CxxxDlg::OnCheckButton(UINT nID) {// key pressed // TODO: Add your command handler code here CString txt; GetDlgItemText(nID,txt); //afxDump << txt << "\n"; m_Edit=txt; UpdateData(FALSE); }
schlafenhamster 2016-01-06
  • 打赏
  • 举报
回复
2000, 2100, 可以创建 100 个
schlafenhamster 2016-01-06
  • 打赏
  • 举报
回复
你要 看 resource。h 里 是不是 有 重复的 ID ,不是 随意 给的 !如我的是 2000 开始 //}}AFX_MSG afx_msg void OnCheckButton(UINT nID); DECLARE_MESSAGE_MAP() 、、、、、、、、、、、、、、、 //}}AFX_MSG_MAP ON_CONTROL_RANGE(BN_CLICKED, 2000, 2100,OnCheckButton) 、、、、、、、、、、、、、、、、、、、、、、 void CxxxDlg::OnCheckButton(UINT nID) {// key pressed // TODO: Add your command handler code here afxDump << nID << "\n"; }
schlafenhamster 2016-01-06
  • 打赏
  • 举报
回复
依我看 这个功能多余, 即不易 实现, 用户体验 也不好 (本来点一下 checkbox 就可以了, 现在要 删除 好几个 字符)
schlafenhamster 2016-01-06
  • 打赏
  • 举报
回复
edit 里内容可能是: “(1,1)(2,2)(3,3)” 被 修改后,要 看 哪个 被 删除 了, 问题是 用户 修改 是 随意的,分析 修改后 正确的 “(x,y)”
Hero_bule 2016-01-06
  • 打赏
  • 举报
回复
"Edit Update 时 检查 edit的内容" 运行之后再编辑框里改变内容时会反映到 void CMy111Dlg::OnChangeEdit1() 在这里怎么反过去让复选框状态改变. edit的内容就是m_Edit啊,怎么检查?判断不相等还是什么的
schlafenhamster 2016-01-06
  • 打赏
  • 举报
回复
"如果勾选多个也要显示多个" 在 一个 edit 里 显示 所有 check 的 ?

void CMdiInDlgDlg::OnCheckButton(UINT nID) 
{// key pressed
	// TODO: Add your command handler code here
	int col,row;
	CString str;
	m_Edit="";
	for(row=0;row<m_row;row++)
	{
		for(col=0;col<m_col;col++)
		{
			CButton *p=(CButton*)GetDlgItem((2000+row*m_col+col));
			if(p->GetCheck()==1)
			{
				GetDlgItem((2000+row*m_col+col))->GetWindowText(str);
				m_Edit += str;
			}
		}
	}
	UpdateData(FALSE);
}
“还有怎么删除编辑框中的坐标来是选中的复选框变为不选中” Edit Update 时 检查 edit的内容,
Hero_bule 2016-01-06
  • 打赏
  • 举报
回复
这个只能显示勾选一个的情况啊,如果勾选多个也要显示多个, 还有怎么删除编辑框中的坐标来是选中的复选框变为不选中。
Hero_bule 2016-01-05
  • 打赏
  • 举报
回复
东凑西凑被我凑出来了 void CMy111Dlg::OnUpdateEdit1() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function to send the EM_SETEVENTMASK message to the control // with the ENM_UPDATE flag ORed into the lParam mask. // TODO: Add your control notification handler code here UpdateData(true); int col,row; CString str; m_edit=""; for(row=0;row<m_row;row++) { for(col=0;col<m_col;col++) { CButton *p=(CButton*)GetDlgItem((1000+row*m_col+col)); int x=p->GetCheck(); GetDlgItem((1000+row*m_col+col))->GetWindowText(str); if(x==1) m_edit+=str; } } UpdateData(false); } 用这个可以显示坐标,我将Create函数中的ID改成1000+row*m_col+col了。 但是勾选了不是马上显示在编辑框,要在编辑框里按下空格什么的才刷新。这怎么弄? 还有为什么在编辑框里按回车程序就退出l
schlafenhamster 2016-01-05
  • 打赏
  • 举报
回复
"增加行增加列" 表示 个数 是 动态的 , 这时 要 用 new 了。。h protected: HICON m_hIcon; CButton *m_pMyChk; int m_row; int m_col; 、、 第一次 创建时

BOOL CMdiInDlgDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.
	int x,y;
	int row,col;
	m_row=3;
	m_col=3;
	m_pMyChk=new CButton[m_row*3+m_col];
	for(row=0;row<3;row++)
	{
		y = 30+ 40*row;
		for(col=0;col<3;col++)
		{
			x = 20+150*col;
			CRect rect;// not (x,y,50,50); !!!!
			rect.left=x;
			rect.top=y;
			rect.right=rect.left+100;
			rect.bottom=rect.top+50;
//			afxDump << rect << "\n";
			m_pMyChk[row*3+col].Create("(4.1)",WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_AUTOCHECKBOX,rect,this,(col+100)*(row+100));
		}
	}
以后 再创建 要 m_pMyChk[jj].DestryWindow(); delete [] m_pMyChk 然后 再 create
Hero_bule 2016-01-05
  • 打赏
  • 举报
回复
还有怎么把勾选的复选框后面的坐标显示在编辑框中
Hero_bule 2016-01-05
  • 打赏
  • 举报
回复
万分感谢
schlafenhamster 2016-01-05
  • 打赏
  • 举报
回复
CString caption; caption.Format("(%d,%d)",row+1,col+1); m_pMyChk[row*m_col+col].Create(caption,WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_AUTOCHECKBOX,rect,this,(col+100)*(row+100));
Hero_bule 2016-01-05
  • 打赏
  • 举报
回复
还有一个问题是 m_pMyChk[row*m_row+col].Create("(4.1)",WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_AUTOCHECKBOX,rect,this,(col+100)*(row+100)); 中的名字“(4,1)”,怎么让他按位置改变创建出来的名字分别是坐标。 比方说3乘3的那么名字就是: (1,1)(1,2)(1,3) (2,1,)(2,2)(2,3) (3,1)(3,2)(3,3) 另外能将勾选的复选框在另一个编辑框中实时显示
Hero_bule 2016-01-05
  • 打赏
  • 举报
回复
19楼的问题我自己解决了。只要将Create函数中的 m_pMyChk[row*m_row+col].Create 改成 m_pMyChk[row*m_col+col].Create 就可以了。 现在增加行函数: void CMy111Dlg::OnMenuitem32771() { // TODO: Add your control notification handler code here DeleteChkBox(); m_row++; CreateChkBox(); } 另外3个分别是m_col++;m_row--;m_col--;
Hero_bule 2016-01-05
  • 打赏
  • 举报
回复
void CxxxDlg::OnAdd() { // TODO: Add your control notification handler code here DeleteChkBox(); // m_row=4; m_col=4; CreateChkBox(); } 你的意思是全删除后重新创建是吧? 这个代码是删除后重新创建一个4乘4的吧。 我要的是增加行是一个函数,增加列是一个函数,删除行是一个函数,删除列也是一个函数。 如果将这个代码改成增加行的话,将 m_row=4; m_col=4; 改成 m_row++; 为什么不行,或者改成 m_row=4; m_col=3; 也不行。运行之后点菜单的增加行就出错退出程序。
加载更多回复(17)

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧