Solved

256 color in CListCtrl

Posted on 1998-08-31
3
415 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need a Shell script to start a service checking the port 6 52
only14 challenge 19 81
MaxSpan challenge 9 106
Excel file not created as expected 7 79
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…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

831 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