如何更改与CListCtrl关联的位图的颜色? [英] How do i change colors of bitmaps associated with a CListCtrl?

查看:68
本文介绍了如何更改与CListCtrl关联的位图的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在listctrl中设置图像.只是一个用一种颜色填充的矩形框32 x 16.但是我希望能够随意更改颜色(带有颜色对话框).
到目前为止,我只能以预定义的颜色设置图像.

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.

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.

推荐答案

我曾经创建了一个示例应用程序,该应用程序将用一系列用渲染球体的openGL创建的图像填充列表视图.每个球体都会应用不同的材质,还使用了不同的镜面反射,环境和漫反射值.

本质上,我遵循以下步骤:

1.)确定所需的图像数量
2.)创建一个imageList,其中包含用于容纳许多图像的空间.
3.)创建图片
4.)将图像添加到imageList
5.)如果numImagesCreated< numImagesNeeded然后转到步骤3
6.)将imageList分配给listView容器.
7.)将项目添加到listView(包括指定其图像的索引)
8.)如果numListViewItems<所需的numListViewItems,然后转到步骤7.


您可以在此处查看有问题的代码:
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:
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);
}



我想您可以在使用FillRect更改颜色之前简单地加载图像.创建一个函数将返回相当简单的任务,该函数将返回用单色填充的指定尺寸的HBITMAP.

也许像这样:



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:

myMakeBmp(int xDim, int yDim, COLORREF colorToFillWith)




这是我的myMakeGlSphereBmp代码.建议您特别注意最新的代码,包括"DeleteObject(myBrush);"
忘记提及了,您也将需要此清理代码




Here''s my code for myMakeGlSphereBmp. I suggest you pay particular attention to the code up-to and including "DeleteObject(myBrush);"
Forgot to mention, you''ll need this clean-up code too

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









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;
}


这篇关于如何更改与CListCtrl关联的位图的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆