断言在clistctrl上添加子项 [英] Assertion adding subitems on a clistctrl
问题描述
项目:MFC-SDI-C ++(VS2012)
大家好我新编程在MFC中,我有下一个:
1.我已经构建了一个CListCtrl-looking作为CDialog的报告(使用控件工具箱添加了CListCtrl)。 CDialogEx位于CDockablePanel上,该程序在打开程序时始终显示。我在CListCtrl中添加了列和项,没有任何问题 (Class:CCoordsListDlg) 。
2.我创建了一个不同的类来生成双重类型数据 (类:CCoordsGenerator) 。生成这些数据时捕获鼠标单击屏幕坐标没有任何问题。这些数据应该作为子项添加在CListCtrl上。
3.要在CListCtrl上添加生成数据,我有一个从CCoordsGenerator类调用的方法,但是当添加子项的方法运行DEBUG ASSERTION时出现。
4. DEBUG ASSERTION停止: ASSERT(:: IsWindow(m_hWnd));下一节中的winctrl2.cpp文件:
Project: MFC-SDI-C ++ (VS2012)
Hi everybody Im new programming in MFC i have the next:
1. I have built a CListCtrl-looking as a report on a CDialog (The CListCtrl was added using the controls toolbox). The CDialogEx is on a CDockablePanel that are created when the program is opened and always is showed. I added columns and items to CListCtrl to be created without any problems (Class: CCoordsListDlg).
2. I have created a different class that generate double type data (Class: CCoordsGenerator). These data are generated catching the mouse click screen coordinates without any problems. These data should be added on the the CListCtrl as subitems.
3. To add the generate data on the CListCtrl, i have a method that is called from the CCoordsGenerator class but when the method that add the subitems run a DEBUG ASSERTION appears.
4. The DEBUG ASSERTION stop in: ASSERT(::IsWindow(m_hWnd)); of the winctrl2.cpp file in the next section:
BOOL CListCtrl::SetItemText(int nItem, int nSubItem, LPCTSTR lpszText)
{
ASSERT(::IsWindow(m_hWnd));
ASSERT((GetStyle() & LVS_OWNERDATA)==0);
LVITEM lvi;
lvi.iSubItem = nSubItem;
lvi.pszText = (LPTSTR) lpszText;
return (BOOL) ::SendMessage(m_hWnd, LVM_SETITEMTEXT, nItem, (LPARAM)&lvi);
}
创建CListCtrl的类代码是下一个 (CCoordsListDlg.cpp) :
The class code where the CListCtrl is created is the next (CCoordsListDlg.cpp):
BOOL CCoordsListDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// TODO: Add extra initialization here
CListCtrl m_coordslist;
m_coordslist.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_SINGLEROW |LVS_EX_GRIDLINES|LVS_SHOWSELALWAYS);
// The tree columns are created
// The column where the points will be displayed is created
m_coordslist.InsertColumn(0,"point");
m_coordslist.SetColumnWidth(0,90);
// The column where the point x-coord will be displayed is created
m_coordslist.InsertColumn(1,"x coord");
m_coordslist.SetColumnWidth(1,64);
// The column where the point y-coord will be displayed is created
m_coordslist.InsertColumn(2,"y coord");
m_coordslist.SetColumnWidth(2,64);
// The points are added
m_coordslist.InsertItem(0,"p1");
m_coordslist.InsertItem(1,"p2");
m_coordslist.InsertItem(2,"p3");
m_coordslist.InsertItem(3,"p4");
return TRUE;
// EXCEPTION: OCX Property Pages should return FALSE
}
生成数据的部分代码是 (CCoordsGenerator.cpp) :
A portion of the code where the data are generated is (CCoordsGenerator.cpp):
void CCoordsGenerator::OnLeftButtonDown()
{
// Get the pickposition
double q[2];
picker->GetPickPosition(q);
// Share pick position
x_coord = q[0];
y_coord = q[1];
// The coordinates value are added as subitems to a CListCtrl
CCoordsListDlg m_coordDlg;
m_coordDlg.RWImageInteractor(row, x_coord, y_coord); //<<<< HERE IS WHEN THE COORDS ARE SENDED TO A CListCtrl >>>>
// Draw a sphere on the point
createSpherePoint(row, x_coord,y_coord);
}
添加的类 CCoordsListDlg 的方法子项目是:
The method of the class CCoordsListDlg that add the subitems is:
void CCoordsListDlg::RWImageInteractor(int row, double x_coord, double y_coord)
{
// Converting the double variable to LPTSTR
char x_buff[8],
y_buff[8];
_gcvt(x_coord,8,x_buf);
_gcvt(y_coord,8,y_buff);
// The coordinates are added to list as subitems
m_coordslist.SetItemText(row,1,x_buff);
m_coordslist.SetItemText(row,2,y_buff);
}
你能帮我理解我的错误吗?
谢谢大家非常感谢你的帮助
问候
我的尝试:
我真的不知道我的代码有什么问题。我试图使用LVITEM方法来添加子项并将注意力集中在CListCtrl上但是断言总是出现。
Could you help me to understand where my error is?
Thank you all so much for your help
Regards
What I have tried:
really I don´t know what is the trouble with my code. I've tried to use the LVITEM methosd to add the subitems and put progamatically the focus on the CListCtrl but the assertion always appears.
推荐答案
列表控件实例必须是对话框类的成员。实际上你是在OnInitDialog
中创建一个新的本地实例,而不是使用类宽成员。
一个典型的实现如下:
The list control instance must be a member of your dialog class. Actually you are creating a new local instance inOnInitDialog
using that instead of the class wide member.
A typical implementation would look like:
// CCoordsListDlg.h
class CCoordsListDlg : public CDialogEx
{
// ...
protected:
CListCtrl m_coordslist;
// ...
};
// CCoordsListDlg.cpp
void CCoordsListDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
// When list is defined by dialog resource template.
// If not, it must be created / sized in OnInitDialog.
DDX_Control(pDX, IDC_OF_LIST, m_coordslist);
// ...
}
BOOL CCoordsListDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// This would create a new local instance. Remove it.
//CListCtrl m_coordslist;
m_coordslist.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_SINGLEROW |LVS_EX_GRIDLINES|LVS_SHOWSELALWAYS);
// ...
}
类似的情况可能适用于创建新本地 CCoordsListDlg
但未显示的情况:
Similar may apply here where a new local CCoordsListDlg
is created but not shown:
void CCoordsGenerator::OnLeftButtonDown()
{
// ...
// This looks suspicious too.
// Remove it if m_coordDlg is a class member and dialog already shown
// or show dialog here.
CCoordsListDlg m_coordDlg;
m_coordDlg.RWImageInteractor(row, x_coord, y_coord); //<<<< HERE IS WHEN THE COORDS ARE SENDED TO A CListCtrl >>>>
// ...
}
我不能完全确定,因为我没有制作任何MFC 15年左右的项目,但我认为你需要调用Create方法来设置父窗口。
参见CListCtrl::Create [ ^ ]
或者 CListCtrl :::CreateEx [ ^ ]
此代码
I cannot be entirely sure, because I haven't made any MFC projects in 15 years or so, but I think you need to call the Create method in order to set the parent window.
See CListCtrl::Create[^]
or maybe CListCtrl::CreateEx[^]
This code
CListCtrl m_coordslist;
m_coordslist.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_SINGLEROW |LVS_EX_GRIDLINES|LVS_SHOWSELALWAYS);
应更改为类似
should be changed to something like
CListCtrl m_coordslist;
ASSERT(m_coordslist.CreateEx(
LVS_EX_FULLROWSELECT | LVS_EX_SINGLEROW |LVS_EX_GRIDLINES|LVS_SHOWSELALWAYS,
WS_CHILD|WS_VISIBLE|WS_BORDER|LVS_REPORT|LVS_EDITLABELS,
CRect(10,10,400,200), pParentWnd, IDD_SOME_ID));
其中 pParentWnd
是的一个实例CCoordsListDlg
和 IDD_SOME_ID
是列表控件的ID。
Where pParentWnd
is an instance of CCoordsListDlg
and IDD_SOME_ID
is the ID of your list control.
这篇关于断言在clistctrl上添加子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!