[英]CComboBoxEx debug assert failure: afxcmn2.inl Line 334
Please note that I have found that I need to add COMBOBOXEXITEM
values much like LVITEM
based on this book: Image Lists and ComboBoxEx Controls |请注意,我发现我需要添加COMBOBOXEXITEM
值,就像基于本书的LVITEM
一样: Image Lists and ComboBoxEx Controls | Programming Windows with MFC, Second Edition (flylib.com)使用 MFC 对 Windows 进行编程,第二版 (flylib.com)
FYI, I am getting the above error on the SetImageList
call:仅供参考,我在SetImageList
调用中收到上述错误:
// Add color icons to combobox
for (int nCount = 0; nCount < m_colorBarTemplateFiles.GetCount(); nCount++) {
CBitmap colorTemplateBitmap;
const CString fname = colorBarTemplateDirectory + "\\" + m_colorBarTemplateImageFileNames[nCount];
HANDLE colorTemplateImageHandle = LoadImage(0, fname, IMAGE_BITMAP, 16, 16, LR_LOADFROMFILE);
colorTemplateBitmap.FromHandle((HBITMAP)colorTemplateImageHandle);
m_colorBarTemplateImages.Add(&colorTemplateBitmap, (COLORREF)0xFFFFFF);
}
SetImageList(&m_colorBarTemplateImages);
In the book I link, the author writes this code (BTW, I have purchased the book which will hopefully come with the CD, but it won't arrive for another 3 weeks):在我链接的书中,作者写了这段代码(顺便说一句,我已经购买了希望随 CD 一起提供的书,但它不会再到 3 周):
m_il.Create (IDB_IMAGES, 16, 1, RGB (255, 0, 255));
SetImageList (&m_il);
But unfortunately, on the website the code does not have an *.rc file:但不幸的是,网站上的代码没有 *.rc 文件:
So I don't know how he sets up IDB_IMAGES
.所以我不知道他是如何设置IDB_IMAGES
。 As far as I understand, the IDB_IMAGES is a big bitmap that is partitioned out into different icons, but it is not clear to met how to set that up in MFC.据我了解,IDB_IMAGES 是一个大的 bitmap,它被划分为不同的图标,但尚不清楚如何在 MFC 中进行设置。
This is afxcmn2.inl Line 334:这是 afxcmn2.inl 第 334 行:
_AFXCMN_INLINE CImageList* CComboBoxEx::SetImageList(_In_ CImageList* pImageList)
{ ASSERT(::IsWindow(m_hWnd)); return CImageList::FromHandle((HIMAGELIST) ::SendMessage(m_hWnd, CBEM_SETIMAGELIST, 0, (LPARAM)pImageList->GetSafeHandle())); }
So somehow I am not creating the handle properly.所以不知何故我没有正确地创建句柄。 I have also checked out other posts like:我还查看了其他帖子,例如:
visual c++ - How to add Images to CListCtrl in MFC - Stack Overflow visual c++ - 如何在 MFC 中将图像添加到 CListCtrl - 代码日志
ccombobox - MFC CComboBoxEx icon update issue - Stack Overflow ccombobox - MFC CComboBoxEx图标更新问题 - Thinbug
CImageList Class | CImageList Class | Microsoft Docs 微软文档
CComboBoxEx Class | CComboBoxEx Class | Microsoft Docs 微软文档
Do you have any suggestions?你有什么建议吗? TIA. TIA。
UPDATE:更新:
Please note that I just learned that I need to be able to add these colors dynamically at runtime so it turns out I won't be able to work with *.rc
files and CBitmap
s.请注意,我刚刚了解到我需要能够在运行时动态添加这些 colors ,所以事实证明我将无法使用*.rc
文件和CBitmap
s。 Instead, I'll have to research adding a colored region to the ComboBoxEx
in OnPaint
or OnDraw
somehow using using something like this:相反,我将不得不研究在OnPaint
或OnDraw
中以某种方式使用以下方法向ComboBoxEx
添加彩色区域:
COLORREF itemColor = colorArray[subitem][item];
CRect rect;
GetSubItemRect(item, subitem, LVIR_LABEL, rect);
CDC* pDc = GetDC();
pDc->FillRect(rect, &CBrush(itemColor));
ReleaseDC(pDc);
So I'll keep you posted on what I figure out.所以我会及时告诉你我的发现。
This is how I ended up adding a color swatch to the combobox dynamically without using *.rc files or *.bmp files.这就是我最终在不使用 *.rc 文件或 *.bmp 文件的情况下动态地将色样添加到 combobox 的方式。 In the parent windows class OnCreate
function where a key argument is CBS_OWNERDRAWFIXED
which causes the DrawItem
function to be called every time the combobox is opened by the user: In the parent windows class OnCreate
function where a key argument is CBS_OWNERDRAWFIXED
which causes the DrawItem
function to be called every time the combobox is opened by the user:
int ParentToolbarWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
// ...
CRect rect;
int toolbarButtonIndex = 0;
wndToolBar.GetItemRect(toolbarButtonIndex, &rect);
CSize colorSwatchSize = pDC->GetTextExtent("1234567");
rect.left = rect.right;
rect.right = rect.left + colorSwatchSize.cx + 60;
if (!wndToolBar.cmbColor.Create(WS_CHILD | WS_VISIBLE | CBS_DROPDOWN | WS_VSCROLL | CBS_HASSTRINGS | CBS_OWNERDRAWFIXED,
rect, &wndToolBar, IDC_COMBO_XLINE)) {
// error message...
return -1;
}
// ...
UpdateCombobox();
// ...
}
void ParentToolbarWindow::UpdateCombobox()
{
wndToolBar.cmbColor.ResetContent();
// Get input value CStringArray "colorSwatchNames" for combobox...
for (int i = 0; i < colorSwatchNames.GetCount(); i++) {
if (wndToolBar.cmbColor.AddString(colorSwatchNames[i]) == CB_ERR || wndToolBar.cmbColor.AddString(colorSwatchNames[i]) == CB_ERRSPACE) {
AfxMessageBox("Error setting up combo");
break;
}
}
wndToolBar.cmbColor.SetCurSel(0);
}
Here is where I actually draw the color swatch along with the text:这是我实际绘制颜色样本和文本的地方:
void CColorCombo::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// ...
CRect itemRect(lpDrawItemStruct->rcItem);
CRect blockRect(itemRect);
// ... setup rect sizes and other setup steps
// Draw color swatch as columns, each one pixel wide where inputColors is CStringArray
for (int i = 0; i < inputColors.GetCount(); i++) {
CString inputColorLine = inputColors[i];
// read and tokenize inputColors into inputColorLine...
int red = atoi(inputColorLine[0]);
int green = atoi(inputColorLine[1]);
int blue = atoi(inputColorLine[2]);
COLORREF color = RGB(red, green, blue);
CPen* pen = new CPen(PS_SOLID, 1, color);
dc.SelectObject(pen);
dc.MoveTo(blockRect.left + i, blockRect.top);
dc.LineTo(blockRect.left + i, blockRect.bottom);
if (pen != NULL) delete pen;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.