Click here to Skip to main content
15,913,685 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to set an image in a listctrl. just a rectangle box 32 by16 filled with one color. But i want to be able to change the colors at will (with a color dialog).
So far, i've only been able to set the image in with pre-defined colors.

C++
HIMAGELIST hList = ImageList_Create(32,16, ILC_COLOR8 |ILC_MASK , 8, 1);
m_SmallImg.Attach(hList);
CBitmap cBmp;
cBmp.LoadBitmap(IDB_SMALLIMG);
m_SmallImg.Add(&cBmp, RGB(0,0, 0));
m_listTable.SetImageList(&m_SmallImg, LVSIL_SMALL);


How do i go about doing it? Any help is appreciated.
Posted
Updated 16-Feb-12 20:42pm
v5

1 solution

I created a sample app once that would fill a listview with a series of images created with openGL of rendered spheres. Each sphere would have a different material applied, different specular, ambient and diffuse values were also used.

In essence, I followed the following steps:

1.) Determine number of images required
2.) Create an imageList with space for this many images.
3.) Create image
4.) Add image to imageList
5.) if numImagesCreated < numImagesNeeded then goto step 3
6.) Assign imageList to listView container.
7.) Add item to listView (including specifying the index of it's image)
8.) If numListViewItems < numListViewItems required then goto step 7.


You can see the code in question here:
C++
void fillListView(HINSTANCE hInst, HWND listHwnd)
{
    HBITMAP tmpBmp;
    HIMAGELIST imgList;
    HWND ctrlWnd;
    int i;
    LV_ITEM newItem;

    int numMaterials = matLib.size();

    imgList = ImageList_Create(tileSize,tileSize,ILC_COLOR32,numMaterials,0);

    for (i=0;i<numMaterials;i++)
        {
            tmpBmp = myMakeGlSphereBmp(matLib[i]);
            ImageList_Add(imgList, tmpBmp, NULL);
            DeleteObject(tmpBmp);
        }

    ListView_SetImageList(listHwnd, imgList, LVSIL_NORMAL);

    newItem.mask = LVIF_IMAGE | LVIF_PARAM;
    newItem.iItem = 0;
    newItem.iSubItem = 0;
    newItem.state = 0;
    newItem.stateMask = 0;

    char itemString[100];
    for (i=0; i<numMaterials; i++)
    {
        newItem.iImage = i;
        newItem.lParam = i;
        ListView_InsertItem(listHwnd, &newItem);
    }
    ListView_SortItems(listHwnd, listViewLparamSHINYComparareProc, NULL);
    //ListView_SortItems(listHwnd, listViewLparamComparareProc, NULL);
    //ListView_SortItems(listHwnd, listViewLparamBRIGHTComparareProc, NULL);

    ListView_SetIconSpacing(listHwnd, tileSize+4, tileSize+4);
}


I suppose that you could simply load the image before changing it's colour with FillRect. It should be a fairly straightforward task to create a function that will return a HBITMAP of the specified dimensions that is filled with a single solid colour.

Perhaps something like:
C++
myMakeBmp(int xDim, int yDim, COLORREF colorToFillWith)



Here's my code for myMakeGlSphereBmp. I suggest you pay particular attention to the code up-to and including "DeleteObject(myBrush);"
[EDIT:] Forgot to mention, you'll need this clean-up code too
C++
SelectObject(memDC, oldBmp);
    DeleteDC(memDC);




C++
HBITMAP myMakeGlSphereBmp(material_t materialToUse)
{
    HDC memDC, screenDC;
    HBITMAP result, oldBmp;
    BITMAPINFO bitmapInfo;
    char *bitmapBits;
    RECT myRect;
    HBRUSH myBrush;

    bitmapInfo.bmiHeader.biBitCount = 24;
    bitmapInfo.bmiHeader.biWidth = tileSize;
    bitmapInfo.bmiHeader.biHeight = tileSize;
    bitmapInfo.bmiHeader.biCompression = BI_RGB;
    bitmapInfo.bmiHeader.biPlanes = 1;
    bitmapInfo.bmiHeader.biSize = sizeof(bitmapInfo.bmiHeader);
    bitmapInfo.bmiHeader.biClrImportant = 0;
    bitmapInfo.bmiHeader.biClrUsed = 0;

    screenDC = GetDC(NULL);
    memDC = CreateCompatibleDC(screenDC);

    result = CreateDIBSection(memDC, &bitmapInfo, DIB_RGB_COLORS, (void**)&bitmapBits, 0, 0);

    oldBmp = (HBITMAP)SelectObject(memDC, result);
    myRect.left=0;
    myRect.top=0;
    myRect.right=bitmapInfo.bmiHeader.biWidth;
    myRect.bottom=bitmapInfo.bmiHeader.biHeight;
//    myBrush = CreateSolidBrush(RGB(r,g,b));
  //      FillRect(memDC, &myRect, myBrush);
    DeleteObject(myBrush);

    HGLRC tmpRC;
    tmpRC = EnableOpenGL(memDC);
        glViewport(0, 0, bitmapInfo.bmiHeader.biWidth, bitmapInfo.bmiHeader.biHeight);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        float aspectRatio = (float)bitmapInfo.bmiHeader.biWidth / bitmapInfo.bmiHeader.biHeight;
//        glFrustum(-aspectRatio, -aspectRatio, -1.0, 1.0, 2.0,100.0);
        gldPerspective (40, -aspectRatio, 1, 10.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        glEnable(GL_LIGHT0);
        glEnable(GL_NORMALIZE);

        glEnable(GL_LIGHTING);

        const GLfloat light_ambient[]  = { 0.3f, 0.3f, 0.3f, 1.0f };
        const GLfloat light_diffuse[]  = { 0.5f, 0.5f, 0.5f, 1.0f };
        const GLfloat light_specular[] = { 0.8f, 0.8f, 0.8f, 1.0f };
        const GLfloat light_position[] = { -3.0f, -4.0f, 5.0f, 0.0f };
        glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
        glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
        glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
        glLightfv(GL_LIGHT0, GL_POSITION, light_position);

        const GLfloat mat_ambient[]    = { 0.5f, 0.5f, 0.5f, 1.0f };
        const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
        const GLfloat mat_specular[]   = { 0.9f, 0.9f, 0.9f, 1.0f };
        const GLfloat high_shininess[] = { materialToUse.shininess};
        glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
        glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
        glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
        glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

        glEnable(GL_COLOR_MATERIAL);
        glClearColor(1,1,1,1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        GLfloat rr,gg,bb;
        rr=(int)materialToUse.r/255.0;
        gg=(int)materialToUse.g/255.0;
        bb=(int)materialToUse.b/255.0;

        glTranslated(0,0,-3);
        glRotated(-90,1,0,0);

        glColor3d(rr,gg,bb);

        myGlutBall(1, 32,32);

//        myGlutCylinder(1, 1, 64);

        SwapBuffers(memDC);

    wglMakeCurrent( NULL, NULL );
    wglDeleteContext( tmpRC );

    SelectObject(memDC, oldBmp);
    DeleteDC(memDC);

    return result;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900