WTL学习(3)

来源:互联网 发布:知米听力 编辑:程序博客网 时间:2024/06/05 21:11

这本WTL指南的电子书是有附件的,在vckbase上,不过现在下载的话应该是需要注册

再者想尽量自己动手编写一下,那些附件Demo下载运行总是觉得不够深入

还是DDX部分

有如下代码

LRESULT CMainDlg::OnListItemchanged ( NMHDR* phdr )

{

NMLISTVIEW* pnmlv = (NMLISTVIEW*) phdr;

int nSelItem = m_wndList.GetSelectedIndex();

CString sMsg;

 

// If no item is selected, show "none". Otherwise, show its index.

if ( -1 == nSelItem )

      sMsg = _T("(none)");

else

      sMsg.Format ( _T("%d"), nSelItem );

SetDlgItemText ( IDC_SEL_ITEM, sMsg );

return 0;  // retval ignored

}

其他的不重要,问题出现在int nSelItem = m_wndList.GetSelectedIndex();这句

编译能能过,执行也能启动,这是一个List Control控件,想要在选定上面的某一项时,让下面IDC_SEL_ITEM静态文本框显示变化,但结果一旦选定,程序就崩溃

按提示是在这里

//atlctrls.h

 int GetSelectedIndex() const
 {
  ATLASSERT(::IsWindow(m_hWnd));
  ATLASSERT((GetStyle() & LVS_SINGLESEL) != 0);
  return (int)::SendMessage(m_hWnd, LVM_GETNEXTITEM, (WPARAM)-1, MAKELPARAM(LVNI_ALL | LVNI_SELECTED, 0));
 }

 

ATLASSERT((GetStyle()&LVS_SINGLESEL)!=0);这一句崩掉了

找答案

http://www.codeguru.com/forum/archive/index.php/t-389564.html

Hello, I am sorry, but I am a C++ medium newbie, and I should continue the work of a friend.
It s a kind of trace and there is a ListView with processes. When I click on one of this processes, I get the Error the Assertation failed.
It always happens in the second of these two lines...

ATLASSERT(::IsWindow(m_hWnd));
ATLASSERT((GetStyle() & LVS_SINGLESEL) != 0);


GetStyle points to zero put why ??
Thanks a lot

看来是一样的,答案如下

 

Can i ask you two things.
From which cpp file you are getting this assertion message.
I guss it would be a list control class(猜得很对), if so is it your own list control class or MFC class.
are you subclassing the list control using your own or by some one else list control class.
what my guss is you are using a derived list contol class. and this control class is only supporting multi selection.(这个感觉就不对了,应该是只支持单一选择,而资源却没有设定成单选的) but you may not have set your list control resource multiselection property.
i am not sure its a guss only. think in this way also. it may help you :)

试一试:

 框没有设成单选,更改如下,添加上Single selection就行了。

 

原创粉丝点击