// IDC_LISTBOX ListBox的ID号
// 有一个ListBox,里面有n行,当选择第一行时自动弹出一个MessageBox,选中第二行时自动弹出一个
// MessageBox……以此类推
//
// 但是为什么下面代码无法实现呢?//SampleDlg.h
class CSampleDlg : public CDialog
{
afx_msg void OnSelNumber();
}//SampleDlg.cppBEGIN_MESSAGE_MAP(CSampleDlg, CDialog)
ON_BN_CLICKED(IDC_CHECKLISTBOX, OnSelNumber)
END_MESSAGE_MAP()BOOL CSampleDlg::OnInitDialog()
{
CDialog::OnInitDialog(); …… mylist.Create (
WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_HSCROLL|WS_TABSTOP,
CRect(10,80,450,200),
this,
IDC_LISTBOX);
mylist.AddString("1");
mylist.AddString("2");
mylist.AddString("3");
mylist.AddString("4");
mylist.AddString("5");
mylist.AddString("6");
mylist.AddString("7"); ……
)
void CSampleDlg::OnSelNumber ()
{
if (mylist.GetCurSel ()==0)
MessageBox("0");
if (mylist.GetCurSel ()==0)
MessageBox("1");
……
}哪里错了?
// 有一个ListBox,里面有n行,当选择第一行时自动弹出一个MessageBox,选中第二行时自动弹出一个
// MessageBox……以此类推
//
// 但是为什么下面代码无法实现呢?//SampleDlg.h
class CSampleDlg : public CDialog
{
afx_msg void OnSelNumber();
}//SampleDlg.cppBEGIN_MESSAGE_MAP(CSampleDlg, CDialog)
ON_BN_CLICKED(IDC_CHECKLISTBOX, OnSelNumber)
END_MESSAGE_MAP()BOOL CSampleDlg::OnInitDialog()
{
CDialog::OnInitDialog(); …… mylist.Create (
WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_HSCROLL|WS_TABSTOP,
CRect(10,80,450,200),
this,
IDC_LISTBOX);
mylist.AddString("1");
mylist.AddString("2");
mylist.AddString("3");
mylist.AddString("4");
mylist.AddString("5");
mylist.AddString("6");
mylist.AddString("7"); ……
)
void CSampleDlg::OnSelNumber ()
{
if (mylist.GetCurSel ()==0)
MessageBox("0");
if (mylist.GetCurSel ()==0)
MessageBox("1");
……
}哪里错了?
{
if (mylist.GetCurSel ()==0)
MessageBox("0");
if (mylist.GetCurSel ()==1) // 改为1
MessageBox("1");
……
}
void CSecondDlg::OnSelchangeList1()
{
// TODO: Add your control notification handler code here
if (m_listbox1.GetCurSel ()==0)
MessageBox("0");
if (m_listbox1.GetCurSel ()==1)
MessageBox("1");
if (m_listbox1.GetCurSel ()==2)
MessageBox("1");
if (m_listbox1.GetCurSel ()==3)
MessageBox("1");
if (m_listbox1.GetCurSel ()==4)
MessageBox("1");
}
你试试
ON_BN_CLICKED(IDC_CHECKLISTBOX, OnSelNumber)只有
BEGIN_MESSAGE_MAP(CSecondDlg, CDialog)
//{{AFX_MSG_MAP(CSecondDlg)
ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
c(IDC_LIST1, OnDblclkList1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
这两个还有点象,但是ON_BN_CLICKED(IDC_CHECKLISTBOX, OnSelNumber)也同样可以
选择2也没有如果使用拖动控件到界面上,双击控件生成的代码,就能够弹出MessageBox框
ON_BN_CLICKED(IDC_CHECKLISTBOX, OnSelNumber) mylist.Create (
WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_HSCROLL|WS_TABSTOP,
CRect(10,80,450,200),
this,
IDC_LISTBOX);两个资源ID不同?是不是又敲错?在调试状态下能进消息函数OnSelNumber?
如果用PreTranslateMessage,该怎么写呢?
出在Create的时候解决方法很简单,看看ClistBox Create的style,按照最初的想法做就可以了当然,最初的问题有2个:
一是消息响应出现错误,不应该是ON_BN_CLICKED,应该是ON_LBN_SELCHANGE
二是Create时候Style出现问题,差一个Style,所以不能响应问题解决~!