动态创建ListBox出错

caddor2011 2011-10-02 10:32:59
在OnInitDialog中


m_listBox.Create(WS_CHILD|WS_VISIBLE|LBS_STANDARD,CRect(10,10,300,300),this,IDC_LISTBOX);

m_listBox.ShowWindow(SW_SHOW);

int i; CString tmp;

for(i=0;i<100;i++)
{

m_listBox.AddString(NULL);
m_listBox.SetItemData(i,i);

tmp.Format("第ssss%d行", i);

m_listBox.StoreData(tmp);

}

运行到 AddString 的时候程序挂了,提示访问Violation




之所以改动态创建,是因为 <mfc window程序设计》书上提到制定了风格 LBS_STANDARD,

就可以由于滚动条了,,,由于我刚开始采用ddx/ddv机制,变量绑定控件的方法, 但是没有滚动条的出现。。。

所以,我改成 动态创建, 在Create函数中指定 LBS_STANDARD 这个风格, 结果出错了。。。。。

注意: 这个ListBox是自绘的!!!





...全文
412 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
firefly3233 2011-10-03
  • 打赏
  • 举报
回复
重载MeasureItem虚函数就好了,结帖吧?
caddor2011 2011-10-02
  • 打赏
  • 举报
回复
改成"" 果然没有问题,但是 问题来了


如何将 动态动态创建的 ListBox 的风格设置成自绘型的呢??

我们知道 对于 非动态的 ,可以 在属性里面修改。。但是对于动态的就无法修改啊??

mfc window程序一书里面基本上把所有的风格都提到了。。

单独没有提到如何手动 来修改 为自绘风格







[Quote=引用 4 楼 caddor2011 的回复:]

不是吧 ,我把控件与变量关联, 在这种情况下 ,从来不出错。。。。

为什么对于 动态创建的情况出错呢???

引用 3 楼 gameslq 的回复:

m_listBox.AddString(NULL);
NULL 为0指针,所以出错
改为
m_listBox.AddString("");
[/Quote]
caddor2011 2011-10-02
  • 打赏
  • 举报
回复
不是吧 ,我把控件与变量关联, 在这种情况下 ,从来不出错。。。。

为什么对于 动态创建的情况出错呢???

[Quote=引用 3 楼 gameslq 的回复:]

m_listBox.AddString(NULL);
NULL 为0指针,所以出错
改为
m_listBox.AddString("");
[/Quote]
gameslq 2011-10-02
  • 打赏
  • 举报
回复
m_listBox.AddString(NULL);
NULL 为0指针,所以出错
改为
m_listBox.AddString("");
caddor2011 2011-10-02
  • 打赏
  • 举报
回复
up up
caddor2011 2011-10-02
  • 打赏
  • 举报
回复
up up
gameslq 2011-10-02
  • 打赏
  • 举报
回复
一旦运行到Create这里程序就提示:

winctrl1.cpp Line:107
出错

原因如下:加上自绘属性(LBS_OWNERDRAWFIXED或者LBS_OWNERDRAWVARIABLE)之后,需要实现
MeasureItem 和 DrawItem 函数功能(也就是需要子类化)
解决方法:
1。从CListBox类继承,建立一子类MyListBox 并设变量 MyListBox m_listBox;(你的代码错在从
CListBox 建立变量 m_listBox)
2。在 MyListBox 类中实现 MeasureItem 和 DrawItem 函数接口(在这两个函数自绘)
3。你原来写的代码即可运行
caddor2011 2011-10-02
  • 打赏
  • 举报
回复
已发 ,劳烦你了。(注意:查收)

[Quote=引用 15 楼 firefly3233 的回复:]

在,发吧
[/Quote]
firefly3233 2011-10-02
  • 打赏
  • 举报
回复
在,发吧
caddor2011 2011-10-02
  • 打赏
  • 举报
回复
大哥,我彻底崩溃了 。。。。


我开始怀疑自己了。。。。

改成你那样,还是程序挂了。。。


你今晚方便嘛, 我打包发到你邮箱,帮忙看一下,不知道是怎么回事,好奇怪,




[Quote=引用 13 楼 firefly3233 的回复:]

m_listBox.Create(WS_VSCROLL|WS_HSCROLL|WS_CHILD|LBS_NOTIFY|LBS_NOINTEGRALHEIGHT|LBS_OWNERDRAWFIXED ,CRect(10,10,300,300),this,IDC_LISTBOX);

这样应该行吧?
[/Quote]
firefly3233 2011-10-02
  • 打赏
  • 举报
回复
m_listBox.Create(WS_VSCROLL|WS_HSCROLL|WS_CHILD|LBS_NOTIFY|LBS_NOINTEGRALHEIGHT|LBS_OWNERDRAWFIXED ,CRect(10,10,300,300),this,IDC_LISTBOX);

这样应该行吧?

caddor2011 2011-10-02
  • 打赏
  • 举报
回复
还不是不行 ,很奇怪,一运行到


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

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here



m_listBox.Create(WS_CHILD|WS_VISIBLE|LBS_STANDARD|LBS_OWNERDRAWFIXED ,CRect(10,10,300,300),this,IDC_LISTBOX);

m_listBox.ShowWindow(SW_SHOW);

int i; CString tmp;

for(i=0;i<100;i++)
{

m_listBox.AddString("");
m_listBox.SetItemData(i,i);

tmp.Format("第ssss%d行", i);

m_listBox.StoreData(tmp);

}




// m_listBox.SetHorizontalExtent(500);
return TRUE; // return TRUE unless you set the focus to a control
}

一旦运行到Create这里程序就提示:

winctrl1.cpp Line:107
出错


firefly3233 2011-10-02
  • 打赏
  • 举报
回复
我一直这样用,没发现问题呀?
BOOL CMyListBox::Create(CWnd* pParentWnd, const RECT& rect, UINT nID)
{
DWORD dwStyle = WS_VSCROLL|WS_HSCROLL|WS_CHILD|LBS_NOTIFY|LBS_NOINTEGRALHEIGHT|LBS_OWNERDRAWFIXED;
return CWnd::CreateEx(WS_EX_CLIENTEDGE,"LISTBOX",NULL,dwStyle,rect,pParentWnd,nID,NULL);
}

int CMyListBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CListBox::OnCreate(lpCreateStruct) == -1)
return -1;
//设置字体
SetFont(GetParent()->GetFont());
//增加项
for (int i = 0; i < sizeof(m_idlist) / sizeof(DWORD); i ++)
{
AddString(NULL);
SetItemData(i, m_idlist[i]);
}

return 0;
}

firefly3233 2011-10-02
  • 打赏
  • 举报
回复
这样不就自绘了?
m_listBox.Create(WS_CHILD|WS_VISIBLE|LBS_STANDARD|LBS_OWNERDRAWFIXED,CRect(10,10,300,300),this,IDC_LISTBOX);
caddor2011 2011-10-02
  • 打赏
  • 举报
回复
这么说吧, 对于动态创建的ListBox 如何指定其 是自绘的风格,我发现无法做到。。

我在msdn查询了 所有风格,见6 楼。。。。

基本上无法指定其风格为自绘的。。


另外一个问题:我没有听懂你的意思, 对于非动态的创建, 可以AddString(NULL)

为什么对于动态的 AddString(NULL)就出错,而要改成:AddString("")




[Quote=引用 8 楼 firefly3233 的回复:]

没有这个样式,不是自绘,所以AddString必须要指定串地址。
有了这个样式,就不必指定串地址了
[/Quote]
firefly3233 2011-10-02
  • 打赏
  • 举报
回复
没有这个样式,不是自绘,所以AddString必须要指定串地址。
有了这个样式,就不必指定串地址了
firefly3233 2011-10-02
  • 打赏
  • 举报
回复
自绘的?
m_listBox.Create(WS_CHILD|WS_VISIBLE|LBS_STANDARD,CRect(10,10,300,300),this,IDC_LISTBOX);

这里面没有LBS_OWNERDRAWFIXED样式呀?
caddor2011 2011-10-02
  • 打赏
  • 举报
回复
LBS_DISABLENOSCROLL

LBS_EXTENDEDSEL

LBS_HASSTRINGS Specifies an owner-draw list box that contains items consisting of strings. The list box maintains the memory and pointers for the strings so the application can use the GetText member function to retrieve the text for a particular item.

LBS_MULTICOLUMN Specifies a multicolumn list box that is scrolled horizontally. The SetColumnWidth member function sets the width of the columns.

LBS_MULTIPLESEL

LBS_NODATA Specifies a no-data list box. Specify this style when the count of items in the list box will exceed one thousand. A no-data list box must also have the LBS_OWNERDRAWFIXED style, but must not have the LBS_SORT or LBS_HASSTRINGS style.

A no-data list box resembles an owner-drawn list box except that it contains no string or bitmap data for an item. Commands to add, insert, or delete an item always ignore any given item data; requests to find a string within the list box always fail. The system sends the WM_DRAWITEM message to the owner window when an item must be drawn. The itemID member of the DRAWITEMSTRUCT structure passed with the WM_DRAWITEM message specifies the line number of the item to be drawn. A no-data list box does not send a WM_DELETEITEM message.

LBS_NOINTEGRALHEIGHT The size of the list box is exactly the size specified by the application when it created the list box. Usually, Windows sizes a list box so that the list box does not display partial items.

LBS_NOREDRAW List-box display is not updated when changes are made. This style can be changed at any time by sending a WM_SETREDRAW message.

LBS_NOSEL

LBS_NOTIFY

LBS_OWNERDRAWFIXED The owner of the list box is responsible for drawing its contents; the items in the list box are the same height.

LBS_OWNERDRAWVARIABLE

LBS_SORT Strings in the list box are sorted alphabetically.

LBS_STANDARD

LBS_USETABSTOPS

LBS_WANTKEYBOARDINPUT


msdn 就这几个风格



【更新至2025年】2001-2025年上市公司数字化转型年报词频统计(吴非、赵宸宇、甄红线)(300+年报词频统计) 1、时间:2001-2025年 2、来源:上市公司年报 3、参考文献:企业数字化转型与资本市场表现——来自股票流动性的经验证据(吴非) 数字化转型如何影响企业全要素生产率(赵宸宇) 知识产权行政保护与企业数字化转型(甄红线) 4、方法说明:(1)参考吴非老师的做法,对人工智能技术、大数据技术、云计算技术、区块链技术、数字技术运用五个维度76个数字化相关词频进行统计 (2)参考赵宸宇老师的做法,对数字技术应用、互联网商业模式、智能制造、现代信息系统四个维度99个数字化相关词频进行统计 (3)参考甄红线老师的做法,对技术分类、组织赋能、数字化应用等类别下139个数字化相关词频进行统计 5、指标:年份、股票代码、公司简称、行业名称、行业代码、全文-文本总长度、仅中英文-文本总长度、人工智能技术-吴、大数据技术-吴、云计算技术-吴、区块链技术-吴、数字技术运用-吴、数字技术应用-赵、互联网商业模式-赵、智能制造-赵、现代信息系统-赵、技术分类-人工智能技术-甄、技术分类-区块链技术-甄、技术分类-云计算技术-甄、技术分类-大数据技术-甄、组织赋能-人工智能技术-甄、组织赋能-云计算技术-甄、组织赋能-大数据技术-甄、组织赋能-广义数字技术-甄、数字化应用-技术创新-甄、数字化应用-流程创新-甄、数字化应用-业务创新-甄、人工智能、商业智能、图像理解、投资决策辅助系统、智能数据分析、智能机器人、机器学习、深度学习、语义搜索、生物识别技术、人脸识别、语音识别、身份验证、自动驾驶、自然语言处理、大数据、数据挖掘、文本挖掘、数据可视化、异构数据、征信、增强现实、混合现实、虚拟现实、云计算、流计算、图计算、内存计算、多方安全计算、类脑计算、绿色计算、认知计算等300+词频

16,550

社区成员

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

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

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