Solved

256 color in CListCtrl

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mixString challenge 36 111
Separate into files by filename 12 75
maven archtype selection in eclipse 1 53
matchUp  challenge 9 93
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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

911 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

18 Experts available now in Live!

Get 1:1 Help Now