Solved

256 color in CListCtrl

Posted on 1998-08-31
3
407 Views
Last Modified: 2013-11-19
Hi,
I have CTreeCtrl within a dialog box and I want to use 256 color bitmaps that a stored in a CImageList. But everything I tried seemed
not to work. The Images are allways displayed  in gray.

Does anybody have a solution for this problem?

Thanx in advance,
 Tom

0
Comment
Question by:stzclaus
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
motigust earned 100 total points
ID: 1321492
Hi Stzclaus,

The problem is that you have needs to adjust the palette of your conrol to handle the 256 colors Bitmaps.

You should to write a program that gets the colors from the bitmap and creates a palette.

Then select the palette, and realize it, it should look something like this:
   GetBitmapAndPalette(IDB_1,&MyBitmap,Palette);
   dc.SelectPalette( &palette, FALSE );
   dc.RealizePalette();

The function that gets the plette from the bitmap should be somthing like this:

BOOL CMyClass::GetBitmapAndPalette(UINT nIDResource, CBitmap &bitmap, CPalette &pal)
{
      LPCTSTR lpszResourceName = (LPCTSTR)nIDResource;

      HBITMAP hBmp = (HBITMAP)::LoadImage( AfxGetInstanceHandle(),
                  lpszResourceName, IMAGE_BITMAP, 0,0, LR_CREATEDIBSECTION );

      if( hBmp == NULL )
            return FALSE;

      bitmap.Attach( hBmp );

      // Create a logical palette for the bitmap
      DIBSECTION ds;
      BITMAPINFOHEADER &bmInfo = ds.dsBmih;
      bitmap.GetObject( sizeof(ds), &ds );

      int nColors = bmInfo.biClrUsed ? bmInfo.biClrUsed : 1 << bmInfo.biBitCount;

      // Create a halftone palette if colors > 256.
      CClientDC dc(NULL);                  // Desktop DC
      if( nColors > 256 )
            pal.CreateHalftonePalette( &dc );
      else
      {
            // Create the palette

            RGBQUAD *pRGB = new RGBQUAD[nColors];
            CDC memDC;
            memDC.CreateCompatibleDC(&dc);

            memDC.SelectObject( &bitmap );
            ::GetDIBColorTable( memDC, 0, nColors, pRGB );

            UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
            LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];

            pLP->palVersion = 0x300;
            pLP->palNumEntries = nColors;

            for( int i=0; i < nColors; i++)
            {
                  pLP->palPalEntry[i].peRed = pRGB[i].rgbRed;
                  pLP->palPalEntry[i].peGreen = pRGB[i].rgbGreen;
                  pLP->palPalEntry[i].peBlue = pRGB[i].rgbBlue;
                  pLP->palPalEntry[i].peFlags = 0;
            }

            pal.CreatePalette( pLP );

            delete[] pLP;
            delete[] pRGB;
      }

      return TRUE;
}



Have fun,

      Moti  :-)

0
 

Author Comment

by:stzclaus
ID: 1321493
Hi Moti,

Thank you for your answer. I've tried the code you gave me, but the bitmaps still appear in grayscale. I don't know exactly what else is wrong in my code.
Maybe you could help me with the following questions.
 How do I have to create the CImageList?
   I use: pMyImageList->Create(IDB_BLABLA,XX,Y,0)
   Maybe I should use pMyImageList->Create(x,x ILC_COLOR8,x,x) ??
   If I do so, do I have to Add every Bitmap I want to use? And how do I get the Bitmap? With the    same function you gave to me?

To what DC do I have to select the pallet?
 I tried  CClientDC myDC (this) and CClientDC myDC (NULL)
Is this wrong?

It would be great, if you could help me!

Many, many thanx in advance

Tom

0
 

Author Comment

by:stzclaus
ID: 1321494
Hi Moti!

It works!

I think the only problem was, that I used the wrong create-function for my CImageList.
I tried Create(x,x ILC_COLOR8,x,x), Add (IDB_IMAGEWHATSOEVER,xx ) and everything was fine. I didn't even had to create or add a pallete!

Anyway thank you for your troubles!

Tom


0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now