简体   繁体   English

如何在 wxListCtrl 中显示图像(多列报表样式)

[英]How to display Images in a wxListCtrl (multicolumn report style)

I already managed to create wxListCtrls with either icons or multicolumn text like this我已经设法用这样的图标或多列文本创建 wxListCtrls

Picture of two wxListCtrls 两个 wxListCtrls 的图片

Now I'd like to add an icon to each line of the text list on the left.现在我想向左侧文本列表的每一行添加一个图标。 I thought this should be possible as typical wxWidgets applications like code::blocks and wxSmith often diplay icons in list/tree views (resource browser window) and even in tabs of notebooks (compiler log window).我认为这应该是可能的,因为像 code::blocks 和 wxSmith 这样的典型 wxWidgets 应用程序经常在列表/树视图(资源浏览器窗口)甚至笔记本标签(编译器日志窗口)中显示图标。 So how can I create something like this?那么我怎样才能创建这样的东西呢? (Everybody knows Windows Explorer) (大家都知道 Windows 资源管理器)

Picture of Explorer Window with icons 带有图标的资源管理器窗口图片

I tried this...我试过这个...

SetImageList (ToolImages, wxIMAGE_LIST_NORMAL);
InsertColumn (0, "Icon");
SetColumnWidth (0, 40);
...
for (int i=0; i<5; i++)
{
    InsertItem (i, i);
    SetItemColumnImage (i, 0, i);
    SetItem (i, 1, IntToStr (i+1));
...

But as you can see, only the text gets displayd, the image column is blank.但如您所见,仅显示文本,图像列是空白的。 Is it possible at all to mix text and images in report mode?是否可以在报告模式下混合文本和图像? If not, what other wxControl class can I use to get the desired result?如果没有,我可以使用哪些其他 wxControl 类来获得所需的结果?

Many Thanks in advance.提前谢谢了。

Yes, it is possible, and the listctrl sample shows how to do it, in particular see MyFrame::InitWithReportItems() function.是的,这是可能的,并且listctrl 示例显示了如何做到这一点,特别是请参阅MyFrame::InitWithReportItems()函数。 The only difference with your code seems to be that you use a different InsertItem() overload, so perhaps you should use InsertItem(i, "") instead.与您的代码的唯一区别似乎是您使用了不同的InsertItem()重载,因此也许您应该改用InsertItem(i, "")

Also check that your image list does have the 5 icons in it.还要检查您的图像列表中是否有 5 个图标。

More generally, trying to reduce the differences between your code and the (working) sample will almost always quickly find the problem.更一般地说,尝试减少您的代码和(工作)示例之间的差异几乎总是会很快找到问题。

Thanks, VZ, but I found out that it's not the InsertItem() but the SetImageList().谢谢,VZ,但我发现它不是 InsertItem(),而是 SetImageList()。 My image list was correct, but the "which" parameter wasn't.我的图像列表是正确的,但“which”参数不正确。 Replacing wxIMAGE_LIST_NORMAL by wxIMAGE_LIST_SMALL fixes the problem!用 wxIMAGE_LIST_SMALL 替换 wxIMAGE_LIST_NORMAL 解决了这个问题! I thought "SMALL" was only meant for the SMALL_ICON mode and that "NORMAL" should be the default.我认为“SMALL”仅适用于 SMALL_ICON 模式,而“NORMAL”应该是默认值。 But yes, that makes sense, normal icons are big and don't fit in the text display.但是,是的,这是有道理的,普通图标很大并且不适合文本显示。 Would be nice if the documentation had told us that before long trial and error...如果文档告诉我们在长时间的反复试验之前......

This is a simple example for SMALL ICONIC VIEW USING WXLISTCTRL .Please place this code inside the class declaration.I did it in Frame based Windows Application using CODE BLOCKS .It will be useful to you.这是使用 WXLISTCTRL 的 SMALL ICONIC VIEW的简单示例。请将此代码放在类声明中。我使用代码块在基于框架的 Windows 应用程序中完成了它。它对您有用。

wxImageList *il=new wxImageList(32,32,false,0);

wxImageList *i2=new wxImageList(32,32,false,0);

 wxDir dir(wxGetCwd());
wxDir dir1(wxGetCwd());



    if ( !dir.IsOpened() )

    {

        // deal with the error here - wxDir would already log an error message
        // explaining the exact reason of the failure
        return;
    }
 if ( !dir1.IsOpened() )
    {
        // deal with the error here - wxDir would already log an error message
        // explaining the exact reason of the failure
        return;
    }
    puts("Enumerating object files in current directory:");

  wxString path, filename, dirstring,filename1, dirstring1, img,imgPath,path1,img1,imgPath1;
    int i=0;
path=wxT("C:\\testing\\splitterwindow\\set\\devices");
    path1=wxT("C:\\testing\\splitterwindow\\set\\actions");
    img=wxT("C:\\testing\\splitterwindow\\set\\devices\\");
    img1=wxT("C:\\testing\\splitterwindow\\set\\actions\\");


   bool cont=dir.Open(path);
   bool cont1=dir1.Open(path1);
   cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_DEFAULT);
    dirstring.Append(filename.c_str());
    cont1 = dir1.GetFirst(&filename1, wxEmptyString, wxDIR_DEFAULT);
    dirstring1.Append(filename1.c_str());
    while ( cont )
    {

        imgPath.clear();

        cont = dir.GetNext(&filename);


       dirstring.Append(filename.c_str());

       // Consturct the imagepath
       imgPath.Append(img.c_str());
       imgPath.Append(filename.c_str());



       //Now, add the images to the imagelist
       il->Add(wxBitmap(wxImage(imgPath.c_str())));
       i++;

    }

    while ( cont1 )
    {

        imgPath1.clear();
       cont1 = dir1.GetNext(&filename1);
dirstring1.Append(filename1.c_str());
       // Consturct the imagepath
       imgPath1.Append(img1.c_str());
       imgPath1.Append(filename1.c_str());

       //Now, add the images to the imagelist
       i2->Add(wxBitmap(wxImage(imgPath1.c_str())));
       i++;

    }
    //assigning the imagelist to listctrl
       ListCtrl1->AssignImageList(il, wxIMAGE_LIST_SMALL);
ListCtrl3->AssignImageList(i2, wxIMAGE_LIST_SMALL);

    for(int j=0;j < il->GetImageCount()-1;j++)
    {
        wxListItem itemCol;
        itemCol.SetId(j);
        itemCol.SetImage(j);
        itemCol.SetAlign(wxLIST_FORMAT_LEFT);
        ListCtrl1->InsertItem(itemCol);

    }
     for(int k=0;k < i2->GetImageCount()-1;k++)
    {
        wxListItem itemCol1;
        itemCol1.SetId(k);
        itemCol1.SetImage(k);
        itemCol1.SetAlign(wxLIST_FORMAT_LEFT);
        ListCtrl3->InsertItem(itemCol1);

    }

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM